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);
}
}
}
}

HDOJ 1163 Eddy's digital Roots(九余数定理的应用)的更多相关文章

  1. HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论

    我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...

  2. HDU 1163 Eddy's digital Roots

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. hdu 1163 Eddy's digital Roots 【九余数定理】

    http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数定理: 如果一个数的各个数位上的数字之和能被9整除,那么这个数能被9整除:如果一个数各个数位上的数字 ...

  4. Hdu1163 Eddy's digitai Roots(九余数定理)

    题目大意: 给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下: 例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6: 因为 6 < 10,所 ...

  5. HDU 1163 Eddy's digital Roots(模)

    HDU 1163 题意简单,求n^n的(1)各数位的和,一旦和大于9,和再重复步骤(1),直到和小于10. //方法一:就是求模9的余数嘛! (228) leizh007 2012-03-26 21: ...

  6. Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  7. HDU-1163 Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. Eddy's digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU-1163Eddy's digital Roots,九余定理的另一种写法!

    下午做了NYOJ-424Eddy's digital Roots后才正式接触了九余定理,不过这题可不是用的九余定理做的.网上的博客千篇一律,所以本篇就不发篇幅过多介绍九余定理了: 但还是要知道什么是九 ...

随机推荐

  1. android recovery 主系统代码分析

    阅读完上一篇文章: http://blog.csdn.net/andyhuabing/article/details/9226569 我们已经清楚了如何进入正常模式和Recovery模式已有深刻理解了 ...

  2. C primer plus 读书笔记第四章

    本章的标题是字符串的格式化输入/输出,重点介绍输入和输出. 本章的第一段示例代码和上一张示例代码很相近,代码就不贴了,新出现的特性是使用了一个数组来存放字符串,C预处理命令和strlen()函数. 下 ...

  3. Asp.Net MVC 页面代码压缩筛选器-自定义删除无效内容

    Asp.Net MVC 页面代码压缩筛选器 首先定义以下筛选器,用于代码压缩. /*页面压缩 筛选器*/ public class WhiteSpaceFilter : Stream { privat ...

  4. C#_串口程序_二次打包_事件响应

    using System;using System.IO.Ports;using System.Windows.Forms; namespace Dll_Serial_Comm{    public ...

  5. java06switch

    public class SwitchTest { public static void main(String[] args) { /** * 如果第一名,参加麻省理工大学组织的1个月夏令营 * 如 ...

  6. OD: ASLR

    ASLR,Address Space Layout Randomization,通过加载程序的时候不再使用固定的基址,从而干扰 shellcode 定位的一种保护机制,包括映像随机化.堆栈随机化.PE ...

  7. 最近很火的携程Java 工程师的一道面向对象面试题

    最近这道面试题,传遍程序员各大小园地,本小白特摘抄整理与大家一起学习: 原题: package com.gxlee; public class Base { private String baseNa ...

  8. spring-data-redis问题总结

    如何判断一个字符串是否是合法的long类型? 参考文档如下: http://www.oschina.net/question/59889_45179 spring-data-redis http:// ...

  9. php捕获异常的处理

    try {            $result = *} catch (Exception $e) {            $result = $e; } 如果try里面报异常,$result = ...

  10. 使用CAEmitterLayer实现下雪效果

    效果图: 代码部分: #import "ViewController.h" @interface ViewController () @end @implementation Vi ...