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. 涛哥的Python工具箱之批量删除含指定字符串行

    我们在软件研发中不可避免的要用到大量的反复性的繁琐的工作,比如批量改动代码中接口的字符串.批量下载文件并又一次按规则命名.这些工作人工做特别累,尤其是对我这样的懒人来说. 对于一个出色的程序猿来说,反 ...

  2. 【C语言疯狂讲义】(三)C语言运算符

    1.运算符: 连接两个操作数(常量.变量)的符号 用运算符依照一定的规则连接的式子称为表达式 运算符的分类: 1)操作数的个数: 单目运算(++     sizeof    !) 双目运算符:... ...

  3. systemd管理进程

    systemd很强大的管理工具,这里简单用来管理一个进程: [Unit]Description=Imges Compress Server [Service]Type=simpleExecStart= ...

  4. sprint3 【每日scrum】 TD助手站立会议第九天

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 整合原来做过的功能,并做相应的改进,整合其他的功能 团队进入最终的功能测试阶段,准备发布Beta版 在测试阶段BUG太多,不知道如何解决 Y ...

  5. 使用AlloyLever来搞定开发调试发布,错误监控上报,用户问题定位

    传送门: # gituhbhttps://github.com/AlloyTeam/AlloyLever # 官网https://alloyteam.github.io/AlloyLever/ 下载和 ...

  6. mysql更改表结构:添加、删除、修改字段、调整字段顺序

    添加字段: alter table `user_movement_log` Add column GatewayId int not null default 0 AFTER `Regionid` ( ...

  7. ubuntu16.04 安装系统之后的开发必备-sourcelist--idk-sublime--opencv

    设置sourcelist.txt # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ub ...

  8. Junit 内部解密之二: TestResult + TestListener + Assert

    转自:http://blog.sina.com.cn/s/blog_6cf812be0100wbhw.html 之前我们看到了Test接口里面的run方法有个TestResult的参数,不错,这个类就 ...

  9. POI 实现合并单元格以及列自适应宽度

    POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...

  10. golang struct 定义中json``解析说明

    在代码学习过程中,发现struct定义中可以包含`json:"name"`的声明,所以在网上找了一些资料研究了一下 package main import ( "enco ...