题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2100
AAAADH BCE DRW UHD D AAAAA
BFL XYZ D
PS:
直接模拟26进制相加就OK啦!
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn = 547; int main() { char s1[maxn], s2[maxn]; int a[maxn], b[maxn], c[maxn], d[maxn]; while(~scanf("%s %s",s1,s2)) { memset(a, 0,sizeof(a)); memset(b, 0,sizeof(b)); memset(c, 0,sizeof(c)); memset(d, 0,sizeof(d)); int len1 = strlen(s1); int len2 = strlen(s2); int len = max(len1, len2); int cnt = 0; for(int i = len1-1; i >= 0; i--) { a[cnt++] = s1[i] - 'A'; } cnt = 0; for(int i = len2-1; i >= 0; i--) { b[cnt++] = s2[i] - 'A'; } for(int i = 0; i < len; i++) { c[i] += a[i]+b[i]; if(c[i] >= 26) { c[i]-=26; c[i+1]++; } } cnt = 0; int flag = 0; for(int i = len; i >= 0; i--)//前导零 { if(c[i] == 0 && flag == 0) { continue; } else { flag = 1; d[cnt++] = c[i]; } } if(cnt == 0) { printf("A\n"); continue; } for(int i = 0; i < cnt; i++) { printf("%c",d[i]+'A'); } printf("\n"); } return 0; } /* A A B B C C */
原文地址:http://blog.csdn.net/u012860063/article/details/43760143