例9003 火星上的加法
Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others)
Total Submissions: 49 Accepted Submissions: 38
Problem Description
22世纪,科学家们发现火星上的居民智能很高。火星人热爱算术。每年他们都要举行火星算术比赛简称ACM。比赛的任务是计算两个100位数的和,用时最少者获胜。今年他们也邀请地球人参加此项赛事。
作为地球人的唯一代表,你被送到火星上向他们展示人类的智慧。幸运的是,你带上了笔记本电脑,这将帮助你快速完成这项任务。现在唯一的问题就是编程计算两个100位数的和。需要注意的是,火星人用的是20进制,因为他们有20个手指。
Input
给定你多对火星数,每个数占一行。火星数包括09,以及小写字母aj,小写字母分别代表十进制中的10~19。每个数位数不超过100位。
Output
输出每对数的和,每个和占一行。
Sample Input
1234567890
abcdefghij
99999jjjjj
9999900001
Sample Output
bdfi02467j
iiiij00000
#include <stdio.h>
#include <string.h>
int main() {
char num1[110],num2[110];
char res[110];
int temp;
int len1,len2;
while(~scanf("%s%s",num1,num2)) {
len1 = strlen(num1)-1;
len2 = strlen(num2)-1;
if(len1==len2) {
int j = 0;
for(int i = len1;i>=0;i--) {
temp = (num1[i]<'a' ? num1[i]-'0' : (num1[i]-'a'+10)) +
(num2[i]<'a' ? num2[i]-'0' : (num2[i]-'a'+10));
if(temp >= 20 ) {
num1[i-1]++;
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
else {
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
}
if((num1[0]<'a' ? num1[0]-'0' : (num1[0]-'a'+10))>=20) res[j++] = '1';
res[j] = '\0';
for(int i = strlen(res)-1;i>=0;i--) {
printf("%c",res[i]);
}
printf("\n");
}
if(len1>len2) {
int j = 0;
for(;len2>=0;len1--,len2--) {
temp = (num1[len1]<'a' ? num1[len1]-'0' : (num1[len1]-'a'+10)) +
(num2[len2]<'a' ? num2[len2]-'0' : (num2[len2]-'a'+10));
if(temp >= 20 ) {
num1[len1-1]++;
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
else {
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
}
for(;len1>0;len1--) {
temp = num1[len1]<'a' ? num1[len1]-'0' : (num1[len1]-'a'+10);
if(temp >= 20 ) {
num1[len1-1]++;
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
else {
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
}
if((num1[0]<'a' ? num1[0]-'0' : (num1[0]-'a'+10))>=20) res[j++] = '1';
res[j] = '\0';
for(int i = strlen(res)-1;i>=0;i--) {
printf("%c",res[i]);
}
printf("\n");
}
if(len1<len2) {
int j = 0;
for(;len1>=0;len1--,len2--) {
temp = (num1[len1]<'a' ? num1[len1]-'0' : (num1[len1]-'a'+10)) +
(num2[len2]<'a' ? num2[len2]-'0' : (num2[len2]-'a'+10));
if(temp >= 20 ) {
num1[len2-1]++;
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
else {
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
}
for(;len2>0;len2--) {
temp = num2[len2]<'a' ? num2[len2]-'0' : (num2[len2]-'a'+10);
if(temp >= 20 ) {
num2[len2-1]++;
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
else {
res[j++] = temp%20>9 ? temp%20+'a'-10 : temp%20+'0';
}
}
if((num2[0]<'a' ? num2[0]-'0' : (num2[0]-'a'+10))>=20) res[j++] = '1' ;
res[j] = '\0';
for(int i = strlen(res)-1;i>=0;i--) {
printf("%c",res[i]);
}
printf("\n");
}
}
return 0;
}