/*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. 交叉编译faac共享库

    作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 Advanced Audio Coding.一种专为声音数据设计的文件压缩格式,与Mp3不同,它採 ...

  2. 機器學習基石 机器学习基石 (Machine Learining Foundations) 作业2 Q16-18 C++实现

    大家好,我是Mac Jiang,今天和大家分享Coursera-NTU-機器學習基石(Machine Learning Foundations)-作业2 Q16-18的C++实现.尽管有非常多大神已经 ...

  3. “XXX.Index”不扩展类“System.Web.UI.Page”,因此此处不同意的问题

    "XXX.Index"不扩展类"System.Web.UI.Page",因此此处不同意的问题 原因:设计页面继承的路径和后台.cs页面类的路径不一致造成的 看下 ...

  4. U盘安装 CentOS 64bit (dell c6100, CentOS6.3, 64bit)

    在淘宝买了一个server,dell c6100,64bit, 曾经系统是black apple.近期又买了一块企业级硬盘打算装CentOS. 综合各方面原因决定安装6.3版本号. 我參考了http: ...

  5. tensorflow利用预训练模型进行目标检测(二):预训练模型的使用

    一.运行样例 官网链接:https://github.com/tensorflow/models/blob/master/research/object_detection/object_detect ...

  6. Centos7 minimal 系列之Redis共享sessionid(七)

    这一章节的内容就当看看,只是个人理解,我想应该是有误的. 一.SessionId sessionid是一个会话的key,浏览器第一次访问服务器会在服务器端生成一个session,有一个sessioni ...

  7. c# ExecuteScalar()

    ExecuteScalar这个方法是从数据库中检索单个值返回值是object类型,必须用与它在数据库里存放的类型相同类型或者可以转换成的类型,比如数据是nchar类型值为 "123" ...

  8. Pyhton学习——Day48

    # 转载:http://www.cnblogs.com/yuanchenqi/articles/6437362.html#python关于mysql的API--pymysql模块# 执行sql语句# ...

  9. 谈 instanceof 和 typeof 的实现原理

    typeof: js 在底层存储变量的时候,会在变量的机器码的低位1-3位存储其类型信息

  10. iOS开发——GPUImage源码解析

    一.基本概念 GPUImage:一个开源的.基于openGL的图片或视频的处理框架,其本身内置了多达120多种常见的滤镜效果,并且支持照相机和摄像机的实时滤镜,并且能够自定义图像滤镜.同时也很方便在原 ...