码迷,mamicode.com
首页 > 其他好文 > 详细

XDOJ_1003_高精度+进制

时间:2016-10-12 06:45:39      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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_高精度+进制

标签:

原文地址:http://www.cnblogs.com/zhurb/p/5951326.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!