您的位置:首页 > 其它

HDOJ 1013 Digital Roots

2017-10-13 20:17 323 查看
HDACM1013

大数解法

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()) {
BigInteger n = sc.nextBigInteger();
if (n.compareTo(BigInteger.ZERO)==0) {
break;
}
BigInteger sum = BigInteger.ZERO;
while (n.compareTo(BigInteger.ZERO)!=0){
BigInteger temp = n.mod(BigInteger.TEN);
sum = sum.add(temp);
n=n.divide(BigInteger.TEN);
if (n.compareTo(BigInteger.ZERO)==0) {
if (sum.compareTo(BigInteger.TEN)==-1) {
break;
}
n = sum;
sum = BigInteger.ZERO;
}
}
System.out.println(sum);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: