hdu 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): 5032    Accepted Submission(s): 2821


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 
将一个正整数各个位相加求出其根,如果这个数的和是个位数(0<=x<=9),那么他就是根;
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
举一个例子,24=2(十位)  4(个位),2+4 =6,所以6就是24的根;
 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 
例如:39= 3 (十位)+9(个位),3+9=12,12>9,12 = 1(十位)+2(个位),1+2=3,3就是39的根。
digital oot of 39.The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
 埃迪(Eddy')是一个简单的问题,给一个整数n,求出其根。

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).
 输入文件将包含多个正整数,输入n=0表明输入语句结束。n的范围是1<=n<10000.

Output
Output n^n's digital root on a separate line of the output.
 在一单独的行输出n^n的根

Sample Input
  
  
2 4 0
 

Sample Output
  
  
4 4
 

Author
eddy
题意:

输入一个N

求出求出N^N(n的n次方)

例如:

2^2 = 4

4^4 = 16*16=256=13=4

4^4 = (4^2)*(4^2)=16*4*4=(16%9)*4*4=7*4*4=28*4=(28%9)*4=1*4=4

递推公式

n= 9*k+MOD(余数)    {k属于R,R是自然数0,1,2........}

MOD = n-9*k;

假如n是一个四位数n=abcd

n = (a+b)%9*cd= e*cd=(e+c)%9*d=f*d=f*d%9=MOD


核心思想:

数论,同余
数论当中有个结论:“某数乘积的九余数=等于该数九余数的乘积”当余数为0时
,该数为九。

例如------》

12^12=3^3=9

12=1+2

       ||

12=12%9=3


import java.util.Scanner;

public class p1163 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			if (n == 0) {
				return;
			}
			int result = 1;
			for (int i = 0; i < n; i++) {
				result = result * n % 9;
			}
			if (result == 0) {
				System.out.println(9);
			} else {
				System.out.println(result);
			}
		}
	}

}






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值