首页 > 试题广场 >

数字翻转

[编程题]数字翻转
  • 热度指数:31128 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
对于一个整数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
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int x=sc.nextInt();
            int y=sc.nextInt();
            int n1=rev(x);
            int n2=rev(y);
            System.out.println(rev(n1+n2));
        }
    }
    
    public static int rev(int n){
        int x=0,tmp=n;
        while(n!=0){
            x=x*10+n%10;
            n/=10;
        }
        return x;
    }
}

发表于 2020-08-30 11:20:26 回复(0)
import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNextInt())
        {
            int a=scanner.nextInt();
            int b=scanner.nextInt();
            int c=rev(a)+rev(b);
            int d=rev(c);
            System.out.println(d);
        }
    }

    public static int rev(int n) 
    {

        String str=String.valueOf(n);
        StringBuilder sBuilder=new StringBuilder();
        for(int i=str.length()-1;i>=0;i--)
        {
            char s=str.charAt(i);
            sBuilder.append(""+s);
        }
        String str2=sBuilder.toString();
        int result=Integer.parseInt(str2);
        return result;
    }

}
发表于 2020-01-28 14:52:36 回复(0)
为啥都没考虑倒置越界 都能过呢?  整数有符号负数   int的取值范围为: -2^31——2^31-1,即-2147483648——2147483647    2147483647最末一位大于2越界 负值类似
发表于 2019-12-19 15:50:08 回复(0)

public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
while(reader.hasNext()){
int sum;
String str=reader.nextLine();
List<Integer> ilist=new ArrayList<>();
List<String> list = Arrays.stream(str.split(" ")).collect(Collectors.toList());
list.stream().forEach(item->{
item=new StringBuffer(item).reverse().toString();
ilist.add(Integer.valueOf(item));
});
sum = ilist.stream().mapToInt(i -> i).sum();
String strs=String.valueOf(sum);
System.out.println(Integer.valueOf(new StringBuffer(strs).reverse().toString()));
}
}
}

发表于 2018-11-02 10:18:53 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        int y = in.nextInt();
        System.out.println(rev(rev(x)+rev(y)));
    }
    public static int rev(int x){
        return Integer.parseInt(new StringBuilder(String.valueOf(x)).reverse().toString());
    }
}

发表于 2018-10-05 20:34:32 回复(0)
public class Main {

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        String n1 = scan.next();
        String n2 = scan.next();
        String s1="";
        String s2="";
        for (int i = n1.length()-1; i >=0; i--) {
            s1+=n1.charAt(i);            
        }
        for (int i = n2.length()-1; i >=0; i--) {
            s2+=n2.charAt(i);            
        }
        String sum=(Integer.parseInt(s1)+Integer.parseInt(s2))+"";
        s1="";
        for (int i = sum.length()-1; i >=0; i--) {
            s1+=sum.charAt(i);            
        }   
        System.out.println(Integer.parseInt(s1));
    }
}
发表于 2018-09-29 17:56:54 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();
        int sum = Integer.parseInt(new StringBuffer(s1).reverse().toString()) + Integer.parseInt(new StringBuffer(s2).reverse().toString());

        System.out.println(Integer.parseInt(new StringBuffer(String.valueOf(sum)).reverse().toString()));
    }
}
发表于 2018-08-08 15:18:13 回复(0)

import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
StringBuffer sbx=new StringBuffer();
sbx.append(x+"");
String strx=sbx.reverse().toString();
StringBuffer sby=new StringBuffer();
sby.append(y+"");
String stry=sby.reverse().toString();
int sum=Integer.parseInt(String.valueOf(strx))+Integer.parseInt(String.valueOf(stry));
StringBuffer sbsum=new StringBuffer();
sbsum.append(sum+"");
String s=sbsum.reverse().toString();
for(int i=0;i<s.length();i++) {
if(Integer.parseInt(String.valueOf(s.charAt(i)))!=0) {
System.out.println(s.substring(i,sbsum.reverse().toString().length()));
break;
}else {
continue;
}
}
}
}
大爱StringBuffer

发表于 2018-06-27 17:17:19 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        System.out.println(rev(rev(x) + rev(y)));
    }

    public static int rev(int n) {
        String s = String.valueOf(n);
        StringBuilder sb = new StringBuilder(s);
        sb = sb.reverse();
        for (int i = 0;i < sb.length();i ++) {
            if (sb.charAt(i) == '0') {
                sb.deleteCharAt(i);
            }else break;
        }
        return Integer.valueOf(sb.toString());
    }
} 

发表于 2018-05-24 21:25:16 回复(0)
代码比较多 ,但 容易理解、

import java.util.Scanner;

public class Main{     public static void main(String[] args) {         // TODO Auto-generated method stub                  //System.out.println("hello world");                  Scanner scanner=new Scanner(System.in);                  int a=scanner.nextInt();         int b=scanner.nextInt();                  String aString=String.valueOf(a);         String bString=String.valueOf(b);                  aString=reverse(aString);         bString=reverse(bString);                  int answer=Integer.valueOf(aString)+Integer.valueOf(bString);                  if(answer%10==0) {                          while (answer%10==0) {                 answer=answer/10;             }                      }                  String string=String.valueOf(answer);         string=reverse(string);                  System.out.println(string);                                }     private static String reverse(String aString) {         char c[]=aString.toCharArray();         int l=0,r=aString.length()-1;         char temp;                  while (l<=r) {             temp=c[l];             c[l]=c[r];             c[r]=temp;             l++;             r--;         }                                    return new String(c);     }

}

发表于 2018-04-11 20:08:44 回复(0)

数字翻转,余数和整除的应用

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        System.out.println(rev(rev(x) + rev(y)));
    }
    public static int rev(int x) {
        int result = 0; 
        while(x != 0) {
            result = x % 10 + result * 10;
            x = x / 10;
        }
        return result;
    }
}

