Digital Roots

Time Limit: 1000ms   Memory limit: 65536K

题目描述

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 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.

输出

For each integer in the input, output its digital root on a separate line of the output.

示例输入

24
39
0

示例输出

6
3

提示

请注意,输入数据长度不超过20位数字。
      题目要求:给你一串数字,这些数字的“和”,如果大于一位数,则再将他们的和数字的每一位再相加,如此下去,直到数字和为一位数字!
     算法思想很简单,多用了几个while循环,关键是思路要清晰!
 
#include <stdio.h>
#include <string.h> int main()
{
char s[50];
int a[50], e;
int i, j, k;
int sum;
while(scanf("%s", s)!=EOF )
{
if(s[0]=='0')
break;
int len=strlen(s);
e=0;
sum=0;
for(i=0; i<len; i++)
{
a[e++]=s[i]-48 ;
sum+=a[e-1];
}
if(sum<10)
printf("%d\n", sum );
else
{
int cnt=sum;
sum=0;
while(cnt>10)
{
while(cnt!=0)
{
sum=sum + (cnt%10);
cnt=cnt/10;
}
if(sum<10)
{
printf("%d\n", sum );
break;
}
else
{
cnt=sum;
sum=0;
}
}
}
}
return 0;
}

SDUT 1048 Digital Roots的更多相关文章

  1. Digital Roots 1013

    Digital Roots 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:456            测试通过:162 描述 T ...

  2. Eddy's digital Roots

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

  3. Digital Roots 分类: HDU 2015-06-19 22:56 13人阅读 评论(0) 收藏

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

  4. HDU 1163 Eddy's digital Roots

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. ACM——Digital Roots

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...

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

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

  7. Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. HDU1163 Eddy&#39;s digital Roots【九剩余定理】

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

随机推荐

  1. lua学习笔记(六)

    (2012-04-12 23:32:35) 转载▼   函数  定义     function mytest(a,b,c) <函数体> end     mytest = function( ...

  2. spark 的一些常用函数 filter,map,flatMap,lookup ,reduce,groupByKey

    定义不带参数也不带返回值的函数(def :定义函数的关键字  printz:方法名称) scala> def printz = print("scala hello")   ...

  3. freemarker 开始时间与当前时间进行比较

    <#if startTime?datetime lt .now?datetime>:年月日时分秒比较 <#if startTime?date lt .now?date>:年月日 ...

  4. oracle中的minus数据比对

    1.要有唯一索引或者主键作为前提,减少数据冲突的数量,如示例标红的地方:   2.当有in查询的时候,尽量用exists,这样能提高查询效率: create table TF_F_USER_DIFF1 ...

  5. COM组件多接口对象模型

    COM组件有两种接口类型,Dual and Custom,如下图所示.本文说的是Custom.所谓多接口COM对象是指此COM对象实现了多于一个的自定义接口,即Custom接口. 接口图如下: 需要注 ...

  6. jQuery效果之显示与隐藏

    .hide([duration][,complete])--目标元素的隐藏. .show([duration][,complete])--目标元素的显示: .toggle([duration][,co ...

  7. saltstack内置state模块file之managed

    managed管理一个模板文件,载入到各个节点并运行相应配置 salt.states.file.managed(name, source=None, source_hash='', user=None ...

  8. XtraBackup全备与增量备份

    一.XtraBackup安装 下载地址:http://www.percona.com/downloads/XtraBackup/XtraBackup-2.2.8/source/ 安装步骤: ===== ...

  9. 自定义cginc文件

    首先定义一个cginc文件如下所示: #ifndef MY_CG_INCLUDE #define MY_CG_INCLUDE struct appdata_x { float4 vertex : PO ...

  10. iOS 线程管理的学习记录

    本文转载至 http://www.2cto.com/kf/201312/265451.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...