Eddy's digital Roots


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

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

题目大意:给你一个正整数n,把n的各位上数字加起来,假设结果小于10,则所得结果为n的数字根,假设大于10,则再把上边所得结果各位上的数字加起来。如今给你一个数n,求n^n的数字根

思路:一看数据规模10000^10000,肯定要把n拆分掉。通过找规律发现,求n^n的数字根可转化为先求n的数

字根a,然后求a*n的原根,赋给a,接着依次求a*n,求n-1次,就得到了n^n的数字根。

比如:求5^5的数字

第一种方法:5^5 = 3125     3 + 1 + 2 + 5 = 11      1 + 1 = 2   终于结果是2

另外一种方法:5的数字根是5   5*5*5*5*5 =  25*5*5*5

相当于25的数字根7 *5*5*5  = 35*5*5 = 8*5*5 = 40*5 = 4*5 = 20 = 2

终于结果为2

对于另外一种方法能够用九余数定理,更加简单。

九余数定理:一个数N各位数字的和,对9取余等于这个数对9取余

<span style="font-family:Microsoft YaHei;font-size:18px;">//不使用九余数定理
#include<stdio.h> int main()
{
int n;
while(~scanf("%d", &n) && n)
{
int a = n;
int b = 0;
while(a > 0)
{
b += a % 10;
a /= 10;
}
if(b != 0)
a = b;
int x = a;
for(int i = 1; i < n; i++)
{
a *= x;
b = 0;
while(a > 0)
{
b += a % 10;
a /= 10;
}
if(b != 0)
a = b;
}
b = 0;
while(a > 0)
{
b += a % 10;
a /= 10;
}
if(b != 0)
a = b;
printf("%d\n",a);
}
return 0;
}
</span>
//使用九余数定理
#include <stdio.h> int main()
{
int n,a,sum,i;
while(scanf("%d",&n)&&n)
{
sum=1;
for(i=0;i<n;i++)
{
sum=sum*n%9;
}
if(sum==0)
printf("9\n");
else
printf("%d\n",sum); }
return 0;
}

HDU1163【九余数定理】【水题】的更多相关文章

  1. 2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题

    听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到10 ...

  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(九余数定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163 思路: 九余数定理:一个数对九取余的结果叫做九余数, 一个数的各个位数相加的得到的小于10的数也 ...

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

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

  5. HDU1013_Digital Roots【大数】【水题】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. HDU-1013九余数定理

    题目传送门:HDU1013 九余数定理 //题目描述:给定一个数,要求你求出它的每位上的数字之和,并且直到每位上的数字之和为个位时候输出它 //输入:一个整数 //输出:题目描述的结果 //算法分析: ...

  7. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

  8. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  9. ny525 一道水题

    一道水题时间限制:1000 ms  |  内存限制:65535 KB 难度:2描述 今天LZQ在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他吗? ...

随机推荐

  1. 上传多张图片用Session临时存储

    DataTable dtImages = new DataTable(); string filepath = FileUpload1.PostedFile.FileName; //检查是否有文件要上 ...

  2. DataSet中取值(转)

    1 DataSet.Table[0].Rows[ i ][ j ] 其中i 代表第 i 行数, j 代表第 j 列数 2 DataSet.Table[0].Rows[ i ].ItemArray[ j ...

  3. JavaScript的深度克隆

    1.JavaScript的五种基本数据类型: Number.String.Boolean.null.undefined. 2.typeof返回的六种数据类型: Number.String.Boolea ...

  4. js后缀判断

    var extension=fileName.substring(fileName.lastIndexOf('.')+1);

  5. C++ 字符串分割,分割到vector中

    #include <string> #include <vector> using std::string; using std::vector; int splitStrin ...

  6. BZOJ 2440: [中山市选2011]完全平方数( 二分答案 + 容斥原理 + 莫比乌斯函数 )

    先二分答案m,<=m的有m-∑(m/pi*pi)+∑(m/pi*pi*pj*pj)-……个符合题意的(容斥原理), 容斥系数就是莫比乌斯函数μ(预处理)... ----------------- ...

  7. preg_replace的用法

    <?php $str1 = "03/28/2015"; // 要替换成 2015-03-28 echo preg_replace("/([0-1][1-9])\/( ...

  8. yield return in C#

    Yield has two great uses It helps to provide custom iteration with out creating temp collections. It ...

  9. centos7 install magento

    http://mirrors.yun-idc.com/epel/7/x86_64/e/epel-release-7-2.noarch.rpmrpm --import /etc/pki/rpm-gpg/ ...

  10. POJ1797 Heavy Transportation 【Dijkstra】

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted:  ...