2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 I. Reversion Count (java大数)

Description:

There is a positive integer X, X's reversion count is Y. For example, X=123, Y=321; X=1234, Y=4321. Z=(X-Y)/9, Judge if Z is made up of only one number(0,1,2...9), like Z=11,Z=111,Z=222,don't consider '+'and '-'.

Input:

Input contains of several test cases. Each test case only contains of a number X, L is the length of X. ( 2 <= L < 100)

 

Output:

Output “YES”or “NO”.

样例输入

10
13

样例输出

YES
YES

题意:一个数与自己的翻转做差,最后结果的数是不是只有一个数字组成
思路:利用java中的BigInteger与StringBuffer类
代码如下:
 1 import java.math.*;
 2 import java.security.Principal;
 3 import java.util.*;
 4 
 5 import javax.xml.bind.helpers.AbstractMarshallerImpl;
 6 
 7 import java.*;
 8 public class Main {
 9 
10     public static void main(String[] args) {
11         Scanner cin = new Scanner(System.in);
12         BigInteger x;
13         String xx,yyy;
14         int cnt[] = new int[10];
15         while (cin.hasNext()){
16             for (int i=0;i<10;i++)
17                 cnt[i]=0;
18             xx = cin.nextLine();
19             StringBuffer yy = new StringBuffer(xx).reverse();
20             yyy = yy.toString();
21             BigInteger a = new BigInteger(xx);
22             BigInteger b = new BigInteger(yyy);
23             if (a.compareTo(b)>=0){
24                 a = a.subtract(b);
25             }
26             else a = b.subtract(a);
27             a = a.divide(BigInteger.valueOf(9));
28             String s = a.toString();
29             for (int i=0;i<s.length();++i){
30                 cnt[s.charAt(i)-'0' ]++;
31             }
32             //System.out.println(a);
33             int ans = 0;
34             for (int i=0;i<10;i++){
35                 if (cnt[i]!=0) ans++;
36             }
37             if (ans==1) System.out.println("YES");
38             else System.out.println("NO");
39         }
40     }
41 }
 
原文地址:https://www.cnblogs.com/agenthtb/p/8916451.html