[编程题]整数的倒数
• 热度指数：2540 时间限制：C/C++ 1秒，其他语言2秒 空间限制：C/C++ 32M，其他语言64M
• 算法知识视频讲解

输入描述:
`输入一个整数x`

`把x倒序输出`

`123`

`321`

`-123`

`-321`

`-0`

输出

`-0`
```""""

"""

if __name__ == "__main__":
s = input().strip()
if s[0] == '-':
print(s[0], end='')
s = s[1:]
ans = s[::-1]
print(ans)```

python

```num = input()
print("-" + num[1:][::-1] if "-" in num else num[::-1])```

```public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder builder = new StringBuilder(in.nextLine());
if (builder.charAt(0) == '-'){
builder.deleteCharAt(0);
builder.reverse();
builder.insert(0,'-');
}else
builder.reverse();
System.out.println(builder.toString());
}
}```

```#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
res=[]
if x[0]=='-':
res.append(x[0])
x.pop(0)
res.extend(x[::-1])
print(''.join(res))```

```nums = input()

if nums[0] == "-":
print(nums[0] + nums[1:][::-1])
else:
print(nums[::-1])```

```str_num = input()
if str_num[0] == "-":
new_str = "-"
for i in range(1, len(str_num)):
new_str += str_num[-i]
print(new_str)
else:
new_str = ""
for i in range(1, len(str_num)+1):
new_str += str_num[-i]
print(new_str)```

word = input()
print(word[::-1])
else:
word = word[1:]

```#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s;
cin>>s;
if(s[0] == '-')
reverse(s.begin()+1,s.end());
else
reverse(s.begin(),s.end());
cout<<s;
}```

import java.io.IOException;
import java.util.Arrays;
import java.util.Stack;
public class Main{
public static void main(String[] args) throws IOException{
char[] ch = str.toCharArray();
char[] ch1=new char[ch.length];
char[] ch2=new char[ch.length-1];
if(ch[0]!='-'){
for(int i =0;i<=ch.length-1;i++){
ch1[ch.length-1-i]=ch[i];
}
System.out.println(String.valueOf(ch1));
}else{
for(int i =1;i<=ch.length-1;i++){
ch2[ch.length-1-i]=ch[i];
}
System.out.println("-"+String.valueOf(ch2));
}
}
}

import java.util.*;
public class Main{
public static void main(String[] agrs){
Scanner sc=new Scanner(System.in);
String x=sc.nextLine();
char[] ch=x.toCharArray();
StringBuffer str=new StringBuffer();
if(ch[0]=='-'||ch[0]=='+'){
for(int i=1;i<ch.length;i++){
str.append(ch[i]);
}
str.append(ch[0]);
}else{
for(int i=0;i<ch.length;i++){
str.append(ch[i]);
}
}
System.out.println(str.reverse().toString());
}
}

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
char[] chars = string.toCharArray();
if(chars[0]=='+'||chars[0]=='-'){
System.out.print(chars[0]);
for(int j = chars.length-1;j>0;j--){
System.out.print(chars[j]);
}
}else{
for(int j = chars.length-1;j>=0;j--){
System.out.print(chars[j]);
}
}}

}

```x = input()
print('-'+x[1:][::-1] if x[0]=='-' else x[::-1])
```

```import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder sb = new StringBuilder(scanner.next());
if (sb.toString().charAt(0) == '-') {
System.out.println("-" + new StringBuilder(sb.substring(1)).reverse());
} else {
System.out.println(sb.reverse());
}
}
}```

``` public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
//Base case
//Corner case
if(input==0){
System.out.println("-"+input);
}else{
long res=0;
while(input!=0){
int digit=input%10;
res=res*10+digit;
input=input/10;
}
System.out.println(res);
}
}
```

17条回答 1595浏览

通过挑战的用户

• 2020-01-23 14:36:12
• 2020-01-23 09:00:06
• 2020-01-20 15:41:19
• 2020-01-16 22:59:12
• 2020-01-16 12:40:21

相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题