洛谷-P1307 数字反转

版权声明:版权归Ordinarv所有 https://blog.csdn.net/ordinarv/article/details/84758716

题目描述

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2)。

 


坑点,用数组存储时负数只需一个符号,还有去除前导零。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
void exchange(int n){
	if(n == 0) {printf("0\n");return; }
	int a[15];
	int b = n,cnt = 0;
	while(b){
		a[cnt++] = b%10;
		b/=10;
	}
	if(n<0) cout<<'-';
	int flag = 0;
	for(int i = 0;i < cnt;i++){
		if(a[i] != 0) flag = 1;
		if(flag) printf("%d",abs(a[i]));
	}
	printf("\n");
}
int main(){
	int n;
	while(~scanf("%d",&n))
		exchange(n);
}

Test_Data

1          1
0          0
666           666
5200         25
002500           52
40206030        3060204
999888777        777888999

猜你喜欢

转载自blog.csdn.net/ordinarv/article/details/84758716