[Blue Bridge Cup] 1434: Palindrome Numbers —> Three Methods of Judging Palindrome

Foreword:

Through the explanation of the palindrome numbers in the Lanqiao Cup Zhenti, three ways of judging palindrome are introduced: array or string , stack , and directly reversed numbers .


Table of contents

Foreword:

Blue Bridge Cup Topic: Palindromic Numbers

Topic analysis:

1. Array or string judgment palindrome:

2. Use the data structure of the stack to judge the palindrome:

3. Reverse the numbers directly


Blue Bridge Cup Topic: Palindromic Numbers

Observe the number: 12321, 123321 has a common feature, whether it is read from left to right or right to left, it is the same. Such numbers are called palindromic numbers.
This question asks you to find some 5- or 6-digit decimal numbers. Satisfy the following requirement:
the sum of each digit of this number is equal to the integer entered.


Topic analysis:

According to the requirements of the topic, we learned that the range of numbers we need to perform judgment operations is from five digits to six digits, that is, from 10000 to 999999, that is, we must first generate these numbers, and secondly, these numbers need to meet two conditions.

The first condition : the sum of digits is equal to the integer we input;

The second condition : palindrome.

So we implement the code based on the above analysis.

1. Array or string judgment palindrome:

int main()
{
	int n = 0;
	scanf("%d", &n);
	int num = 0;
	//生成数字
	for (num = 10000; num <= 999999; num++)
	{
		int tmp = num;
		int sum = 0;
		int count = 0;
		//计算位数之和
		while(tmp)
		{
			sum += tmp % 10;
			tmp /= 10;
			//统计位数
			count++;
		}
		//判断回文
		int arr[6] = { 0 };
		tmp = num;
		int i = 0;
		for (i = 0; i < count; i++)
		{
			arr[i] = tmp % 10;
			tmp /= 10;
		}
		int flag = 1;
		for (i = 0; i < count / 2; i++)
		{
			if (arr[i] != arr[count - i - 1])
				flag = 0;
		}
		//判断是否同时满足两个条件,并输出
		if (sum == n && flag==1)
		{
			printf("%d\n", num);
		}
	}
	return 0;
}

2. Use the data structure of the stack to judge the palindrome:

We know that the characteristic of the stack is first in and then out, that is, the elements that enter first come out, then judging the palindrome can just use this characteristic of the stack, after the general elements in the front are put on the stack, and then popped out of the stack in turn to compare with the half of the elements in the back, if they are the same, then it means that this string of characters is a palindrome character.

code show as below:

int main()
{
	int n = 0;
	scanf("%d", &n);
	int num = 0;
	//生成数字
	for (num = 10000; num <= 999999; num++)
	{
		int tmp = num;
		int sum = 0;
		int count = 0;

		//计算位数之和
		while (tmp)
		{
			sum += tmp % 10;
			tmp = tmp / 10;
			//统计位数
			count++;
		}
		int flag = 1;
		//判断回文
		SeqStack s;
		//将前一半字符入栈
		for (i = 0; i < count / 2; i++)
		{
			Push(s, str[i]);
		}
		if (count % 2) // 若输入字符为奇数个时自动跳过中间的字符
			i++;
		while (!EmptyStack(s))
		{
			Pop(s, e);
			if (e!= str[i])
			{
				flag = 0;
				break;
			}
			else
				i++;
		}
		//判断是否同时满足两个条件,并输出
		if (sum == n && flag == 1)
		{
			printf("%d\n", num);
		}
	}
	return 0;
}

3. Reverse the numbers directly:

The method of directly reversing numbers is a very ingenious method. His idea can be understood as the inverse process of taking each digit of a certain integer. We know that we only need to get each digit of a certain integer by %10 each time, and then /10. Then the method of directly inverting numbers is the inverse process of this process, and the code implementation is also very simple.

int main()
{
	int n;
	int flag = 0;
	scanf("%d", &n);
	int num = 0;
	for (num = 10000;  num <=999999;  num++)
	{
		//创建临时变量tmp,循环中利用tmp进行操作,不会影响num的值
		int tmp = num;
		int t = 0;
		int sum = 0;
		while (tmp)
		{
			//这里t计算得到的值就是该串字符反转后的值
			t = t * 10 + tmp % 10;
			sum += tmp % 10;
			tmp /= 10;
		}
		if (t == num && sum == n)
		{
			flag = 1;
			printf("%d\n", num);
		}
	}
	return 0;
}

The most important idea of ​​this code is   t = t * 10 + tmp % 10;

After mastering it, it is very simple to judge palindrome, and the time complexity and space complexity of this method are significantly improved compared with the previous two methods.

Guess you like

Origin blog.csdn.net/2301_77112634/article/details/131142637
Recommended