HDOJ 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 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

题意:输入一个数n,求n的n次方的数根。
数根:即某数字的每一位上的数字之和,如果和大于等于10,重复每一位上的数字之和,直到每一位上的数字之和是个位数。则这个个位数就是这个数字的数根。

九余数定理:
一个数对九取余,得到的数称之为九余数;
一个数的九余数 等于 它的各个数位上的数之和的九余数!
例:
5^5=3125
3+1+2+5=11
1+1=2(digital Roots)
3125%9=2;
一个数对9取余就等于它的个位数字之和对9取余就等于数根对9取余。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0){
                return ;
            }
            int m = 1;
            for(int i=0;i<n;i++){
                m=(m*n)%9;
            }
            if(m==0){
                System.out.println(9);
            }else{
                System.out.println(m);
            }
        }
    }
}
时间: 2024-10-23 16:31:16

HDOJ 1163 Eddy&#39;s digital Roots(九余数定理的应用)的相关文章

HDOJ 1164 Eddy&amp;#39;s research I(拆分成素数因子)

Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to h

HDOJ 1210 Eddy&amp;#39;s 洗牌问题

Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如果他有2N张牌,编号为1,2,3..n,n+1,..2n.这也是最初的牌的顺序.通过一次洗牌可以把牌的序列变为n+1,1,n+2,2,n+3,3,n+4,4..2n,n.那么可以证明,对于任意自然数N,都可以在经过M次洗牌后第一次重新得到初始的顺序.编程对于小于100000的自然数N,求出M的值. Input 每行一个整数N Output 输出与之对应的M Sa

HDOJ 2200 Eddy&amp;#39;s AC难题(数学组合概率题)

Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目的数量摘录下来,然后从中选择一部分人(或者全部)按照ac的数量分成两组进行比较,他想使第一组中的最小ac数大于第二组中的最大ac数,但是这样的情况会有很多,聪明的你知道这样的情况有多少种吗? 特别说明:为了问题的简化,我们这里假设摘录下的人数为n人,而且每个人ac的数量不会相等,最后结果在64位整数

HDOJ 1013题Digital Roots 大数,9余数定理

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 a

算法题:HDU 1013 Digital Roots

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 pr

[Qt教程] 第39篇 网络(九)进程和线程

[Qt教程] 第39篇 网络(九)进程和线程 楼主  发表于 2013-8-29 15:48:56 | 查看: 415| 回复: 0 进程和线程 版权声明 该文章原创于作者yafeilinux,转载请注明出处! 导语 在前面的几节内容中讲解了Qt网络编程的一些基本内容,这一节来看一下在Qt中进程和线程的基本应用. 环境:Windows Xp + Qt 4.8.5+Qt Creator2.8.0 目录 一.进程 二.线程 正文 一.进程     在设计一个应用程序时,有时不希望将一个不太相关的功能

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ 1013 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 or more digits, those digits a

HDU 1006 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 or more digits, those digits a