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. 数据结构之shell排序

    #SIZE  10        //直接插入排序     void insert_sort(){           int i,j;           int array[SIZE+1];   ...

  2. JAVA、Android与Cordova环境搭建

    一些坑(如Manager.exe闪退的问题)请查看:https://www.cnblogs.com/CyLee/p/9911195.html 官方网址: # Cordova http://cordov ...

  3. eclipse下构建maven spring项目

    准备工作: 1.下载eclipse(Eclipse Java EE IDE for Web Developers,Version: Juno Service Release 2). 2.下载maven ...

  4. python socket编程(socket)

    代码如下: server端: import sockets=socket.socket(socket.AF_INET,socket.SOCK_STREAM)host=socket.gethostnam ...

  5. sparkstreaming+socket workCount 小案例

    Consumer代码 import org.apache.spark.SparkConf import org.apache.spark.streaming.StreamingContext impo ...

  6. 【深入JVM】JVM工具之JMAP

    一.工具介绍 假设把java\bin文件夹配置到环境变量.在cmd输入jmap会有例如以下提示: 翻译:打印出某个java进程(使用pid)内存内的,全部'对象'的情况(如:产生那些对象,及其数量). ...

  7. linux 中vim学习与总结

    平常使用vim总是忘记快捷键,在这里做一个总结一下比较常用的快捷把,省的每次都要去查. h : 向左移动一个字符(←) j : 向上移动一个字符(↑) k : (↓) l : (→) ctrl+f : ...

  8. mac 编译ffmpeg真简单!

    brew info ffmpeg 有安装选项,并且会提示依赖库安装状态 安装依赖库 brew install automake fdk-aac git lame libass libtool libv ...

  9. 线程池 http请求

    package com.aibi.cmdc.test; import java.io.BufferedReader; import java.io.InputStream; import java.i ...

  10. WebView加载URL跳转到系统浏览器的解决方法

    1.问题 webview加载url跳转到系统浏览器,用户体验非常的差 2.解决方法 重写WebViewClient的shouldOverrideUrlLoading(WebView view, Str ...