杭电 1163 Eddy'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 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

Author

eddy

九余数定理:

一个数对九取余后的结果称为九余数。

一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加)

<span style="font-size:10px;">#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int n,m;
    while(cin>>n,n)
    {
        m=n;
        for(int i=2;i<=n;i++)
            m=(m*n)%9;
        if(m)
            cout<<m<<endl;
        else
            cout<<"9"<<endl;
    }
    return 0;
}
</span>

杭电 1163 Eddy's digital Roots

时间: 2024-10-01 02:59:05

杭电 1163 Eddy's digital Roots的相关文章

杭电ACM1163——Eddy&#39;s digital Roots

这题主要是找规律,第一次找出来!~~ 题目的意思是求一个数的digital root,这个所谓的digital root也就是一个数的各位数之和,如果这个数的两位数以上,重复再算digital root,直到这个数是一位数. 这一题就是求n^n的digital root. 规律如下: n个n相乘的结果假设为S,S的digital root 等于这n个数的digital root的相乘. 下面的是AC的代码,很简单的: #include <iostream> using namespace st

杭电 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

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

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)

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

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的树根. 程序中,实现一个计算整数数根的

杭电ACM1210——Eddy&#39;s 洗牌问题~~找规律。

例如:n = 3 一开始,序列为1 2 3 | 4 5 6 然后序列为        4 1 5 | 2 6 3 接着为                2 4 6 | 1 3 5 最后为                1 2 3 | 4 5 6 所以m = 3. 找出规律就可以解决了,只需要第一个数,也就是1的位置重新回到位置1,那整个数列就变回开始的序列了. 1的位置 i 小于等于n时,下一个位置是 2 * i :i 大于 n 时,下一个位置是 2 * (i - n)- 1. 下面是AC的代码