# # Blue Bridge Cup practice palindrome numbers

[Blue Bridge Cup] [previous questions] palindromic numbers

Here Insert Picture Description

Every 10%, to obtain a final number, a sum value of the defined requirements, with the last one taking over every sum * 10
The final sum is the result of i reverse order, pay attention: should every time i / 10, but not i change the value, so each time you start the cycle must first define a temp. As calculated sum.

#include<iostream>

using namespace std;

int main()
{
	int n;
	cin>>n;
	int flag = 1;
	for(int i = 10000; i < 999999; i++)
	{
		int num = 0;
		int sum = 0;
		int temp = i;
		while(temp > 0)
		{
			sum = sum + temp % 10;
			num = num * 10 + temp % 10;
			temp /= 10;
		}
		if(sum == n && num == i)
		{
			flag = 0;
			cout<<i<<endl;
		}
	}
	if(flag)
		cout<<"-1";
    return 0;
}

Published 145 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43476037/article/details/104064578
Recommended