HDOJ 1013 Digital Roots

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

解释一下题目: 就是给你一个数让你求数的数根!什么叫数根? 比如24这个数的数根,就是把24各个位的数加起来!2+4=6 而且6是个位数,于是它就是24的数根。 然后39这个数一样的方法,3+9=12 此时我们发现12不是个位数,所以它不是数根,继续让它各个 位数相加,1+2=3,3是个位数所以3是39的数根。
数根公式: a的数根b = ( a - 1) % 9 + 1 还不明白可以百度百科一下数根,很简单的数学定义,简称自然数的一种定义.

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String n = sc.next();
            if(n.equals("0")){
                break;
            }
            BigInteger m = new BigInteger(n);

            m = m.add(new BigInteger("-1"));
            m= m.mod(new BigInteger("9"));
            m = m.add(BigInteger.ONE);
            System.out.println(m);
        }
    }
}
时间: 2024-07-31 18:42:15

HDOJ 1013 Digital Roots的相关文章

算法题:HDU 1013 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 pr

HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

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 a

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 a

HDU 1006 Digital Roots

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 a

HDOJ 1013

//wa的代码,既然说是整数,应该不用考虑大数,可实际上却要考虑大数   #include<stdio.h> int main() { int num,temp; while(scanf("%d",&num),num) { temp=num%9; printf("%d\n",temp); } return 0; } //或者mod9,若是k,则各个数字和为k   //既然说是整数,应该不用考虑大数,可实际上却要考虑大数   /*超时代码 #inc

ZOJ 1009, 1115, 1476, 1733, 2405 解题报告

        星期天这天一口气AC了五道题,除了1009外基本都可算是简单题.        (1)1009 Enigma:        http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1009        题目是讲解二战期间德国使用的密码机Enigma.明文通过按键,通过转盘(rotor)转换为密文.如下图所示为有1个转盘,一共有6个字母的情况,每击键一次,转盘转动一格.如果含有多个转盘,则以类似数字进制方式转动,

Digital Unix Version 4.0下Oracle 8.0.5服务器的安装

oracle|unix|服务器 Digital Unix Version 4.0下Oracle 8.0.5服务器的安装 石骁騑(中软网络技术股份有限公司,北京,100081) 一. 系统需求在安装Oracle 8 服务器前,首先检查系统是否满足表1和表2所示的软硬件需求.1. 硬件需求 表1 硬件需求硬件项目 需求CPU Digital Unix alpha 系统内存 最少128M RAM交换空间 2-4倍的内存大小磁盘驱动(Disk Drives) 至少四个设备:一个用于Oracle软件的分发

DVNEWS 3.2 1013版免虚拟目录的安装方法,只要三个步骤

虚拟目录 以下操作,在W2K3 E文版,IIS 6,免虚拟目录测试通过,解决了一部份虚拟主机用户创建不了虚拟目录,或者把DVNEWS放在根目录因大量文件存在而管理麻烦的问题. 1.首先将DVNEWS/BIN目录COPY到网站根目录 2.将DVNEWS/web.config文件作以下修改 <RewriterRule> <LookFor>~/换成你的DVNEWS目录/c(\d{1,8})\.aspx</LookFor> <SendTo>~/换成你的DVNEWS目

解决苹果刷机时1601错误1013错误等的方法

我们在刷机的时候我们会遇到各式各样的错误,如:3194错误.1031错误.160X错误等等,这些错误时常让我们很烦恼.今天三联教程给大家分享下如何解决这些问题,相信大家看完之后,遇到同样的问题都会解决的. iOS系统错误集合:iTunes 中的警告信息可能还包括(但不限于)以下编号之一:2.4.5.6.9.13.14.-18.19.20.21.23.28.29.-48.-50. 1002.1004.1011.1013.1014.1015.1601.1602.1603.1604.1608.1609