• 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 }
     
  • 相关阅读:
    DataTable 类(二)处理表中的数据
    .NET中的正则表达式
    DataTable 类(一)表结果操作
    DataGridView(2)数据操作
    DataGridView(一)
    VS2008中配置 Windows SDK v7
    TabControl
    .NET中的正则表达式 (一)Regex 类
    db2的count()函数和sum()函数的用法
    显示原始字符串
  • 原文地址:https://www.cnblogs.com/agenthtb/p/8916451.html
Copyright © 2020-2023  润新知