这题是在顶贴找来做的,是20进制加法
Martian Addition
--------------------------------------------------------------------------------
Time limit: 1 Seconds Memory limit: 32768K
Total Submit: 5392 Accepted Submit: 1696
--------------------------------------------------------------------------------
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>
#define MAX 100
int main()
{
char num1[MAX],num2[MAX],temp[MAX];
int i,j;
while(1)
{ /*将两个字符串反转*/
if(scanf("%s",temp)==EOF)
break;
for(i=0;temp[i]!='\0';i++);
for(i-=1,j=0;i>=0;i--,j++)
{
if(temp[i]>='0'&&temp[i]<='9')
num1[j]=temp[i]-'0';
if(temp[i]>='a'&&temp[i]<='j')
num1[j]=temp[i]-'a'+10;
}
num1[j]='k';
if(scanf("%s",temp)==EOF)
break;
for(i=0;temp[i]!='\0';i++);
for(i-=1,j=0;i>=0;i--,j++)
{
if(temp[i]>='0'&&temp[i]<='9')
num2[j]=temp[i]-'0';
if(temp[i]>='a'&&temp[i]<='j')
num2[j]=temp[i]-'a'+10;
}
num2[j]='k';
for(i=0;num1[i]!='k'&&num2[i]!='k';i++)/*将对应位相加*/
{
if(num1[i]=='k')
{
for(j=i;num2[j]!='k';j++)
num1[j]=num2[j];
num1[j]='k';
break;
}
if(num2[i]=='k')
break;
num1[i]+=num2[i];
}
for(i=0;num1[i]!='k';i++)/*进位*/
if(num1[i]>=20)
{
if(num1[i+1]=='k')
{
num1[i+2]='k';
num1[i+1]=num1[i]/20;
num1[i]=num1[i]%20;
continue;
}
num1[i+1]+=num1[i]/20;
num1[i]=num1[i]%20;
}
for(i-=1;i>=0;i--)/*将字符串反向输出*/
{
if(num1[i]<=9)
printf("%d",num1[i]);
else
printf("%c",num1[i]-10+'a');
}
printf("\n");
}
return 0;
}
为什么这个过不了ACM啊,救命啊~~~~~~
大家帮帮忙啦,在此谢过~~~~
[此贴子已经被作者于2007-7-20 17:33:33编辑过]
----------------解决方案--------------------------------------------------------
无人答我,自己顶自己
----------------解决方案--------------------------------------------------------
顶你一个,今天有点晚了,得吃饭去了,有时间我看看,能解答得话一定贴出来。
----------------解决方案--------------------------------------------------------
麻烦啦
----------------解决方案--------------------------------------------------------
1234567890
abcdefghij
??
为什么我算出来的是
bdfi13578j呢
----------------解决方案--------------------------------------------------------
if(scanf("%s",temp)==EOF)
这句好像你要输入100个才能退出
----------------解决方案--------------------------------------------------------
第一次发那个是有已知错误的,现在改发了过不了ACM那个.
----------------解决方案--------------------------------------------------------
if(scanf("%s",temp)==EOF)
这句好像你要输入100个才能退出
额...没注意,不过ACM不会因这个报错吧
[此贴子已经被作者于2007-7-20 17:43:53编辑过]
----------------解决方案--------------------------------------------------------
你的算法好像过于复杂了,晚上我来写个看看,好像很好玩
我运行你的程序,一直没有条出去,还是在接收字符的模式
[此贴子已经被作者于2007-7-20 17:45:38编辑过]
----------------解决方案--------------------------------------------------------
你的算法好像过于复杂了,晚上我来写个看看,好像很好玩
我运行你的程序,一直没有条出去,还是在接收字符的模式
麻烦了,这个我也只是强制退出的.....
----------------解决方案--------------------------------------------------------