1102: 数字反转
1102: 数字反转
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 27 Solved: 6
[Submit][Status][Web Board]
Description
给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(如:输入-380,输出-83)。
Input
输入共1行,一个整数N。
Output
输出共1行,一个整数,表示反转后的新数。
Sample Input
123
Sample Output
321
HINT
-1,000,000,000<=N<=1,000,000,000。
NOIP2011 普及组 reverse
#include <iostream>
#include <string>
using
namespace
std;
int
main()
{
string str;
int
i, j;
cin >> str;
i = str.size() - 1;
while
(str[i] ==
'0'
)
i --;
if
(str[0] ==
'-'
)
{
cout <<
'-'
;
for
(j = i; j >= 1; j--)
cout << str[j];
}
else
{
for
(j = i; j >= 0; j--)
cout << str[j];
}
cout << endl;
return
0;
}
/**************************************************************
Problem: 1102
User: 1006440533
Language: C++
Result: Accepted
Time:0 ms
Memory:1272 kb
****************************************************************/
最后更新:2017-04-02 15:14:57