1. 程式人生 > 其它 >2021-02-17

2021-02-17

技術標籤:c語言

Description
In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time. This year they also invite people on Earth to join the contest. As the only delegate of Earth, you’re sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However, before you begin to program, you remember that the Martians use a 20-based number system as they usually have 20 fingers.

Input
You’re given several pairs of Martian numbers, each number on a line. Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, …, 19). The length of the given number is never greater than 100.

Output
For each pair of numbers, write the sum of the 2 numbers in a single line.

Sample Input
1234567890
abcdefghij
99999jjjjj
9999900001
Sample Output
bdfi02467j
iiiij00000

#include <stdio.h>
#include <string.h>
int judge(char c,int j)
{
	char cc;
	if(j>=0)
		if (c>='a'&&c<='j')//對c2字元進行判斷是0-9還是a-j
			cc=c-'a'+10;
		else
			cc=c-'0';
	else
		cc=0;
	return cc;
}
int main
() { char c1[110]={0},c2[110]={0},c3[110]={0}; char cc1,cc2,cc3; int x; int len1,len2,i,j,k,jinwei,len3; while(scanf("%s%s",c1,c2)!=EOF) { k=0;//c3計數器 jinwei=0; len1=strlen(c1)-1; len2=strlen(c2)-1; for (i=len1,j=len2;i>=0||j>=0;i--,j--)//只要有一個不為空就接著繼續 { cc1=judge(c1[i],i); cc2=judge(c2[j],j); x=cc1+cc2+c3[k];//x存字元與進位之和(沒有進行處理) c3[k]=x%20;//對20求餘 if (c3[k]>=0&&c3[k]<=9) c3[k]+='0'; else c3[k]+='a'-10; jinwei=x/20;//進位 k++; c3[k]=jinwei;//提前把進位存到c3 } len3=strlen(c3)-1; for (i=len3;i>=0;i--) printf("%c",c3[i]); printf("\n"); strcpy(c3,""); } return 0; }