HDU1163【九余数定理】【水题】
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【九余数定理】【水题】的更多相关文章
- 2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题
听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到10 ...
- HDU-1163 Eddy's digital Roots(九余数定理)
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu-1163(九余数定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163 思路: 九余数定理:一个数对九取余的结果叫做九余数, 一个数的各个位数相加的得到的小于10的数也 ...
- Hdu1163 Eddy's digitai Roots(九余数定理)
题目大意: 给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下: 例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6: 因为 6 < 10,所 ...
- HDU1013_Digital Roots【大数】【水题】
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU-1013九余数定理
题目传送门:HDU1013 九余数定理 //题目描述:给定一个数,要求你求出它的每位上的数字之和,并且直到每位上的数字之和为个位时候输出它 //输入:一个整数 //输出:题目描述的结果 //算法分析: ...
- HDU1013,1163 ,2035九余数定理 快速幂取模
1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...
- POJ 水题(刷题)进阶
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- ny525 一道水题
一道水题时间限制:1000 ms | 内存限制:65535 KB 难度:2描述 今天LZQ在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他吗? ...
随机推荐
- DevExpress ASP.NET 使用经验谈(5)-通过ASPxGridView实现CRUD操作
这节,我们将通过使用DevExpress的ASPxGridView控件,实现对数据的CRUD操作. 首先,我们在解决方案中,添加一个网站: 图一 添加新网站 图二 添加DevExpress.Data. ...
- asp.net发布网站(转)
1. 在Web项目中点击发布网站,如图1所示 图1 2. 选择要发布的路径——>“确定”,如果项目显示发布成功就可以了.如图2所示 图2 3. 打 ...
- iOS 百度地图大头针使用
百度地图使用第五讲:大头针使用(地图标注)http://bbs.yusian.com/thread-8384-1-1.html(出处: 小龙虾IT笔记)
- tomcat 部署web项目异常
项目部署到Tomcat报这样的异常:validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending cla ...
- ffmpeg 命令详解
常用视频命令 //转码(并添加黑边) ffmpeg -ss start_time -i input.mp4 -t total_duration -vcodec copy/h264/... -acode ...
- 5.PHP 教程_PHP echo/print
PHP echo 和 print 语句 echo和print区别: echo-可以输出一个或多个字符串 print-只允许输出一个字符串,返回值总为1 提示:echo输出的速度比print快,echo ...
- codeforces 609F. Frogs and mosquitoes 二分+线段树
题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...
- linux和windows的文件互传
Linux →→→Windows 1.使用secureCRT:下载文件只需在shell终端仿真器中输入命令"sz 文件名",即可利用Zmodem将文件下载到本地某目录下. 2. s ...
- 斯坦福 IOS讲义 课件总结 二
1,OC里面传参数个数不同,可以是两个完全不同的方法.如 - (void)addCard:(Card *)card atTop:(BOOL)atTop; - (void)addCard:(Card * ...
- S50卡
产品名称:Mifare 1K(S50)卡 芯片类型:Philips Mifare 1 IC S50 存储容量:8Kbit,16个分区,每分区两组密码 工作频率:13.56 MHz 通讯速率:106KB ...