hdu2100: Lovekey

hdu2100: http://acm.hdu.edu.cn/showproblem.php?pid=2100
解法:高精度加法-26进制,A~Z分别代表0~26,求法和10进制一样 code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
char a[300],b[300],c[300],d[300];
int max(int x,int y)
{
    if(x>y)
        return x;
    else
        return y;
}
int main()
{
    int x,y,s;
    while(scanf("%s%s",a,b)!=EOF)
    {
        x=strlen(a);y=strlen(b);
        for(int i=x-1,j=0;i>=0;i--,j++)
            c[j]=a[i];
        c[x]='\0';
        for(int i=y-1,j=0;i>=0;i--,j++)
            d[j]=b[i];
        d[y]='\0';
        if(x<y)
        {
            for(int i=x;i<y;i++)
                c[i]='A';
            c[y]='\0';
        }
        else
        {
            for(int i=y;i<x;i++)
                d[i]='A';
            d[x]='\0';
        }
        x=max(x,y);
        s=0;
        for(int i=0;i<x;i++)
        {
            c[i]=c[i]+d[i]+s-'A';
            if(c[i]>'Z')
            {
                s=1;
                c[i]=c[i]-26;
            }
            else
                s=0;
        }
        if(s==1)
        {
            c[x++]='B';
        }
        for(int i=x-1;i>=0;i--)
        {
            if(c[i]=='A')
                x--;
            else
                break;
        }
        for(int i=x-1;i>=0;i--)
            printf("%c",c[i]);
        printf("\n");
    }
}
/*input:
AAAADH BCE 
DRW UHD 
D AAAAA
output:
BFL 
XYZ 
D*/
 

posted on 2012-07-25 20:59  acmer-jun  阅读(300)  评论(0编辑  收藏  举报

导航