hdoj 1013Digital Roots
/*Digital Roots
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.
Input
The input file will contain a list of positive integers, one per line.
The end of the input will be indicated by an integer value of zero.
Output
For each integer in the input, output its digital root on a separate line of the output.
Sample Input
24
39
0
Sample Output
6
3*/
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
int main()
{
int sum;
char a[1000],n[1000];
while(gets(n))
{
if (strcmp(n,"0")==0)
{
break;
}
else
{
int sum=0;
for (int j=0;j<strlen(n);j++)
{
sum+=(n[j]-48);
}
while (sum>=10)
{
sprintf(a,"%d",sum);//把格式化的数据写入某个字符串缓冲区。 意思是把sum转化为字符串a。 sum=0;
for (int i=0;i<strlen(a);i++)
{
sum+=(a[i]-48);
}
}
printf("%d\n",sum);
}
}
}</span>
这种方法不easy想到。
<span style="font-size:18px;">//9余数定理
#include<stdio.h>
#include<string.h>
char a[10010];
int main()
{
int n;
while(~scanf("%s",a),strcmp(a,"0"))
{
int sum=0;
int len=strlen(a);
for(int i=0;i<len;++i)
sum+=a[i]-'0';
printf("%d\n",(sum-1)%9+1);//为什么减一后再加一。是为了避免18这些数字。
}
return 0;
}
</span>
hdoj 1013Digital Roots的更多相关文章
- HDU——1013Digital Roots(九余数定理)
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1163 Eddy's digital Roots(九余数定理的应用)
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 1013题Digital Roots 大数,9余数定理
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论
我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
随机推荐
- http格式(graph)
http请求格式 http请求头 字段 http响应 http响应头字段
- Android实现能够揉动的图片
public class Demo01 extends Activity{ private Bitmap bitmap = null; @Override protected void onCreat ...
- hdu2476String painter (区间DP)
Problem Description There are two strings A and B with equal length. Both strings are made up of low ...
- Map (就一个json.jar)
public static void main(String[] args) { List<Map<Integer, String>> m = new ArrayList< ...
- python面向对象与结构成员之间的关系
1面向对象结构分析:----面向对象整体大致分为两块区域:-------第一部分:静态字段(静态变量)部分-------第二部分:方法部分--每个区块可以分为多个小部分 class A: countr ...
- POJ 2446 匈牙利算法
题意: 思路: 二分图匹配... // by SiriusRen #include <cmath> #include <cstdio> #include <cstring ...
- java8 Lambad表达式自己的例子
service层方法 public <E> E outer(Function<Session, E> function) { return dao.outer(function ...
- paratest
class Program { static void Main(string[] args) { long result = 0; Stopwatch Watch = new Stopwatch() ...
- BZOJ3529: [Sdoi2014]数表 莫比乌斯反演_树状数组
Code: #include <cstdio> #include <algorithm> #include <cstring> #define ll long lo ...
- Java之Object类
0 引言 Object类是类层次结构的根,Java中所有的类从根本上都继承自这个类.Object类是Java中唯一没有父类的类. 其他所有的类,包括标准容器类,比如数组,都继承了Object类中的方法 ...