对于给定的整数N,输出其逆序数。
输入格式:
输入在一行中给出一个绝对值不超过10​9​​的整数N。
输出格式:
在一行中输出N的逆序数。
输入样例:
-12340

输出样例:
-4321

注:此函数任意整数的数字倒转,算是原题的升级版,故此不要直接粘贴复制到PTA上,可根据题目需要自行修改

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int jisuan(string n){
int num=n.length();
if(n[0]=='-') num-=1;
return num;
}
void fzs(string str,bool istrue){
bool pd=true;
int num=jisuan(str);
cout<<"总共有"<<num<<"位数\n";
if(str[0]=='-'){
pd=false;
for(int i=0;i<num/2;i++){//0123 3
char temp=str[i+1];
str[i+1]=str[num-i];
str[num-i]=temp;
}
}
else{
for(int i=0;i<num/2;i++){//0123 4
char temp=str[i];
str[i]=str[num-i-1];
str[num-i-1]=temp;
}
}
if(istrue==true)
{ //正数
if(pd==true)
{
//判断首位是否位0;
if(str[0]==0)
{
for(int i=1;i<num;i++)
{
cout<<str[i];
}
cout<<endl;
}
else cout<<str<<endl;
}
//负数,str[num+1]
else
{
//判断第二位位是否为0;
if(str[1]=='0')
{ cout<<"第二位为0\n";
cout<<str[0];
for(int i=2;i<num+1;i++)
{
cout<<str[i];
}
}
else
{ cout<<"第二位不为0\n";
cout<<str<<endl;
}
}
}

else
{
cout<<str<<endl;

}
}
int main(void){
string n;
//cin>>n;
cout<<"请输入需要逆转的整数: \n";
getline(cin,n);
int num=n.length();
cout<<"倒转后是否需要去掉前导为0的情况(如:0123去掉后为123)"<<endl;
cout<<"请输入: 1为 YES ,0 为 NO\n";
int ch;
cin>>ch;
if(ch==1) fzs(n,true);
else if(ch==0) fzs(n,false);
return 0;
}

演示:
在这里插入图片描述

在这里插入图片描述