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. C#CRC16 Modbus 效验算法

    CRC校验(循环冗余校验)小知识 CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗 ...

  2. elk升级文档

    1.kibana等都统一版本了,5.4版本的kibana要5.4版本的elasticsearch 2.现有架构: logstash logstash读取日志-------->内网redis做队列 ...

  3. 【Caffe代码解析】Blob

    主要功能: Blob 是Caffe作为传输数据的媒介,不管是网络权重參数,还是输入数据,都是转化为Blob数据结构来存储,网络,求解器等都是直接与此结构打交道的. 其直观的能够把它看成一个有4纬的结构 ...

  4. FILE 创建

    public class CreateDelFileUtils implements Serializable{ /** * */ private static final long serialVe ...

  5. 【数据挖掘】关联分析之Apriori(转载)

    [数据挖掘]关联分析之Apriori 1.Apriori算法 如果一个事务中有X,则该事务中则很有可能有Y,写成关联规则 {X}→{Y} 将这种找出项目之间联系的方法叫做关联分析.关联分析中最有名的问 ...

  6. usermod命令

    usermod 功能: 修改用户 常用参数:-c    账号说明-d    账号家目录-e    密码失效日期-g    主用户组GID-G    次用户组GID-l    账号名称-s    she ...

  7. liunx 下安装 php_screw 扩展 以及报错处理

    php_screw 是一个 php 源代码加密扩展.首先来看一下 php_screw 在liunx下是如何安装的 首先 去源完整下载 安装包,现在的最新版是 1.5,我们就用1.5 来做个实例 如果有 ...

  8. python pymysql安装

    ==================pymysql=================== 由于 MySQLdb 模块还不支持 Python3.x,所以 Python3.x 如果想连接MySQL需要安装 ...

  9. 【BZOJ2728】[HNOI2012]与非 并查集+数位DP

    [BZOJ2728][HNOI2012]与非 Description Input 输入文件第一行是用空格隔开的四个正整数N,K,L和R,接下来的一行是N个非负整数A1,A2……AN,其含义如上所述.  ...

  10. 【BZOJ3689】异或之 堆+可持久化Trie树

    [BZOJ3689]异或之 Description 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A ...