/*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的更多相关文章

  1. HDU——1013Digital Roots(九余数定理)

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  3. HDOJ 1013题Digital Roots 大数,9余数定理

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  4. HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论

    我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  9. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

随机推荐

  1. ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes

    http://www.codeproject.com/Articles/775220/ASP-Net-MVC-What-are-the-uses-of-Display-DisplayNa?utm_so ...

  2. inconsistent line endings 解决方法

     I'm using Unity 3D in combination with Visual Studio 2008 on a Windows 7 64 bit system. When savi ...

  3. HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)

    Boring counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. [jzoj 6080] [GDOI2019模拟2019.3.23] IOer 解题报告 (数学构造)

    题目链接: https://jzoj.net/senior/#main/show/6080 题目: 题意: 给定$n,m,u,v$ 设$t_i=ui+v$ 求$\sum_{k_1+k_2+...+k_ ...

  5. Python笔记(八)

    #-*-coding:utf-8-*- # Python内置函数 print abs(-45) # 绝对值函数 print divmod(7,2) # 返回一个包含商和余数的元组 # input(&q ...

  6. BZOJ 3052 树上带修莫队

    思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...

  7. 数据库 The Network Adapter could not establish the connection解决方案

    连接数据库 注意 url ip地址换的时候 oracle 里的listener.ora thnsnames.ora也要随之变化 重启数据库 不然可能会报出 java.sql.SQLException: ...

  8. android 方案源码下载repo同步遇到的问题

    1. error: could not verify the tag 'v1.12.4'的解决 repo init -u git://github.com/CyanogenMod/android.gi ...

  9. C# 鼠标左右手切换

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  10. 你应该更新的 Java 知识

    作者:dreamhead 出处:<你应该更新的Java知识>系列 你应该更新的 Java 知识 Tag:你应该更新的Java知识 Java Guava 集合 版权声明:转载时请以超链接形式 ...