二十进制的两数相加
喜闻乐见的a+b
Description
喜闻乐见的a+b来了,当然二进制,八进制,十进制什么的估计你们也懒得写了,来个20进制的a+b
Input
给出多组a和b,每个数字一行
数字由0~9和小写字母a~j(10~19)
给出的长度不超过100
Output
每组数据输出a+b的和。
Sample Input
Sample Output
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define maxn 100 + 10
#define INF 10000000000
char a[20] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j'};
int main(){
char b[maxn];
char c[maxn];
int d[maxn];
int e[maxn];
while(~scanf("%s %s",b,c)){
int k1 = 0;
int k2 = 0;
memset(d,0,sizeof(d));
memset(e,0,sizeof(e));
for(int i = strlen(b) - 1;i>=0;i--){
if(b[i] > '9'){
d[k1++] = b[i] - 'a' + 10;
}
else d[k1++] = b[i] - '0';
}
for(int i = strlen(c) - 1;i>=0;i--){
if(c[i] > '9'){
e[k2++] = c[i] - 'a' + 10;
}
else e[k2++] = c[i] - '0';
}
int t = max(k1,k2);
int c;
for(int i = 0;i<t;i++){
d[i] += e[i];
if(d[i] >= 20) {
d[i+1] += d[i]/20;
d[i] %= 20;
c = i+1;
}
else{
c = i;
}
}
for(int i = c;i>=0;i--){
int m = d[i];
printf("%c",a[m]);
}
printf("\n");
}
return 0;
}