首页 > 试题广场 >

数字翻转

[编程题]数字翻转
  • 热度指数: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

Integer.parseInt(str)方***自动帮我们去除所有的前导0

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String x = sc.next();
            String y = sc.next();
            int xy = rev(x) + rev(y);
            System.out.println(rev(String.valueOf(xy)));
        }
        sc.close();
    }
    private static int rev(String s) {
        String str = new StringBuilder(s).reverse().toString();
        return Integer.parseInt(str);
    }
}
发表于 2017-08-29 12:43:00 回复(0)
将一个数字进行反转可以先转换成字符串获取该数字的长度,然后进行对应乘10反向进位
import java.util.Scanner;

public class Main {
	
	//数字反转
	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		int x = in.nextInt();
		int y = in.nextInt();
		String xstr = String.valueOf(x);
		String ystr = String.valueOf(y);
		x = Integer.valueOf(reverse(xstr));
		y = Integer.valueOf(reverse(ystr));
		
		int result = x + y;
		System.out.println(reverse(String.valueOf(result)));
	}
	
	public static String reverse(String num){
		
		int len = num.length();
		int ceil = 1;
		int rx = 0;
		for(int i=0; i<len; i++){
			rx += Integer.parseInt(num.charAt(i)+"") * ceil; 
			ceil *= 10;
		}
		return String.valueOf(rx);
	}
	
}

发表于 2017-08-31 13:39:28 回复(0)
难得的水题,直接依题意硬做就行
def reverse(x):
    res = 0
    while x:
        res = res * 10 + x % 10
        x //= 10
    return res

if __name__ == "__main__":
    x, y = map(int, input().split())
    print(reverse(reverse(x) + reverse(y)))

发表于 2022-04-11 15:22:16 回复(0)
function rev(num) {
    return Number(num.toString().split('').reverse().toString().replace(/,/g, ''));
}

while(line=readline()) {
    var lines = line.split(' '),
        x = parseInt(lines[0]),
        y = parseInt(lines[1]);
}

console.log(rev(rev(x) + rev(y)));

发表于 2019-08-19 22:14:50 回复(0)
x, y = list(input().strip().split())
def get_rev(s):
    return int(s[::-1])
print(get_rev(str(get_rev(x)+get_rev(y))))
发表于 2019-08-02 20:54:09 回复(0)
#include<stdio.h>
#include<math.h>
int rev(int x)
{
 int ws=0,x1,x2,i=0,j=0,sum=0,N=0;
 x1=x;
 x2=x1;
    //判断位数 
 while(x1>=1)
 {
  x1/=10;
  ws++;
 }
 //数组对应每个位数 
 int WS[ws];
 while(x2)
 {
  WS[i]=x2%10;
  x2/=10;
  i++;
 }
 while(!WS[j])
 {
  j++;
 }
 for(i=j;i<=ws-1;i++)
 {
  N=pow(10,ws-i-1);
  sum+=WS[i]*N;
 }
 return sum;
}
int main()
{
 int x,y,result1,result2,total=0;
 scanf("%d %d",&x,&y);
 result1=rev(x);
 result2=rev(y);
 total+=result1+result2;
 printf("%d",rev(total));
 return 0;
}

发表于 2018-09-27 11:10:12 回复(0)

a = input().split()
print(int(str(int(a[0][::-1]) + int(a[1][::-1]))[::-1]))

编辑于 2018-09-02 17:18:06 回复(0)

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int x=s.nextInt();
    int y=s.nextInt();
    System.out.print(rev(rev(x)+rev(y)));
}
public static int rev(int i)
{
    int a []=new int[20];
    int j=0;
    int radix=1;
    while(i/radix>0)
    {
        radix=radix*10;
    }
    radix=radix/10;
    int k=0;
    while(radix>0)
    {
        k=i/radix;
        a[j++]=k;
        i=i-radix*k;
        radix=radix/10;
    }
    String s="";
    for(int ii=a.length-1;ii>=0;ii--)
    {
        s=s+a[ii];
    }
    int num=Integer.parseInt(s);
    return num;
}

}

发表于 2018-08-26 11:16:40 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String []args) {
        Scanner sc = new Scanner(System.in);
        //System.out.println("请输入两个整数:");
        int x=sc.nextInt();
        int y=sc.nextInt();
        System.out.println(rever(rever(x)+rever(y)));
    }
    public static int rever(int x){
       StringBuilder sb=new StringBuilder(String.valueOf(x));
       String s=sb.reverse().toString();
       while(s.startsWith("0")){
           s=s.substring(1);
       }
       return Integer.parseInt(s);
    }
}

编辑于 2018-06-01 16:07:15 回复(0)
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int rev(int x)
{
    if(x<10)
        return x;
    else if(x>=10&&x<100)
    {
        x=(x%10)*10+(x/10)%10;
        return x;
    }
    else if(x>=100&&x<1000)
    {
        x=((x/100)%10)+((x/10)%10)*10+(x%10)*100;
        return x;
    }
    else
    {
        x=((x/1000)%10)+((x/100)%10)*10+((x/10)%10)*100+(x%10)*1000;
        return x;
    }
}
int main()
{
    int x,y;
    cin>>x>>y;
    cout<<rev(rev(x) + rev(y))<<endl;
    return 0;
}
发表于 2018-05-16 18:42:23 回复(1)
import sys
x,y=sys.stdin.readline().strip().split()
print(int(str(int(x[::-1])+int(y[::-1]))[::-1]))
发表于 2018-05-14 16:01:10 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x =input.nextInt();
        int y =input.nextInt();
        System.out.println(Rev(Rev(x)+Rev(y)));
    }

    public static int Rev(Integer number)
    {
        StringBuilder sb = new StringBuilder(String.valueOf(number));
        sb.reverse();
        return Integer.valueOf(sb.toString());
    }
}

