此题本来以为很水.多次WA不过.先贴一遍最初的版本:
#include <stdio.h>
#include <iomap.h>
/*
Problem 1013:Digital Roots
Version :2012-8-11
Author:Unibrighter
Tips:Use the idea of Recursion
*/
int sumDig(int n)
{
if(n<10)
return n;
else
{
int sum=0;
while(n)//if n can still carry a dig
{
sum+=(n%10);
n/=10;//Remeber if the result <0,then we get n as 0(e.g. n=8,n/=10;then n=0)
}
return sumDig(sum);
}
}
int main()
{
freopen(".\\1013in.txt","r",stdin);// Redirection of the Input Stream and get the Test Statics
int num,res;
while((scanf("%d",&num)!=EOF)&&num)
{
printf("%d\n",sumDig(num));
}
return 0;
}
一直WA,在Discusion版块里面看了帖子,跟前人有一样的感受,特么竟然不说说数据长度和n的大小!谁知道你要的是大整数?!恶心!
"恶心,恶心哪!我都关着灯!"
接着是AC的代码:
#include <stdio.h>
/*
Author:Unibrighter
Version:2012-8-13
Problem ID:HDU 1013 Digital Root
Note:Get some idea from Jopix Blog(陈侠 暨南大学)
http://blog.sina.com.cn/s/blog_6ec19c780100vcn2.html
What really made me embarrased is that I made the same mistake as he did.(╯﹏╰)b
*/
int main()
{
char tmp;
int i,sum;
//freopen(".\\1013in.txt","r",stdin);
while(1)
{
sum=0;
for(i=0;i<1000;i++)
{
scanf("%c",&tmp);
if(tmp=='\n')
break;
sum+=(tmp-'0');
}
if(sum==0)
break;
printf("%d\n",sum%9==0?9:sum%9);
}
return 0;
}
开始也是一直AC,后才参考了 Jofix 的代码和Tips.这才AC,一样没有严谨的考虑到9取模为0.他大爷的.还是自己的底子不够厚实么...
多看看别人的Blog.差距啊啊.还有Discuss版块也是学习的好地方!