hdu 1163 Eddy's digital Roots
//0MS 228K 633 B C++ //多个数乘积的数根,与分别计算出数根再相乘计算数根答案一样 //一开始二了很久,一直多乘一次,杯具 O(logn) #include<stdio.h> int root(int a) { while(a%10!=a){ int ans=0; while(a){ ans+=a%10; a/=10; } a=ans; } return a; } int main(void) { int n; while(scanf("%d",&n),n) { int m=n; int ans=1; while(m){ if(m%2) ans=root(n*ans); //ans=root(n*ans); 硬伤... n=root(n*n); m/=2; } printf("%d\n",ans); } return 0; }