【一次过】【2017网易】数字翻转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/majichen95/article/details/89162041

对于一个整数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

输出例子1:

223

解题思路:

考察数字反转,简单。

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int m = sc.nextInt();
            int n = sc.nextInt();
            
            System.out.println(rev(rev(m) + rev(n)));
        }
    }
    
    public static int rev(int n){
        Stack<Integer> stack = new Stack<>();
        if(n == 0)
            return 0;
        
        while(n != 0){
            stack.push(n%10);
            n = n/10;
        }
        
        int res = 0;
        int i = 0;
        while(!stack.isEmpty())
            res += stack.pop()*Math.pow(10, i++);
        
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/majichen95/article/details/89162041