用BufferedReader读取控制台输入,效率提高很多,以后不要用Scanner

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = br.readLine()) != null){
            String[] strs = str.trim().split(" ");
            int x = Integer.parseInt(strs[0]);
            int y = Integer.parseInt(strs[1]);
            System.out.println(rev(rev(x) + rev(y)));
        }
    }

    public static int rev(int x) {
        int result = 0; 
        while(x != 0) {
            result = x % 10 + result * 10;
            x = x / 10;
        }
        return result;
    }
}
编辑于 2018-03-14 15:41:34 回复(0)
import java.util.*;

public class Main{
    public static int rev(int x){
        while (x % 10 == 0){
            x /= 10;
        }
        String s = String.valueOf(x);
        int res = 0;
        int len = s.length();
        for (int i=0; i<len; i++){
            res = (int) (res + (s.charAt(i) - '0') * Math.pow(10, i));
        }
        return res;
    }
    public static int rev2(int x){
        int res = 0;
        while(x > 0){
           res = res * 10 + x % 10;
           x /= 10;
        }
        return res;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int X = sc.nextInt();
        int Y = sc.nextInt();
        System.out.println(rev2(rev2(X)+rev2(Y)));
    }
}

发表于 2018-03-03 12:17:48 回复(0)
 public static int rev(int a){
        while(a%10==0) a/=10;
        if(a==0) return 0;
        return Integer.parseInt(new StringBuilder(String.valueOf(a)).reverse().toString());
    }
发表于 2018-03-02 08:49:53 回复(0)
//解法一
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num1 = in.nextInt();
            int num2 = in.nextInt();
            System.out.println(reverse(reverse(num1) + reverse(num2)));
        }
    }
    private static int reverse(int num) {
        return Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString());
    }
}
-------------------------------------------------------------------------
//解法二
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num1 = in.nextInt();
            int num2 = in.nextInt();
            System.out.println(reverse(reverse(num1) + reverse(num2)));
        }
    }
    private static int reverse(int num) {
        int res = 0;
        while (num > 0) {
            res = res * 10 + num % 10;
            num /= 10;
        }
        return res;
    }
}

编辑于 2017-12-07 19:17:27 回复(0)
import java.util.Scanner;  public class Main
{  public static void main(String args[]) {
              Scanner scanner = new Scanner(System.in);  while (scanner.hasNext()) {
                     String[] xy = scanner.nextLine().split(" ");  int x = Integer.parseInt(xy[0]);  int y = Integer.parseInt(xy[1]);   System.out.println(rev(rev(x) + rev(y)));  }
    } private static int rev(int x) {
        StringBuffer sb = new StringBuffer();  while (x >= 10) {
              sb.append(x % 10);  x = x / 10;  }
        sb.append(x);  return Integer.parseInt(sb.toString());  }
}
这道题还是很简单,重要的是需要搞清楚那个反转函数的功能,感觉就还是把每个数字拿出来反转一下而已

发表于 2017-11-21 15:40:14 回复(0)

//数字反转
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
     Scanner scan = new Scanner(System.in);
    while(scan.hasNext()){
         int s = scan.nextInt();
         int t = scan.nextInt();
         String s1 = String.valueOf(s);
         String t1 = String.valueOf(t);
         StringBuilder s2 = new StringBuilder(s1);
         String s3 = s2.reverse().toString();
         int s4 = Integer.parseInt(s3);
         StringBuilder t2 = new StringBuilder(t1);
        String t3 = t2.reverse().toString();
        int t4 = Integer.parseInt(t3);
        int sum;
        sum = s4+t4;
        String sum1 = String.valueOf(sum);
        StringBuilder sum2 = new StringBuilder(sum1);
        String sum3 = sum2.reverse().toString();
        int sum4 = Integer.parseInt(sum3);
        System.out.println(sum4);
    }
      
}


}
发表于 2017-10-26 17:33:59 回复(0)
package niuke;

import java.util.Scanner;

public class T9 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String x = sc.next();
        String y = sc.next();
        int sum = rev(x) + rev(y);
        String s = sum + "";
        System.out.println(rev(s));
    }

    private static int rev(String y) {
        StringBuffer buf = new StringBuffer(y);
        y = buf.reverse().toString();
        return Integer.parseInt(y);
    }
}

发表于 2017-10-05 14:01:36 回复(0)
import java.util.Scanner;

public class Main {
    
    public static int rev(int x){
        int count=0;
        int sum=0,temp,number=0;
        do{
            sum=sum*10;
            temp = x/10;
            count=x-(temp)*10;
            sum=sum+count;
            x=temp;
        }while(x>0);    
        
        return sum;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        int x=in.nextInt();
        int y=in.nextInt();
        System.out.println(rev(rev(x) + rev(y)));
        
    }

}

发表于 2017-09-16 15:26:08 回复(2)