http://acm.pku.edu.cn/JudgeOnline/problem?id=1519一看这道题就笑了,呵呵,能这么简单? 这不是侮辱我的智商么? 五分钟之内完成代码如下:
1
#include"stdio.h"
2
void main()
3

{
4
int n;
5
int root;
6
while(scanf("%d",&n)&&n!=0)
7
{
8
root=100;
9
10
while(root>=10)
11
{
12
root=0;
13
while(n!=0)
14
{
15
root+=n%10;
16
n=n/10;
17
}
18
n=root;
19
20
21
}
22
23
24
printf("%d\n",root);
25
26
}
27
28
}
29
自己测试了几组简单的数据,都通过了,欣喜若狂,
激动地提交,结果WA了...
郁闷了半天,把int 改成long ,仍然WA...
仔细阅读题目,发现原来题目并没有限制输入数字的范围..汗,也就是说,可以输入好几十位的整数. 真是BT.
怎么办? 看来只能改用字符串接受数据了.
由于有内存限制,字符数组的大小不能太大.
又花了n久,终于ac
代码如下
1
#include"stdio.h"
2
char *ch=new char[30];
3
4
long int input()
5

{
6
long int i,sum=0;
7
scanf("%s",ch);
8
if(ch[0]=='0') return 0;
9
for(i=0;ch[i]!=0;i++)
10
sum+=ch[i]-'0';
11
12
return sum;
13
}
14
15
void main()
16

{
17
long n;
18
long root;
19
20
n=input();
21
while(n!=0)
22
{
23
root=32767;
24
25
while(root>=10)
26
{
27
root=0;
28
while(n!=0)
29
{
30
root+=n%10;
31
n=n/10;
32
}
33
n=root;
34
35
}
36
37
38
printf("%ld\n",root);
39
n=input();
40
41
}
42
43
}