牛客网——数字反转(水题)

点击打开链接

题目描述

对于一个整数X,定义操作rev(X)为将X按数位翻转过来,并且去除掉前导0。例如:
如果 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))的值
示例1

输入

复制
123 100

输出

复制
223

思路:这道题我是用java写的,偷了个懒,利用了java中现成的字符串反转方法和字符串转换成Integer的方法。

import java.util.Scanner;

public class Main {

    public static void word(){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){

           String a = scan.next();
           String b = scan.next();
           StringBuffer c = new StringBuffer().append(a);
           StringBuffer d = new StringBuffer().append(b);
           c = c.reverse();
           d = d.reverse();
           a = c.toString();
           b = d.toString();
           Integer e = (Integer.valueOf(a)+Integer.valueOf(b));
           a = e.toString();
           StringBuffer f = new StringBuffer().append(a);
           f = f.reverse();
           a = f.toString();
           System.out.println(Integer.valueOf(a));
        }
    }
    public static void main(String[] args) {
        word();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_36416680/article/details/80627200