Lanqiao Cup Past Papers-Palindrome Numbers

Problem description:
Observation numbers: 12321 and 123321 have a common feature, whether they are read from left to right or from right to left, they are the same. Such numbers are called: palindrome numbers.
This question requires you to find some 5-digit or 6-digit decimal numbers. Meet the following requirements:
the sum of the digits of the number is equal to the input integer.

Input format:
a positive integer n (10< n< 100), which represents the sum of digits required to be met.

Output format:
Several lines, each line contains a 5-digit or 6-digit integer that meets the requirements.
The numbers are arranged in ascending order.
If the conditions are not met, output: -1

Sample input:
44

Sample output:
99899
499994
589985
598895
679976
688886
697796
769967
778877
787787
796697
859958
868868
877778
886688
895598
949949
958859
967769
976679
985589
994499

Sample input:
60

Sample output:
-1

c Reference code 1:

#include <stdio.h>

int n;

int main()
{
    
    
	scanf("%d",&n);
	
	int i,flag=0;
	
	for(i=10000;i<=999999;i++)
  {
    
    
		int sum=0;
		int t=i,k=0;
		while(t!=0)
		{
    
    
			k=k*10+t%10;
			sum+=t%10;
			t=t/10;
		}

	if(k==i&&sum==n)
	{
    
    
		printf("%d\n",i);
		flag=1;
	}
 }
		
	if(flag==0)
	 printf("-1");
	return 0;
} 

Attach a link to this method to judge the number of palindrome:
poke me, hard work!

c Reference code 2:

#include <stdio.h>

int n;
int a[10];

int main()
{
    
    
	scanf("%d",&n);
	
	int i,flag=0;
	
	for(i=10000;i<=999999;i++)
  {
    
    
		int sum=0;
		int t=i,j=0,k1,k2;
		while(t!=0)
		{
    
    
			a[j++]=t%10; 
			sum+=t%10;
			t=t/10;
		}
       
       for(k1=0,k2=j-1;k1<=k2;k1++,k2--)
       {
    
    
       	    if(a[k1]!=a[k2])
       	    {
    
    
       	    	break;
			}
	   }
	 
	 if(k1>k2&&sum==n)
	 {
    
    
		printf("%d\n",i);
		flag=1;
	 }
 }
		
	if(flag==0)
	 printf("-1");
	return 0;
} 

Attach this method to determine the number of palindrome links:
portal

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/115218978