2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 I Reversion Count(Java大数)
题面:
- 26.82%
- 1000ms
- 65536K
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
题目描述:给你一个长度为100的数,问你用这个数的反转的数减去当前数的绝对值整除9后,所得的数是否只含有一种数字。
对于这道题目,看到这个数的长度为100,果断打开了eclipse。(java在大整数处理上确实是方便太多了)
如果知道BigInteger的话,这道题就很容易的被转化成了一个只需要考虑获取反转的数即可。
第一次用java在网络赛交题,java写得还是太不熟练了2333
附上java代码:
import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args) {
Scanner sca=new Scanner(System.in);
BigInteger n;
while(sca.hasNext()){
n=sca.nextBigInteger();
BigInteger tmp,res,ans;
BigInteger Ten=BigInteger.valueOf(10);
tmp=n;
res=BigInteger.ZERO;
while(tmp.compareTo(BigInteger.ZERO)==1){
res=res.multiply(Ten);
res=res.add(tmp.mod(Ten));
tmp=tmp.divide(Ten);
}
ans=res.subtract(n);
ans=ans.abs();
ans=ans.divide(BigInteger.valueOf(9));
BigInteger tmp1=ans.mod(Ten);
ans.divide(Ten);
int flag=1;
while(ans.compareTo(BigInteger.ZERO)==1){
BigInteger tmp2=ans.mod(Ten);
if(tmp2.compareTo(tmp1)==1){
System.out.println("NO");
flag=0;
break;
}
ans=ans.divide(Ten);
}
if(flag==1) System.out.println("YES");
}
}
}