输入包括一个整数n,(1 ≤ n ≤ 10^5)
输出一个整数,表示n的相反数
1325
6556
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(); } }
#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; }
语言: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;
}
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; } }
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)) });
#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; }
function rev(num){ let *** = num.toString(); *** = ***.split('').reverse().join(''); *** = ****1+num; return *** }
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)); } }
while(str = readline()){
var str1 = str.replace(/^([^0]+)(0+)$/, "$1")
.split("").reverse().join("");
console.log(+str + (+str1));
}
看了大佬们的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; }
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); } }