HDOJ 1013题Digital Roots 大数,9余数定理

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

1 24 239 30 4 5 6 7 8

Sample Output

1 6 23 3 4 5 6 7

一个数对九取余,得到的数称之为九余数;

一个数的九余数等于它的各个数位上的数之和的九余数!

题目大意:

 给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下:

        例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6;

        因为 6 < 10,所以就认为6是数字24的“数根”;

        而对于数字 39 , 将39的各个位上的数字“分离”,分别得到数字 3 和 9,而3+9=12,且12>10;

       所以依据规则再对 12 进行相应的运算,最后得到数字3,而3<10,所以就认为3是数字39的“数根”。

              通过运算可以发现任何一个数的“数根”都是一个取值范围在 1 ~ 9之间的正整数,

     且任何一个正整数都只有唯一的一个“数根”与其相对应。

              题目要求数字 n^n 的“数根”

解题思路:

九余数定理

一个数对九取余后的结果称为九余数。

一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加)

代码如下:

1#include <stdio.h> 2#include <stdlib.h> 3#include<string.h> 4int main() 5{ 6 char a[1010]; 7 int i,j,s,l; 8 while(~scanf("%s",&a)&&a[0]!='0') 9 { 10 l=strlen(a); 11 s=0; 12 for(i=0;i<l;i++) 13 { 14 s=s+a[i]-'0'; 15 } 16 s=s%9; 17 if(s==0) 18 s=9; 19 printf("%d\n",s); 20 } 21 return 0; 22}

一个数对九取余,得到的数称之为九余数;

一个数的九余数等于它的各个数位上的数之和的九余数!

本文同步分享在 博客“谙忆”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )