【蓝桥杯】回文数字
2019-11-14 11:04:47
供稿:网友
#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;}