zoukankan      html  css  js  c++  java
  • 2017网易---数字翻转

    题目描述

    对于一个整数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
    题目链接:https://www.nowcoder.com/practice/bc62febdd1034a73a62224affe8bddf2?tpId=85&tqId=29854&tPage=2&rp=1&ru=%2Fta%2F2017test&qru=%2Fta%2F2017test%2Fquestion-ranking

    法一:利用StringBuilder的reverse的函数,直接进行反转。代码如下(耗时12ms):
     1 package 数字翻转;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 
     7 public class Main {
     8 
     9     public static void main(String[] args) throws IOException {
    10         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    11         String line = in.readLine();
    12         String[] s = line.split(" ");
    13         String s1 = new StringBuilder(s[0]).reverse().toString();
    14         String s2 = new StringBuilder(s[1]).reverse().toString();
    15         int x = Integer.parseInt(s1), y = Integer.parseInt(s2);
    16         int z = x + y;
    17         String res = new StringBuilder(String.valueOf(z)).reverse().toString();
    18         System.out.println(Integer.parseInt(res));
    19     }
    20 
    21 }
    View Code

    法二:直接定义一个反转函数进行反转,反转可以利用反向计算来得到。代码如下(耗时7ms):

     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 public class Main {
     6 
     7     public static void main(String[] args) throws IOException {
     8         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     9         String line = in.readLine();
    10         String[] s = line.split(" ");
    11         int x = Integer.parseInt(s[0]), y = Integer.parseInt(s[1]);
    12         int res = rev(rev(x) + rev(y));
    13         System.out.println(res);
    14     }
    15         //翻转函数
    16     private static int rev(int num) {
    17         int res = 0;
    18         while(num != 0) {
    19             res = res * 10 + num % 10;
    20             num /= 10;
    21         }
    22         return res;
    23     }
    24 }
    View Code
  • 相关阅读:
    vmware下玩ubuntu总结
    .Net Json 字典序列化
    Flex Air TitleWindow 拖动范围控制
    TimesTen 问题荟萃
    TimesTen 时间戳(timestamp)用法
    批量数据插入 (.Net, ODBC)
    腾讯 360浏览器 调用js问题
    [转]Android项目源码混淆问题解决方法
    Intent调用大全
    View实现涂鸦、撤销以及重做功能【转】
  • 原文地址:https://www.cnblogs.com/cing/p/8607156.html
Copyright © 2011-2022 走看看