| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1630 人关注过本帖
标题:关于ACM的题目,不会啊~~
只看楼主 加入收藏
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
 问题点数:0 回复次数:21 
关于ACM的题目,不会啊~~

这题是在顶贴找来做的,是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编辑过]

搜索更多相关主题的帖子: ACM limit Submit mathematics 
2007-07-20 16:19
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 

无人答我,自己顶自己


2007-07-20 16:43
TNTSky
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-7-20
收藏
得分:0 
顶你一个,今天有点晚了,得吃饭去了,有时间我看看,能解答得话一定贴出来。
2007-07-20 16:49
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 

麻烦啦


2007-07-20 16:53
酒肉弥勒佛
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:399
专家分:0
注 册:2006-6-6
收藏
得分:0 
1234567890
abcdefghij
??

为什么我算出来的是
bdfi13578j呢

编程是为了提高效率,好的程序是因为他的高效;在编程的道路上,永远追逐高效的算法
2007-07-20 17:17
酒肉弥勒佛
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:399
专家分:0
注 册:2006-6-6
收藏
得分:0 
if(scanf("%s",temp)==EOF)

这句好像你要输入100个才能退出

编程是为了提高效率,好的程序是因为他的高效;在编程的道路上,永远追逐高效的算法
2007-07-20 17:39
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
第一次发那个是有已知错误的,现在改发了过不了ACM那个.

2007-07-20 17:40
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用酒肉弥勒佛在2007-7-20 17:39:26的发言:
if(scanf("%s",temp)==EOF)

这句好像你要输入100个才能退出

额...没注意,不过ACM不会因这个报错吧

[此贴子已经被作者于2007-7-20 17:43:53编辑过]


2007-07-20 17:42
酒肉弥勒佛
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:399
专家分:0
注 册:2006-6-6
收藏
得分:0 
你的算法好像过于复杂了,晚上我来写个看看,好像很好玩

我运行你的程序,一直没有条出去,还是在接收字符的模式

[此贴子已经被作者于2007-7-20 17:45:38编辑过]


编程是为了提高效率,好的程序是因为他的高效;在编程的道路上,永远追逐高效的算法
2007-07-20 17:44
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用酒肉弥勒佛在2007-7-20 17:44:04的发言:
你的算法好像过于复杂了,晚上我来写个看看,好像很好玩

我运行你的程序,一直没有条出去,还是在接收字符的模式

麻烦了,这个我也只是强制退出的.....


2007-07-20 17:48
快速回复:关于ACM的题目,不会啊~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019247 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved