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

题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNextInt()){
			int a = sc.nextInt();  // a就是sc.hasNextInt的内容
		}
	}
}

思路:来判断Z是否全部是由一样的的数字组成的

java大数的基本函数

参考网址::::https://blog.csdn.net/u011466175/article/details/38445723
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		BigInteger x,y,t,z,a;
		boolean flag;
		while(in.hasNext()) //不断的输入,相当于c语言中的EOF
               {
			flag=true;
			y=BigInteger.ZERO;//对y赋初值
			x=in.nextBigInteger(); //这里的x即是
			t=x;
			while(!(t.equals(BigInteger.ZERO))){
			    y=y.multiply(BigInteger.valueOf(10)).add(t.mod(BigInteger.valueOf(10)));
				t=t.divide(BigInteger.valueOf(10));
			}
			z=(x.subtract(y)).divide(BigInteger.valueOf(9));
			a=z.mod(BigInteger.valueOf(10));
			while(!(z.equals(BigInteger.ZERO))){
				if(!(a.equals(z.mod(BigInteger.valueOf(10)))))
				{
					flag=false;
					break;
				}
				z=z.divide(BigInteger.valueOf(10));
			}
		  if(flag){
			  System.out.println("YES");
		  }else{
			  System.out.println("NO");
		  }
		}
	}
}

compareTo方法 ::
用来实现比较策略
compareTo方法返回一个int值
使用该对象和传入的对象进行比较。
如果该对象大于传入的对象 要求返回一个整数(比如1)
如果该对象小于传入的对象 要求返回一个负数(比如-1)
等于 则返回0
这样定义好之后,有些有序的集合,比如TreeSet, 就能根据你所定义的对象比较策略来按顺序的排列
也可以使用 Collections这个工具类里面的 sort方法进行排序
简单的说,就是告诉机器,两个对象比较的时候,哪个是大哪个是小,这样机器才能比较出来(毕竟,你不可能总是存数字这种大小很明显的东西)

toString方法:

tostring()是一个方法,返回的值是调用这个方法的对象所对应的字符串。

StringBuffer()方法:

参考网址 ::https://www.cnblogs.com/liu-chao-feng/p/5636063.html

import java.util.Scanner;
import java.math.BigInteger;
public class Main {
	public static void main(String args[]){
		Scanner in=new Scanner(System.in);
		while(in.hasNext()){
			String str=in.next();
			String str1=new StringBuffer(str).reverse().toString();//用了StringBUffer()方法的reverse()函数将字符串逆置并且转化为对应的字符串
            BigInteger x=new BigInteger(str);//将字符串转化为大数
            BigInteger y=new BigInteger(str1);
            if(x.compareTo(y)<0)
            {
            	BigInteger tmp=x;
            	x=y;
            	y=tmp;
            }
	       BigInteger z=x.subtract(y);
	       String res=z.divide(BigInteger.valueOf(9)).toString();//将数字转化为字符串
	         char []ch=res.toCharArray();
              //将字符串对象中的字符转换为一个字符数组
	         int flag=1;
	         for(int i=0;i<ch.length;i++)
{ if(ch[i]!=ch[0]) { flag=0; System.out.println("NO"); break; } } if(flag==1){ System.out.println("YES"); }}}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值