例9003 火星上的加法

例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;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值