Eddy's digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4793    Accepted Submission(s): 2672

Problem Description

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

The Eddy‘s easy problem is that : give you the n,want you to find the n^n‘s digital Roots.

Input

The input file will contain a list of positive integers n, one per line. The end of the input will be indicated by an integer value of zero. Notice:For each integer in the input n(n<10000).

Output

Output n^n‘s digital root on a separate line of the output.

Sample Input

2
4
0

Sample Output

4 4

思路:1.九余数定理  一个数的n的树根=(n-1)%9+1

2.N个整数的树根的乘机总值=每个项元素的树根的乘积

3.分步求余

#include<cstdio>
#include<cmath>
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        int m=n;
        int i;
        int ans=1;
        m=(m-1)%9+1;//先把底数m求树根,根据  N个整数的乘积带的树根=每项元素的树根的乘积
        for(i=1;i<=n;i++)
        ans=(ans*m-1)%9+1;//分步求余
        printf("%d\n",ans);
    }
}

Eddy's digital Roots

时间: 2024-08-24 22:23:25

Eddy's digital Roots的相关文章

HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5783    Accepted Submission(s): 3180 Problem Description The digital root of a positive integer is found by summing the digit

HDU1163 Eddy&#39;s digital Roots【九余数定理】

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4436    Accepted Submission(s): 2505 Problem Description The digital root of a positive integer is found by summing the digi

Hdu-1163 Eddy&#39;s digital Roots(九余数定理)

题意:对于一个数,只要它不是单个数,就把它各位的数相加,直到成为单个数. 九余数定理 一个数对九取余后的结果称为九余数. 一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加) 因为数据挺大的,不能暴力.所以可以边乘边取余.很像快速幂. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int main(){ 6 int n; 7 while

HDU1163 Eddy&#39;s digital Roots

问题链接:HDU1013 Digital Roots.入门练习题,用C语言编写程序. 这个问题是对于输入的n,计算n^n的数根. 先看一下以下式子: 因为:(10*a+b)*(10*a+b)=100*a*a+10*2*a*b+b*b 所以右边式子的数根(中间结果,也是左边式子的数根)为:a*a+2*a*b+b*b=(a+b)*(a+b) 故:对于两位数n,n*n的数根=n的树根×n的树根. 同理可以推出,对于任意位数的n,也满足:n*n的数根=n的树根×n的树根. 程序中,实现一个计算整数数根的

HDOJ 1163 Eddy&#39;s digital Roots(简单数论)

[思路]:http://blog.csdn.net/iamskying/article/details/4738838 求解思路: 现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得 ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b 根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b)

Eddy&#39;s digital Roots(九余数定理)

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5113    Accepted Submission(s): 2851 Problem Description The digital root of a positive integer is found by summing the digit

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

hdu 1163 Eddy&#39;s digital Roots(九余数定理)

hdu 1163 Eddy's digital Roots Problem Description The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two

杭电 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4275    Accepted Submission(s): 2404 Problem Description The digital root of a positive integer is found by summing the digi