二十进制的两数相加

喜闻乐见的a+b

Description

喜闻乐见的a+b来了,当然二进制,八进制,十进制什么的估计你们也懒得写了,来个20进制的a+b

Input

给出多组a和b,每个数字一行

数字由0~9和小写字母a~j(10~19)

给出的长度不超过100

Output

每组数据输出a+b的和。

Sample Input

1234567890 abcdefghij 99999jjjjj 9999900001

Sample Output

bdfi02467j
iiiij00000
 
思路:将两数当作字符串输入,反转为数字数组,两两相加,本位除20取整,向前进位除20取余。最后将结果数组反转输出即可。注意字符转为数字的做法,如果字符本身是数字,减‘0’即可,如果是字母,减‘a’ + 10。

代码如下:

#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;
}

posted @ 2018-07-22 12:24  半忧夏  阅读(284)  评论(0编辑  收藏  举报