当前位置:网站首页>C language force buckle the flipped number of question 7. Violence Act

C language force buckle the flipped number of question 7. Violence Act

2022-07-25 00:15:00 Take care of two dogs and never let them go bad

To give you one 32 Signed integer of bit x , Return to x The result of reversing the number part in .

If the integer after inversion exceeds 32 The range of signed integers of bits [−231, 231 − 1] , Just go back to 0.
Suppose the environment doesn't allow storage 64 An integer ( With or without sign ).

Example 1:

Input :x = 123
Output :321

Example 2:

Input :x = -123
Output : - 321

Example 3:

Input :x = 120
Output :21

Example 4:

Input :x = 0
Output :0

source : Power button (LeetCode)
link :https ://leetcode.cn/problems/reverse-integer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

If overflow is not considered in this problem , It's very simple . There are two ways to solve the overflow problem , The first idea is to add... Through string conversion try catch To solve the problem , The second idea is to solve .
Because the efficiency of string conversion is low and more library functions are used , Therefore, this method is not considered in the problem-solving scheme , But through mathematical calculations to solve .
By looping numbers x Every one of them took apart , When calculating a new value, each step determines whether it overflows .
There are two overflow conditions , One is greater than the integer maximum MAX_VALUE, The other is less than the integer minimum MIN_VALUE, Let the current calculation result be ans, The next one is pop.
from ans * 10 + pop > MAX_VALUE In terms of this overflow condition

When there is a ans > MAX_VALUE / 10 And also pop Need to add when , It must overflow
When there is a ans == MAX_VALUE / 10 And pop > 7 when , It must overflow ,7 yes 2 ^ 31 - 1 The number of digits

from ans * 10 + pop < MIN_VALUE In terms of this overflow condition

     When there is a ans < MIN_VALUE / 10 And also pop Need to add when , It must overflow
     When there is a ans == MIN_VALUE / 10 And pop < -8 when , It must overflow ,8 yes - 2 ^ 31 The number of digits

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int test(int x)
{	
	int y = 0;
	int z = 0;
	int max = 2147483647;//int  Maximum 
	int min = -max - 1;
	//printf("%d\n", -max-1);
	//printf("%d\n", max);
	if (x == 0)
	{
		return x;
	}
	if (x == min)// because x Negative is one more than positive   It may cause that negative numbers cannot be de negative   List the last negative number 
	{
		return 0;
	}
	z = x;// Start with the first x Stored in the z in   In order to facilitate the subsequent classification discussion 
	while (x != 0)
	{
		if (x < 0)//x Less than 0 The situation of 
			x = -x;
		
		//printf("%d\n", x);
		y = y * 10 + x % 10;
		//printf("%d\n", y);
		x = x / 10;
		if (x/10 == 0 && x != 0)// Check for spillage 
		{
			if (z > 0)
			{
				if (y > max / 10 || ( y == max/10 && x > max % 10))
					return 0;
			}
			if (z < 0)
			{
				if ((-y)  < (-max - 1)/10 || ((-y)== (-max - 1) / 10 && (-x)< (-max - 1) % 10))
					return 0;
			}
		}

	}
	if (z < 0)
		y = -y;

	return y;
}

int main()
{
	int x = -2147483647;
	x = test(x);
	printf("%d", x);
	return 0;
}

原网站

版权声明
本文为[Take care of two dogs and never let them go bad]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207201442406417.html