zoukankan      html  css  js  c++  java
  • Digital Roots

    描述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.

     
    输入
    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.
    输出
    For each integer in the input, output its digital root on a separate line of the output.
    样例输入
    24
    39
    0
    样例输出
    6
    3

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int sum(int number);
     5 
     6 int main(){
     7     char s[1000];
     8     int i;
     9     int number;
    10     
    11     while(scanf("%s",&s)!=EOF){
    12         
    13         if(strcmp(s,"0")==0)
    14             break;
    15             
    16         number=0;
    17         for(i=0;s[i]!='';i++){
    18             number+=(s[i]-'0');
    19         }
    20         
    21         while(number>=10){
    22             number=sum(number);
    23         }
    24         
    25         printf("%d
    ",number);
    26     }
    27     return 0;
    28 }
    29 
    30 int sum(int number){
    31     int result;
    32     
    33     result=0;
    34     while(number){
    35         result+=number%10;
    36         number/=10;
    37     }
    38     
    39     return result;
    40 }
     
  • 相关阅读:
    rabiitmq 消息丢失处理
    后端返回文件流和json格式的方式进行文件下载导出
    elasticsearch 安装
    docker-compose 命令
    艾孜尔江的部分影视作品
    游戏作品:万里同风
    Unity设置计时器
    Unity项目中VS Code部分功能出现问题解决思路
    Unity接收FFmpeg的UDP推流
    SVN Basics
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/4101520.html
Copyright © 2011-2022 走看看