hdu1163Eddy's digital Roots

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163


九余数定理:

一个数的每位数字之和如果不小于10,那么就继续相加,直到小于10,相当于这个数对9取余。


代码:


#include <cstdio>

int main()

{
    int n;
    while(~scanf("%d",&n),n)
    {
        int t = 1;
        for(int i = 1;i <= n;++i)
            t = t * n % 9;
        if(t == 0)
            printf("9\n");
        else
            printf("%d\n",t);
    }
    return 0;
}

你可能感兴趣的:(ACM)