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
 

Source

题目大意是给出一串数据, 求各位数字的和, 如果数据只有一个数字直接输出, 因为之前做过各位数字的和, 也没考虑到本题是 大数, 第一次尝试的代码如下:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			int n = cin.nextInt();
			if (n == 0) {
				break;
			}
			System.out.println(fun(n));
		}
		cin.close();
	}

	private static int fun(int n) {
		if (n < 10) {
			return n;
		} else {
			while (n > 9) {
				n = fun(n % 10) + fun(n / 10);
			}
			return n;
		}
	}

}

Wrong Answer

后来看了 Discuss 区才知道这题要用大数, 于是调用了 JAVA 的大数类 BigInteger, 代码如下:


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

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			BigInteger n = cin.nextBigInteger();
			if (n.equals(BigInteger.ZERO)) {
				break;
			}
			System.out.println(fun(n));
		}
		cin.close();
	}

	private static BigInteger fun(BigInteger n) {
		if (n.compareTo(BigInteger.TEN) == -1) {
			return n;
		} else {
			while (n.compareTo(BigInteger.TEN) >= 0) {
				n = fun(n.mod(BigInteger.TEN)).add(fun(n.divide(BigInteger.TEN)));
			}
			return n;
		}
	}

}


Accepted

用到了 BigInteger 类中的多个运算方法和 BigInteger.TENBigInteger.ZERO 两个常量

其中用到了 compareTo() 方法, 返回 -1->小于, 0->等于, 1->大于.

BigInteger 类中方法大多返回也是 BigInteger 类型, 甚至参数也多是 BigInteger 类型, 好在可用 valueOf(int n) 把任意整数转换为 BigInteger 类型

查资料时候无意看到了一个定理: 九余数定理 和中国剩余定理相关. 貌似本题应用这个定理可以更快完成, 占个坑以后来补.

一个数对九取余,得到的数称之为九余数;
一个数的九余数等于它的各个数位上的数之和的九余数!


先附上 AC 代码:

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

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			BigInteger n = cin.nextBigInteger();
			if (n.equals(BigInteger.ZERO)) {
				break;
			}
			if (n.mod(BigInteger.valueOf(9)).equals(BigInteger.ZERO)) {
				System.out.println(9);
			} else {
				System.out.println(n.mod(BigInteger.valueOf(9)));
			}
		}
		cin.close();
	}

}

由于第二句话, 如果数据为9再对9取余则会输出0, 所以只要特殊处理这一个, 其余直接对9取余输出既可

通过数学定理大大减少了代码量和运行效率, 可见数学十分重要

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值