【藍橋杯】迴文數字
阿新 • • 發佈:2019-01-28
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <math.h>
using namespace std;
/*
*【篩選特殊迴文數字】
*步驟1:求出迴文數(注意求迴文數的方法,方法low了很耗時)
*步驟2:判斷是否滿足條件
*/
int main()
{
int n;
while (cin>>n)
{
vector<int>v;
for (int i = 100; i <= 999;i++)
{
int x1 = i,x2=i/10;
stringstream ss,ss1,ss2; string num,num1,num2;
ss << i; ss >> num;
ss2 << x2; ss2 >> num2; reverse(num2.begin(), num2.end());//5位
ss1 << x1; ss1 >> num1; reverse(num1.begin(),num1.end());//6位
string x = num + num1;
int sum = 0; int xx = 0;
for (int j = 0; j < x.length();j++)
{
sum += x[j] - '0';
xx += (x[j] - '0')*pow(10,(x.length() - 1 - j));
}
if (sum == n)v.push_back(xx);
x = num + num2; xx = 0 ; sum = 0;
for (int j = 0; j < x.length(); j++)
{
sum += x[j] - '0';
xx += (x[j] - '0')*pow(10, (x.length() - 1 - j));
}
if (sum == n)v.push_back(xx);
}
if (v.size() == 0)cout << -1 << endl;
else
{
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
cout << v[i] << endl;
}
}
return 0;
}