#include <iostream> #include <string> #include <algorithm> using namespace std; int main(){ string x; cin >> x; if(x[0] == '-'){ //-123456 -> -654321 reverse(x.begin(), x.end()); string m = x.substr(0, x.length() - 1); cout << "-" + m << endl; } else{ reverse(x.begin(), x.end()); cout << x << endl; } return 0; }整数拆解
#include <iostream> #include <vector> using namespace std; int main(){ long long x; cin >> x; if(x == -0){ cout << "-" + to_string(0) << endl; return 0; } if(x >= 0){ long long sum = 0; vector<int> vec; while(x){ // 123456 vec.push_back(x % 10); x /= 10; } for(vector<int>::iterator it = vec.begin(); it != vec.end(); ++it){ sum = sum*10 + (*it); } cout << sum << endl; return 0; } else{ x *= -1; long long sum = 0; vector<int> vec; while(x){ // 123456 vec.push_back(x % 10); x /= 10; } for(vector<int>::iterator it = vec.begin(); it != vec.end(); ++it){ sum = sum*10 + (*it); } cout << sum * -1 << endl; return 0; } }
/* 利用StringBuffer的方法来插入 */ import java.util.Scanner; public class Main{//不知道为只有60% public static void main(String[] args){ Scanner input = new Scanner(System.in); String str = input.nextLine();//直接在输入的时候,我就将他看成字符串 StringBuffer sb = new StringBuffer(); StringBuffer sb1 = new StringBuffer();//用于存储翻转后的字符串 sb.append(str); //特殊情况当为-0的时候输出-0 if(sb.length()==2&&sb.charAt(0)=='-'&&sb.charAt(1)=='0'){ System.out.println(sb.toString()); return; } //如果开头为负号,则先存入 if(sb.charAt(0)=='-'){ sb1.append('-'); for(int i = sb.length()-1;i>0;i--) sb1.append(sb.charAt(i)); }else{ for(int i = sb.length()-1;i>=0;i--) sb1.append(sb.charAt(i)); } //StringBuffer转成String 在转成int System.out.println(Integer.parseInt(sb1.toString())); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] arr = str.split(""); if(arr[0].equals("-")){ System.out.print("-"); for(int i = arr.length-1; i > 0; i--){ System.out.print(arr[i]); } }else{ for(int i = arr.length-1; i >= 0; i--){ System.out.print(arr[i]); } } } }
const readline = require('readline') const rl = readline.createInterface({ input: process.stdin, ouput: process.stdout }) let inArr = [] rl.on('line',line=>{ if(!line) return inArr.push(line) if(inArr.length === 1){ let x = +inArr[0] //解法一 // let x = inArr[0].split('') // let f = '' // if(x[0] === '-'){ // f = '-' // x = x.slice(1) // } // let res = x.reverse() // res= f + res.join('') // console.log(res) console.log(reverse(x)) } }) //解法二 var reverse = function(x) { let arr = x.toString().split('') let res = parseInt(arr.reverse().join('')) // if(res>Math.pow(2,31)-1||-res<Math.pow(-2,31)) return 0 return x>0? res:-res };
#include <bits/stdc++.h> using namespace std; string F(string s){ string r; for(int i=s.length()-1;i>=0;i--) r += s[i]; return r; } int main(){ string s, r; while(cin>>s){ bool sign = false; if(s[0]=='-') r = '-' + F(s.substr(1)); else r = F(s); cout<<r<<endl; } return 0; }
import sys in_str = sys.stdin.readline().strip() if in_str[0] == '-': print('-'+in_str[1:][::-1]) else: print(in_str[::-1])
import java.util.*; public class Main{ public static void main(String[] args){ long qian; long hou=0; Scanner ss=new Scanner(System.in); String s=ss.nextLine(); int x=Integer.parseInt(s); if(x==-0){ System.out.println("-0"); return; } while(x!=0){ qian=x%10; x=x/10; hou=hou*10+qian; } System.out.println(hou); } }
#include<iostream> (720)#include<string> #include<algorithm> using namespace std; int main(void){ string s; cin>>s; string temp; string ans; if (s[0] == '-'){ temp = s.substr(1); reverse(temp.begin(), temp.end()); ans = "-" + temp; } else{ reverse(s.begin(), s.end()); ans = s; } cout<<ans<<endl; return 0; }