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. MYSQL—加写锁,加读锁,解锁

    链接地址:http://blog.sina.com.cn/s/blog_7fa2bcf50101j1lu.html 表级锁: 加写锁:          lock   tables   table_n ...

  2. ajaxFileUpload用法

    首先要引入两个js <script type="text/javascript" src="/static/js/jquery.js"></s ...

  3. ASP.NET MVC进阶之路:依赖注入(Di)和Ninject

    0X1 什么是依赖注入 依赖注入(Dependency Injection),是这样一个过程:某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点.在程序运行过程中,客户 ...

  4. Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)

    最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...

  5. QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NETQT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET

    QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET QT在ui文件上建立信号操机制会不会对后期维护产生影响 分类: 学习心得 2 ...

  6. 配置hibernate数据库连接

    第一步:右键项目->MyEclipse->添加Hibernate组件,指定数据库连接配置如下(src/hibernate.cfg.xml) MySQL对连接的有效期是28800s,一个连接 ...

  7. Why is celsius = 5 * (fahr - 32) / 9 ?

    Go to my personal blog There is a program to print Fahrenheit-Celsius table as below. #include <s ...

  8. Spring MVC遭遇checkbox的问题解决方式

    Spring MVC遭遇checkbox的问题是:当checkbox全不选时候,则该checkbox域的变量为null,不能动态绑定到spring的controller方法的入參上,并抛出异常. 解决 ...

  9. android 中文 api (72) —— BluetoothSocket[蓝牙]

    前言 本章内容是   android.bluetooth.BluetoothSocket,为Android蓝牙部分的章节翻译.蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过 ...

  10. ThinkPHP - 组织分类结构