ACM_整数反转

整数反转

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

给定一个32位int型的整数,把这个整数反着输出,如123,输出321。反转过程中有可能会有溢出32位int的情况,则输出0.

Input:

输入包含多组测试数据,对于每组数据,输入一个int型整数。

Output:

对于每组数据,输出反转之后的答案。

Sample Input:

123
1230
-123

Sample Output:

321
321
-321
解题思路:将数据类型定义为64位long long类型,只要反转之后的值大于32位int最大值或者小于32位int最小值,则输出0,否则输出反转之后的值,水过!
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL maxn=(1LL<<31)-1;//获取int最大值和最小值
 5 const LL minn=1<<31;LL n,m;
 6 int main(){
 7     while(cin>>n){m=0;
 8         while(n){m=m*10+n%10;n/=10;}
 9         if(m>maxn||m<minn)cout<<0<<endl;//大于int最大值或者小于int的最小值,则输出0
10         else cout<<m<<endl;//否则输出反转之后的数字
11     }
12     return 0;
13 }

猜你喜欢

转载自www.cnblogs.com/acgoto/p/9220552.html