题目描述
对于一个整数X,定义操作rev(X)为将X按数位翻转过来,并且去除掉前导0。例如:
如果 X = 123,则rev(X) = 321;
如果 X = 100,则rev(X) = 1.
现在给出整数x和y,要求rev(rev(x) + rev(y))为多少?
如果 X = 123,则rev(X) = 321;
如果 X = 100,则rev(X) = 1.
现在给出整数x和y,要求rev(rev(x) + rev(y))为多少?
输入描述:
输入为一行,x、y(1 ≤ x、y ≤ 1000),以空格隔开。
输出描述:
输出rev(rev(x) + rev(y))的值
思路:
用集合list来存储数字,num%10和num/=10就能实现数据翻转
代码:
package com.example.shirodemo; import java.util.*; /** * Created by ASUS on 2018/7/2 * * @Authod Grey Wolf */ public class Practice { public static void main(String[] args) { Practice practice = new Practice(); practice.sys(); } private void sys() { Scanner scan=new Scanner(System.in); int x=scan.nextInt(); int y=scan.nextInt(); System.out.println(rev(rev(x)+rev(y))); } private int rev(int x) { Integer sum=0; //把每次得到的数存入到集合中 ArrayList<Integer>list=new ArrayList<>(); while (x>0){ list.add(x%10); x=x/10; } //判断这个集合中值是不是不止一个。如果一个直接返回 //如果多个,计算出这个值, //如果没有,返回0,这中间有去除掉前导0的功能 if(list.size()>1){ //计算集合中的值 int i; for (i=0;i<list.size();i++){ int temp=list.get(i); sum=i!=0?sum*10+temp:temp; } }else { sum=list.size()==1?list.get(0):sum; } return sum; } }
效果:
123 100
223
我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。