[NOIP2011 Popularity Group] Digital Reversal

Topic link

Title description
Given an integer, please invert the digits of the number to get a new number. The new number should also satisfy the common form of integers, that is, unless the original number is given as zero, the highest digit of the new number obtained after inversion should not be zero (see Example 2).

Input format
An integer N

Output format
An integer representing the new number after inversion.

Sample input and output

Input #1
123
Output #1
321
Input #2
-380
Output #2
-83

Code:

//P1307 数字反转
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    
    
	char s[15];
	cin >> s;
	int len = strlen(s), flag = 0;
	if(len == 1) cout << s << endl;
	else if(s[0] != '-')
	{
    
    
		for(int i = len - 1; i >= 0; i--)
		{
    
    
			if(flag == 0 && s[i] == '0') continue;
			cout << s[i];
			flag = 1;
		}
	}
	else if(s[0] == '-')
	{
    
    
		cout << "-";
		for(int i = len - 1; i > 0; i--)
		{
    
    
			if(flag == 0 && s[i] == '0') continue;
			cout << s[i];
			flag = 1;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113746505