发表于 2018-05-05 18:44:29 回复(0)

Python大法好
x,y = input().split(" ") add = int(x[::-1])+int(y[::-1]) add = str(add)[::-1] for i in range(0,len(add)):     if(add[i] == '0'):         i = i+1     break print(add[i:])
想了想感觉不用判定去首0,那样太束缚Python的灵活了,所以下面有个更简短版的:

x,y = input().split(" ")
add = int(x[::-1])+int(y[::-1])
print(int(str(add)[::-1]))

但是我好奇地对比了一下,第一种方法的运行时间和空间都要优于虽然看上去代码更简短的这种。


编辑于 2018-04-08 08:58:00 回复(0)
package LearnPackage;
//Java比较传统的做法之一

import java.util.Scanner;

public class Shuzifanzhuan {
    private static int rev(int a) {
        String str = Integer.toString(a);
        StringBuilder sb = new StringBuilder(str);
        StringBuilder str2 = sb.reverse();
        String s = str2.toString();
        char[] arr = s.toCharArray();
        int beginIndex = 0;
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] != 0) {
                beginIndex = i;
                break;
            }
        }
        String newS = String.valueOf(arr, 
                beginIndex, arr.length - beginIndex);
        int data = Integer.parseInt(newS);
        return data;
        
    }
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a, b, i;
        a = scan.nextInt();
        b = scan.nextInt();
        int c = rev(a);
        int d = rev(b);
        int out = c + d;
        System.out.println(rev(out));
    }

}

发表于 2018-03-29 13:22:24 回复(1)
#include <stdio.h>
int x,y;
int digit[5];
int mypow(int a,int x)
{
    int i;
    int mul=1;
    for(i=1;i<=x;i++)
        mul*=a;
    return mul;
}
int rev(int n)
{
    int i;
    int temp=n;
    int len=0;
    int revsum=0;
    while(temp!=0)
    {
        digit[len++]=temp%10;
        temp/=10;
    }
    for(i=0;i<len;i++)
        revsum+=(digit[i]*mypow(10,len-1-i));
    return revsum;
}
int main()
{
    int ret;
    scanf("%d %d",&x,&y);
    ret=rev(rev(x)+rev(y));
    printf("%d\n",ret);
    return 0;
}
发表于 2018-03-27 21:21:48 回复(0)
//利用StringBuilder自带的翻转函数,最后用Integer.parseInt()去前导0.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
String[] s = line.split(" ");
String s1 = new StringBuilder(s[0]).reverse().toString();
String s2 = new StringBuilder(s[1]).reverse().toString();
int x = Integer.parseInt(s1), y = Integer.parseInt(s2);
int z = x + y;
String res = new StringBuilder(String.valueOf(z)).reverse().toString();
System.out.println(Integer.parseInt(res)); 

发表于 2018-03-20 09:10:49 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
    int sum=0,flag=0;
    char a[5]={0},b[5]={0};
    scanf("%s %s",a,b);
    for (int i=strlen(a)-1;i>=0;i--)
    {
        flag*=10;
        flag+=a[i]-'0';
    }
    sum+=flag;
    flag=0;
    for (int i=strlen(b)-1;i>=0;i--)
    {
        flag*=10;
        flag+=b[i]-'0';
    }
    sum+=flag;
    for (int i=0;sum;i++)
    {
        if (sum%10==0&&i==0)
        goto a;
        printf("%d",sum%10);
        a:;
        sum/=10;
    }
}
发表于 2018-02-14 00:47:56 回复(0)
用好C++11,也很方便。
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    reverse(s1.begin(), s1.end());
    reverse(s2.begin(), s2.end());
    int n = atoi(s1.c_str()) + atoi(s2.c_str());
    s1 = to_string(n);
    reverse(s1.begin(), s1.end());
    cout << atoi(s1.c_str());
    return 0;
}

发表于 2018-02-10 18:39:50 回复(1)
package LineCode.Recruit2017.网易;

import java.util.Scanner;

/**
 * 题目描述
 对于一个整数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
 */
public class 数字反转 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            Integer x = sc.nextInt();
            Integer y = sc.nextInt();
            System.out.println(rev(rev(x) + rev(y)));
        }
    }

    private static int rev(int n) {
        return n < 10 ? n : n < 100 ? n%10*10 + n/10 : n < 1000 ?
                n%10*100 + n/10%10*10 + n/100 : n%10*1000 + n/10%10*100 + n/100%10*10 + n/1000;
    }
}
发表于 2018-01-08 23:55:56 回复(0)
#include <bits/stdc++.h>

using namespace std;

int rev(int n)
{     int sum = 0;     while(n)     {         sum = sum*10 + n%10;         n /= 10;     }     return sum;
}

int main()
{     int a,b,c;     while(cin>>a>>b)     {         c = rev(rev(a) + rev(b));         cout<<c<<endl;     }     return 0;
}

发表于 2018-01-01 00:57:11 回复(0)