HDU1163 - Eddy's digital Roots

时间:2023-03-09 04:26:37
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 = ;
while (b) {
if (b & ) c = c*a % mod;
b >>= ;
a = a*a % mod;
}
return c;
}
int main() {
int n, jiuyu, ans;
while (cin >> n, n) {
jiuyu = quickerpower(n, n, );
ans = jiuyu?jiuyu:;
//如果九余数为0,则ans=9;如果是其他数,则ans=九余数。
cout << ans << endl;
}
return ;
}