首页 > 代码库 > XDOJ_1003_高精度+进制
XDOJ_1003_高精度+进制
http://acm.xidian.edu.cn/problem.php?id=1003
类似高精度加法的模版,模拟一下即可。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int change1(char a){ switch(a) { case ‘A‘:return 10; case ‘B‘:return 11; case ‘C‘:return 12; case ‘D‘:return 13; case ‘E‘:return 14; case ‘F‘:return 15; default :return a-‘0‘; }}char change2(int a){ switch(a) { case 10:return ‘A‘; case 11:return ‘B‘; case 12:return ‘C‘; case 13:return ‘D‘; case 14:return ‘E‘; case 15:return ‘F‘; default:return a+‘0‘; }}char s1[100],s2[100];int n,ans[100];int main(){ while(~scanf("%d",&n)) { scanf("%s%s",s1,s2); int p1 = strlen(s1)-1,p2 = strlen(s2)-1; int cnt = 0,temp = 0; while(p1 >= 0 && p2 >= 0) { int now = change1(s1[p1])+change1(s2[p2])+temp; ans[++cnt] = now%n; temp = now/n; p1--; p2--; } while(p1 >= 0) { int now = change1(s1[p1])+temp; ans[++cnt] = now%n; temp = now/n; p1--; } while(p2 >= 0) { int now = change1(s2[p2])+temp; ans[++cnt] = now%n; temp = now/n; p2--; } if(temp) ans[++cnt] = temp; for(int i = cnt;i > 0;i--) putchar(change2(ans[i])); putchar(‘\n‘); } return 0;}
XDOJ_1003_高精度+进制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。