HDU1163 - Eddy's digital Roots

Posted oeong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1163 - Eddy's digital Roots相关的知识,希望对你有一定的参考价值。

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

九余数:一个数除于9所得到的余数,即模9得到值

求九余数:

  求出一个数的各位数字之和,如果是两位数以上,则再求各位数字之和,直到只有一位数时结束。

如果求出的和是9,则九余数为0;如果是其他数,则这个数为九余数。

 

思路:快速幂+九余数

先求出九余数,如果九余数为0,则ans=9;如果是其他数,则ans=九余数。

#include <iostream>
using namespace std;
//一个数对九取余,得到的数称之为九余数
int quickerpower(int a, int b, int mod) {
    int c = 1;
    while (b) {
        if (b & 1) c = c*a % mod;
        b >>= 1;
        a = a*a % mod;
    }
    return c % mod;
}
int main() {
    int n, jiuyu, ans;
    while (cin >> n, n) {
        jiuyu = quickerpower(n, n, 9);
        ans = jiuyu?jiuyu:9;
        //如果九余数为0,则ans=9;如果是其他数,则ans=九余数。
        cout << ans << endl;    
    }
    return 0;
} 

以上是关于HDU1163 - Eddy's digital Roots的主要内容,如果未能解决你的问题,请参考以下文章

HDU1163 - Eddy's digital Roots

Hdu-1163 Eddy's digital Roots(九余数定理)

HDU 1165 Eddy's research II

hdu 1162 Eddy's picture (prim)

HDU1161 Eddy&#39;s mistakes

题解报告:hdu 1162 Eddy's picture