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. 征信报告页面的input验证收集

    https://ipcrs.pbccrc.org.cn/ function checkLoginName() { var loginName = $.trim($("#loginname&q ...

  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. 启动spring boot 异常

    再我搭建spring boot工程后,run application的时候抛出下面异常 Exception /slf4j-log4j12-.jar). If you are using WebLogi ...

  4. delphi 无边框窗体常见问题

    实现无边框窗体很简单,直接将窗体的BorderStyle属性设置为bsNone即可.但是这样会引起2个问题: 1.在xp系统下,任务栏鼠标右键点击无法弹出菜单 解决办法:在FormShow中加入这个过 ...

  5. apache占用内存高解决办法

    我用512M的vps,访问量不大,但内存占用很大,甚至宕机. 我用top,然后shitf+m发现,httpd占用内存极大.经过网上找资料设置后,用过一段时间终于没再出现内存问题了. 首先查找配置文件的 ...

  6. Java中的各种锁--分类总结

    前言 本文需要具备一定的多线程基础才能更好的理解. 学习java多线程时,最头疼的知识点之一就是java中的锁了,什么互斥锁.排它锁.自旋锁.死锁.活锁等等,细分的话可以罗列出20种左右的锁,光是看着 ...

  7. selenium 遇到chrome 弹出是否保存密码框

    --待验证(以下是java脚本) ChromeOptions options = new ChromeOptions(); options.addArguments("--start-max ...

  8. html5中form表单新增属性以及改良的input标签元素的种类

    在HTML5中,表单新增了一些属性,input标签也有了更多的type类型,有些实现了js才能实现的特效,但目前有些浏览器不能全部支持.下面是一些h5在表单和input标签上的一些改动. <!D ...

  9. And Then There Was One(约瑟夫环)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  10. wcf读取message内容

    private string MessageToString(ref Message message) { WebContentFormat messageFormat = this.GetMessa ...