数字反转

问题 H: 数字反转

时间限制: 1 Sec  内存限制: 128 MB
提交: 10  解决: 3
[提交] [状态] [讨论版] [命题人:外部导入]

题目描述

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式: 
    1. 除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零; 
    2. 负号应该仍然处于数字最左侧。 

输入

输入一个整数N(-1,000,000,000≤N≤1,000,000,000)。 

输出

输出反转后的新数。 

样例输入 Copy

-380

样例输出 Copy

-83





 1 #include <iostream>
 2 #include <sstream>
 3 #include <algorithm>
 4 #include <string>
 5 using namespace std;
 6 int main(){
 7     int n;
 8     cin >> n;//读入数字 
 9     stringstream ss;//字符串流 
10     string s1;
11     ss << n;
12     ss >> s1;//将数字转化为字符串 
13     if(n < 0){
14         s1 = s1.substr(1, s1.size() - 1);//去除负号 
15     }
16     reverse(s1.begin(), s1.end());//将字符串反转 
17     const char *c = s1.c_str();//返回c型字符串 
18     if(n < 0){
19         n = atoi(c);//将字符串转化为数字 
20         n = -n;
21     }else{
22         n = atoi(c);
23     }
24     cout << n;
25     return 0;
26 }

 

 
posted @ 2019-03-20 08:51  kakaluotedj  阅读(966)  评论(0编辑  收藏  举报