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. 虎说:bootstrap源码解读(重置模块)

    ------<!--action-->------ 开场show:前不生“不犹豫”,后半生“不后悔”.今天又逃课,我不后悔 素材:推特公司的前端框架bootstrap(下称bt),解读源码 ...

  2. CSS3滤镜

    今天在办公室亲眼目睹了同事使用CSS3滤镜为一张漂亮的照片轮廓加上了阴影,瞬间亮瞎了我的的双眼,见笑了. 所以也迅速尝试使用CSS3滤镜让最新出炉的MUI LOGO也性感一把,试图来愉悦一下大家的双眼 ...

  3. iOS实战(零):开发社区、文档等资源

    社区 Apple官方资源 Xcode文档库: Window->Documentation and API Reference (可以在xcode的Preferences中下载最新的文档) iOS ...

  4. 深刻理解void,void*和sizeof关键字

    void的字面值是“无类型”,void*则是"无类型指针".void*可以指向任何类型的数据.void几乎只有"注释"和限制程序的作用,因为从来没有人会定义一个 ...

  5. dubbo架构演变之路

    背景 (#) 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时, ...

  6. Lucence.net索引技术 一

    1.建立索引 为了对文档进行索引,Lucene 提供了五个基础的类,他们分别是 Document, Field, IndexWriter, Analyzer, Directory.下面我们分别介绍一下 ...

  7. hadoop 主节点存储告警

    之前只他调整过dfs 的存储目录到最大配额的目录,其它没有处理(就是在默认的/ 目录下,而这个目录的存储配额只有50G) 运行一周的时间不到,集群开始告警,查看是目录/ 的存储占用超过了60% 再查看 ...

  8. 指针和引用区别 C++

    #include <iostream> using namespace std; int main(){ ; void cubeByPoint(int *);//指针传参声明 void c ...

  9. Noip2011提高组总结

    这套题思考的难度比较大,应该说是有四题基础题,一题比较复杂的搜索加模拟,还有一题需要深度思考一下.自己的代码漏洞还是很大,而且思考的时候会遗漏一些情况,这些错误都是致命的,去年Noip的惨败也证实了这 ...

  10. MySql学习笔记(转载)

    . 数值类型 -- a. 整型 ----------     类型            字节        范围(有符号位)     tinyint        1字节    - ~        ...