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在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他吗? ...
随机推荐
- zoj 1081 (改进的弧长算法)(转)
看到网上除了射线法,很长一段代码之外,看到了一个很简单的算法解决这个问题,特意转了过来 /* 这个算法是源自<计算机图形学基础教程>(孙家广,清华大学出版社),在该书 的48-49页,名字 ...
- SQL2008中Merge的用法(轉載)
在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...
- Sql日期时间格式转换 备用
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- PHP学习笔记12-上传文件
上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...
- COM编程之IUnknown接口
COM组件其实是一种特殊的类,遵循一个统一的标准,使到各个软件都可以通过某种方法访问这个类的函数和方法,也就可以做到组件通用. com就是统一的标准--通过接口来调用com组件.接口是你的com组件能 ...
- 参照openRTSP写的一个RTSP client 加了一些注解
#include "liveMedia.hh" #include "BasicUsageEnvironment.hh" #include "Gro ...
- APUE学习之------------信号
在学习一个东西的时候我总是喜欢去问这样做的理由是什么?也喜欢去究竟他的历史.从中你可以发现所有的设计都在不断改进出来的,从来就没有一个设计是一开始就是完美的.好比是人,之初,性也许是善的,如果我们不通 ...
- ubuntu安装hadoop 若干问题的解决
问题1:安装openssh-server失败 原因: 下列软件包有未满足的依赖关系: openssh-server : 依赖: openssh-client (= 1:5.9p1-5ubuntu1) ...
- 【转】emulator: ERROR: Could not load OpenGLES emulation library: lib64OpenglRender.so
[转]emulator: ERROR: Could not load OpenGLES emulation library: lib64OpenglRender.so ./emulator64-arm ...
- [译]SSRS 报表版本控制
问题 如今商务智能应用广泛,对我们的商业愈加重要. 对新报表和的各种需求不断攀升. 自 SQL Server 2008 R2的 Reporting Services (SSRS) 开始,微软视图为减轻 ...