• 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 }
     
  • 相关阅读:
    Tomcat线程参数
    CDH平台规划注意事项
    python 不同数据类型的序列化
    Python 中__new__方法详解及使用
    线程生命周期
    如何在JAVA中每隔一段时间执行一段程序
    手动开启是事务提交回滚
    MySQL数据类型转换函数CAST与CONVERT的用法
    mybatis插入是返回主键id
    解决dubbo注册zookepper服务IP乱入问题的三种方式
  • 原文地址:https://www.cnblogs.com/agenthtb/p/8916451.html
Copyright © 2020-2023  润新知