题目描述
任意给你一个整数,这个数可能很大(最长不超过100位),你能求出它的逆转数吗?
逆转数定义如下:
1.一个末尾没有0的整数,它的逆转数就是各位数字逆序输出;
2.一个负数的逆转数仍是负数;
3.一个末尾有0的整数,它的逆转数如同下例:
reverse (1200) = 2100
reverse (-56) = -65
要求定义并使用如下函数:
void reverse(char *str)
{
//函数求出str的逆转数并存入str。
}
输入
输入一个长整数str,不超过100位,输入的整数不含前导0。
输出
输出str的逆转数。输出占一行。
样例输入 Copy
-123456789000
样例输出 Copy
-987654321000
源代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100000 + 10;
void reverse(char *str)
{
if(str[0] == '-')
{
int idx = strlen(str) - 1;
while(str[idx] == '0')idx -- ;
for(int i = 1,j = idx;i < j;i ++ ,j -- )
{
swap(str[i],str[j]);
}
}
else
{
int idx = strlen(str) - 1;
while(str[idx] == '0')idx -- ;
for(int i = 0,j = idx;i < j;i ++ ,j -- )
{
swap(str[i],str[j]);
}
}
}
char a[N];
int main()
{
cin >> a;
reverse(a);
cout << a;
return 0;
}