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. IBM Security AppScan Glass Box:一种全新的漏洞扫描思想

    IBM Security AppScan Glass Box:一种全新的漏洞扫描思想 Glass Box 是 IBM Security AppScan Standard Edition(以下简称 Ap ...

  2. NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(三)

     Precondition: hadoop 2.7.1 hbase 0.98.13 solr 5.2.1 / Apache Solr 4.8.1 http://archive.apache.org ...

  3. C# SqlBulkCopy类批量导入数据

    特别注意  sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName); 插入的时候列的顺序可 ...

  4. [译]GLUT教程 - 整合代码3

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far III 这里我们准备包含一 ...

  5. linux 文件夹-文件权限设置

    只设置文件夹权限为755 文件权限为644find -type d -exec chmod 755 {} \;  find -type f -exec chmod 644 {} \;  或者  fin ...

  6. shell脚本中8种字符串截取方法_转自脚本之家

    转自:http://www.jb51.net/article/56563.htm 参考:http://blog.csdn.net/taiyang1987912/article/details/3955 ...

  7. linux模块导出符号 EXPORT_SYMBOL_GPL&EXPORT_SYMBOL(转)

    转自:http://blog.csdn.net/angle_birds/article/details/7396748 一个模块mod1中定义一个函数func1:在另外一个模块mod2中定义一个函数f ...

  8. Python鸡汤

    标准库 很正确 外部库 有一些风险,可能有bug,可能文档不全,可能长时间未更新. ipython 1 pip 这应该是安装Python后第一个需要的命令 pip install -i -i, --i ...

  9. iOS拓展:react-native创建工程的坑和解决方法

    正文 首先,在创建工程之前,你需要有环境.这个网上很多,不赘述. 我们直接从创建工程开始,一步步记叙这过程. 坑一 一.打开终端,cd到你放文件的位置(我放在桌面了): yuqideMacBook-A ...

  10. PHPstorm如何导入字体主题

    概要: 今天在安装phpstorm的时候发现导入字体主题时,出了问题,这个问题总是困惑我,并且曾经遇到过,没记录下来,所以想着这次记录下来吧.网上搜的稀里糊涂的,还是自己做个summary! 前提: ...