http://acm.hdu.edu.cn/showproblem.php?pid=1013這個題模擬也可以AC,剛開始我也是模擬AC的。不過看了百度看了大牛的博客,感謝大牛,知道了還有數(shù)論這回事。
n=0 1 2 3 4 5 6 7 8 9 10 11 12 13 ......... 100 101 102 103 ....
roots=0 1 2 3 4 5 6 7 8 9 1 2 3 4 .......1 2 3 4....
原來是以1.....9為循環(huán)節(jié)的。想想也是,每次增加1,那么層層迭代下來,最終當(dāng)ans<10的時候也是每次增加了1。如此,可以歸納出
roots=(n-1)%9+1
注意輸入的數(shù)字很大需要字符串讀入,求和得n:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int i,n,tmp;
char a[1003];
while (scanf("%s",&a)&&a[0]!='0')
{
n=0;
for (i=0;i<strlen(a); i++)
{
n+=a[i]-48;
}
printf("%d\n",(n-1)%9+1);
}
return 0;
}
啊,要多研究研究啊,本能只是止步于表面東西,大牛,Orz。。。