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

麻烦啦


2007-07-20 16:53
星星鱼虾蟹
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: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用酒肉弥勒佛在2007-7-20 17:44:04的发言:
你的算法好像过于复杂了,晚上我来写个看看,好像很好玩

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

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


2007-07-20 17:48
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用卧龙孔明在2007-7-21 8:16:04的发言:
以前做过这个题,只要用数组读入后以二十进制相加就可以了,其实就是改一下高精度加法中的进位和转换

我看过了你写的那个了,也过不了ACM


2007-07-21 09:10
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用酒肉弥勒佛在2007-7-20 19:13:03的发言:

#include <stdio.h>
#include <curses.h>
#define MAX 100
#define MLEN 19
int old[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
char new[20]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j'};


int main()
{
char put[MAX];
char ch;
int i,j,count=0,count1=0;
int pnew1[MAX],pnew2[MAX];
char result[MAX+1];
int flag=0,sum=0;

printf("please input frist number:");
scanf("%s",&put);
for(i=0;i<MAX;i++)
{
if(put[i]=='\0')
break;

for(j=0;j<=MLEN;j++)
{
if(put[i]==new[j])
{
pnew1[i]=old[j];
break;
}
}

count++;
}

printf("please input second number:");
scanf("%s",&put);
for(i=0;i<MAX;i++)
{
if(put[i]=='\0')
break;

for(j=0;j<=MLEN;j++)
{
if(put[i]==new[j])
{
pnew2[i]=old[j];
break;
}
}

count1++;
}

if(count!= count1)
return 0;

for(i=count-1;i>=0;i--)
{
sum=pnew1[i]+pnew2[i]+flag;
result[i+1]=new[sum%(MLEN+1)];
flag=sum/(MLEN+1);
printf("result:%c\n",result[i+1]);

}

result[0]=new[flag];

for(i=0;i<count+1;i++)
printf("%c",result[i]);
printf("\n");

}

过不了.....
如果你们要测试,到这里http://acm.zju.edu.cn/submit.php?pid=1205


2007-07-21 09:15
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用卧龙孔明在2007-7-21 9:34:58的发言:
是不是那个进行测试的有问题?

好有嫌疑....


2007-07-21 10:11
星星鱼虾蟹
Rank: 1
等 级:新手上路
帖 子:191
专家分:0
注 册:2007-6-2
收藏
得分:0 
以下是引用酒肉弥勒佛在2007-7-21 15:12:26的发言:
我测试过了,我写的没有问题啊,你的测试网站没有用户名和密码

可以注册的在这里http://acm.zju.edu.cn/register.php

[此贴子已经被作者于2007-7-21 16:23:33编辑过]


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



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

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