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
 
Sample Way
 

#include <stdio.h>
#include <string.h>

int main()
{
  char num[2000];
  int sum,sum1,n,i;
  memset(num,0,sizeof(num));           //将字符型数组全部赋值为‘0’。
  gets(num);                                      //将用户输入的整数以字符型存入num。
  while(strcmp(num,"0")!=0)              
  {
  sum1=sum=0;
  n=strlen(num);
    for(i=0;i<n;i++)

    {
    sum1+=(num[i]-'0');                //注意char型跟int型通用,这个地方查ASCII可知原理
    }
    while(1)            //while循环求出数根。
    {
    sum+=sum1%10;                  
    sum1/=10;
    if(sum1==0)
     {
      if(sum<10)
      {
      printf("%d\n",sum);
      break;
      }
      else
      {
      sum1=sum;
      sum=0;
      }
       }
    }
  memset(num,0,sizeof(num));
  gets(num);
  }
return 0;
}

hd acm1013的更多相关文章

  1. ATI Radeon HD 5450 with full QE/CI Support ( 转载 )

    ATI Radeon HD 5450 with full QE/CI Support - DSDT (Contains HDMI Audio Edit Too) & AGPM included ...

  2. XPS 15 9530使用Windows10频繁发生Intel HD Graphics 4600驱动奔溃的一种解决方法

    本人使用XPS 15 9530.集成显卡为Intel HD Graphics 4600.操作系统Windows 10 Pro,使用过程当中经常会发生集成显卡奔溃的问题,错误提示如下: Display ...

  3. Radeon HD 7850 vs Radeon R9 270X

    Radeon HD 7850 vs Radeon R9 270X  HW compare   Intro The Radeon HD 7850 comes with a GPU core speed ...

  4. 电影TS、TC、SCR、R5、BD、HD等版本是什么意思

    在很多电影下载网站的影片标题中我们都能看到,比如<刺杀希特勒BD版>.<游龙戏凤TS版>等,这些英文缩写都是什么意思呢?都代表什么画质?以下就是各个版本的具体含义: 1.CAM ...

  5. stm32类型cl、vl、xl、ld、md、hd的含义

    - startup_stm32f10x_ld_vl.s: for STM32 Low density Value line devices - startup_stm32f10x_ld.s: for ...

  6. 瑞昱Realtek(Realtek HD Audio Driver)音频声卡驱动R2.49 for Win7_Vista

    不管是在高端系列主板上,还是在低端系列主板上,我们都能看到Realtek瑞昱的身影,Realtek HD Audio Driver能够支持所有的Realtek HD Audio音频驱动.Realtek ...

  7. cocos2d-x 2.0.3 设置高清模式注意事项(已移除-hd方式)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=304 在cocos2d-x 2. ...

  8. %hd %d %ld %u ......

    %d 有符号10进制整数 %ld 长整型 %hd短整型%md,m指定的是输出字段的宽度,默认左补空格, 如果数据的位数小于m,则左端补以空格,若大于m,则 按实际位数输出,如: printf(&quo ...

  9. 求刷Kindle Fire HD的方法

    前几天入手了台Amazon Kindle Fire HD 其系统是经过Amazon尝试改造过的Android,用起来很不爽,想刷个CM10之类的,求教程和工具.

随机推荐

  1. 通过Cloudera Manager安装CDH 5.6

    CDH的简介 大家常常说CDH.其全称是:Cloudera's Distribution Including Apache Hadoop.简单的说是Cloudera公司的Hadoop平台,是在Apac ...

  2. SQL Server统计信息:问题和解决方式

    在网上看到一篇介绍使用统计信息出现的问题已经解决方式,感觉写的很全面. 在自己看的过程中顺便做了翻译. 因为本人英文水平有限,可能中间有一些错误. 假设有哪里有问题欢迎大家批评指正.建议英文好的直接看 ...

  3. requests 模块入门玩法和高级玩法

    1.安装 pip install requests 2. http://docs.python-requests.org/zh_CN/latest/user/quickstart.html http: ...

  4. C#中Lock静态字段和实例字段

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Weka关联规则分析

    购物篮分析: Apriori算法: 参数设置: 1.car 如果设为真,则会挖掘类关联规则而不是全局关联规则. 2. classindex 类属性索引.如果设置为-1,最后的属性被当做类属性. 3. ...

  6. IE 下 log 调试的一点不同

    介绍一点IE下控制台调试与chrome的区别 数组 alert([1,2]) console.log([1,2]) console.log([1,2],"1,2") alert I ...

  7. android的DrawerLayout用法

    DrawerLayout的关键点(我认为的)就在于layout文件的layout_gravity属性的值,只有左右,两种选择,不能从上下滑出来,就算有这个效果也不是这个套路弄出来的. <?xml ...

  8. linux用一键安装包 禅道

    ----------------1.重启apache 和 mysql /opt/zbox/zbox restart -----------------2.问题1:发现端口被占用 apache启动报错( ...

  9. 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用

    和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...

  10. python 迭代器,生成器与推导式

    函数的动态传参 *args 动态接收所有位置参数 **kwargs 动态接收关键字参数 顺序: 位置参数, *args, 默认参数, **kwargs def func(*args, **kwargs ...