首页 > 试题广场 >

相反数

[编程题]相反数
  • 热度指数:18422 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
为了得到一个数的"相反数",我们将这个数的数字顺序颠倒,然后再加上原先的数得到"相反数"。例如,为了得到1325的"相反数",首先我们将该数的数字顺序颠倒,我们得到5231,之后再加上原先的数,我们得到5231+1325=6556.如果颠倒之后的数字有前缀零,前缀零将会被忽略。例如n = 100, 颠倒之后是1.

输入描述:
输入包括一个整数n,(1 ≤ n ≤ 10^5)


输出描述:
输出一个整数,表示n的相反数
示例1

输入

1325

输出

6556

python solution:

a=input()
print(int(a)+int(a[::-1]))
发表于 2017-10-11 14:57:46 回复(12)


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		System.out.println(Integer.parseInt(new StringBuilder(s).reverse().toString())+Integer.parseInt(s));
		sc.close();
	}
}



编辑于 2017-09-14 15:11:48 回复(19)
水题。。。
#include <stdio.h>

int n;

void read() {
    scanf("%d", &n);
}

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

void work() {
    printf("%d\n", n + reverse(n));
}

int main() {
    read();
    work();
    return 0;
}

编辑于 2017-09-12 18:11:57 回复(2)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	string s,s1;
	cin >> s;
	s1 = s;
	reverse(s.begin(), s.end());
	int a = atoi(s.c_str());
	int b = atoi(s1.c_str());
	cout << a+b<< endl;
	return 0;
}

发表于 2017-09-10 11:01:36 回复(5)
import java.util.*;
public class Main{
  public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String num = sc.nextLine();
        
        StringBuffer sb = new StringBuffer(num);
        StringBuffer sbr = new StringBuffer(num).reverse();

        System.out.println(Integer.parseInt(sb.toString())+Integer.parseInt(sbr.toString()));
    }
}
//自己犯了个小错误,直接给原字符串reverse(),导致原字符串的值改变了。谨记。
发表于 2017-10-04 23:05:01 回复(0)
#include <iostream>
using namespace std;
 
intmain()
{
    intn;
    while(cin >> n)
    {
        intt = n;
        intr = 0;
        while(t)
        {
            r = r * 10+ t % 10;
            t /= 10;
        }
        cout << (r+n) << endl;
    }
    return0;
}

发表于 2017-09-12 11:54:12 回复(2)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int m=input.nextInt();
        int a=m;
        int b=0;
        while(m>0){
            b=b*10+m%10;
            m=m/10;
        }
        System.out.println(a+b);
    }
}

发表于 2018-03-21 10:10:35 回复(0)

语言:C++ 运行时间: 4 ms 占用内存:504K 状态:答案正确
水题。
本套8道题的C++代码已挂到了我的GitHub(https://github.com/shiqitao/NowCoder-Solutions)上,持续更新。

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    string n; cin >> n;
    string temp = n;
    reverse(n.begin(), n.end());
    cout << atoi(n.c_str()) + atoi(temp.c_str());
    return 0;
}
编辑于 2017-10-10 10:17:38 回复(0)
while(str = readline()){
    var n = parseInt(str);
    var strm = str.split('').reverse().join('').replace(/^[0]*/, '');
    var m = parseInt(strm);
    console.log(n+m);   
}

发表于 2017-09-11 21:39:05 回复(6)
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(true){
			System.out.println(getReverseNumber(sc.nextInt()));
		}
	}
	public static int getReverseNumber(int n){
		String temp=new Integer(n).toString();
		String reverse="";
		StringBuilder sb=new StringBuilder("");
		for(int i=temp.length()-1; i>=0; i--){
			sb.append(temp.charAt(i));
		}
		reverse=sb.toString();
		System.out.println("res"+reverse);
		int n2=Integer.parseInt(reverse);
		return n+n2;
	}
}
编辑于 2017-09-09 23:05:43 回复(0)
只需一行
思路:字符串变成数组,数组反转,数组拼接成字符串,字符串变数字相加
var readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});

rl.on('line', function(line) {
    console.log((+line.split('').reverse().join('')) + (+line))
});

编辑于 2017-09-12 14:34:32 回复(2)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int x, y, t;
	y = 0;
	scanf("%d", &x);
	t = x;
	while (x)
	{
		y = y * 10 + x % 10;
		x /= 10;
	}
	printf("%d\n", t + y);
        return 0;
}


一个数的颠倒我们可以通过%10来获取其个位数,例如1234%10=4
反之123想要得到1234,则需要123*10+1234%10
我们有的数据是1234,用户输入的数为x,颠倒数为y,则y为y*10+x%10
由于while循环会将x变为0,所以需要提前弄个“容器”来装载x的数据,或者使用全局变量也可以。
发表于 2020-08-12 11:45:37 回复(0)
function revNum (str) {
      return str.split('').reverse().join('')
    }
var line=readline();
print(parseInt(line)+parseInt(revNum(line)));


发表于 2019-08-19 20:31:58 回复(0)
function rev(num){
    let *** = num.toString();
    *** = ***.split('').reverse().join('');
    *** = ****1+num;
    return ***
}

发表于 2018-03-14 19:58:17 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String next = sc.next();
            String num = "";
            String[] split = next.split("");
            for (int i = split.length-1; i >= 0; i--) {
             num += split[i];
        }        
            System.out.println(Integer.parseInt(num)+Integer.parseInt(next));
        }
}

发表于 2018-01-29 16:42:51 回复(0)

JavaScript解法

while(str = readline()){
    var str1 = str.replace(/^([^0]+)(0+)$/, "$1")
                  .split("").reverse().join("");
    console.log(+str + (+str1));
}
编辑于 2017-10-26 16:42:19 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int n,i,len,j;
    while(scanf("%d",&n)!=EOF){
        char s[100];
        sprintf(s,"%d",n);
        len=strlen(s);
        for(i=0,j=len-1;i<j;i++,j--){
            char temp=s[i];
            s[i]=s[j];
            s[j]=temp;
        }
        printf("%d\n",n+(int)atof(s));
    }
}

发表于 2017-09-18 19:49:13 回复(0)
    a = raw_input()
    b = list(a)
    b.reverse()
    c = ''.join(b)
    print int(a)+int(c)
编辑于 2017-09-09 20:16:41 回复(0)

看了大佬们的reverse+atoi方法,自惭形秽。

//
// Created by jt on 2020/8/7.
//
#include <iostream>
#include <stack>

using namespace std;

int main() {
    int n;
    cin >> n;
    stack<int> saved;
    int shang = n / 10;
    int yushu = n % 10;
    saved.push(yushu);
    while (shang != 0) {
        yushu = shang % 10;
        shang = shang / 10;
        saved.push(yushu);
    }
    int tmpN = 0, cheng = 1;
    while (!saved.empty()) {
        int topVal = saved.top();
        saved.pop();
        tmpN += topVal * cheng;
        cheng *= 10;
    }
    cout << tmpN + n;
    return 0;
}
发表于 2020-08-07 14:45:33 回复(0)
import java.util.Scanner;
 
public class Main {
    public static void main(String args[]){
        Scanner in =  new Scanner(System.in);
        int initNum = in.nextInt();
        StringBuffer s = new StringBuffer(String.valueOf(initNum));
         
        int reverseNum = Integer.valueOf(s.reverse().toString());
         
        System.out.println(reverseNum+initNum);
    }
}

发表于 2020-04-12 15:01:20 回复(0)