您的位置:首页 > 其它

蓝桥杯基础练习 特殊回文数

2018-03-07 21:08 225 查看
问题描述  123321是一个非常特殊的数,它从左边读和从右边读是一样的。
  输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。输入格式  输入一行,包含一个正整数n。输出格式  按从小到大的顺序输出满足条件的整数,每个整数占一行。样例输入52样例输出899998
989989
998899数据规模和约定  1<=n<=54。
第一反应:深搜 循环暴力枚举
后来看到另一个办法
参考http://blog.csdn.net/sr_19930829/article/details/18675565#include<iostream>;
using namespace std;
int main()
{
int n;
int sum;
while (cin >> n)
{
for (int i = 10000;i <= 999999;++i)
{
if (i <= 99999)
{
int temp = i;
int a, b, c, d, e;
a = temp % 10;
b = temp / 10 % 10;
d = temp / 1000 % 10;
c = temp / 100 % 10;
e = temp / 10000;
if (a == e&&b == d&&a + b + c + d + e == n)
cout << i << endl;
}
else
{
int temp = i;
int a, b, c, d, e, f;
a = temp % 10;
b = temp / 10 % 10;
c = temp / 100 % 10;
d = temp / 1000 % 10;
e = temp / 10000 % 10;
f = temp / 100000;
if (a == f&&b == e&&c == d&&a + b + c + d + e + f == n)
cout << i << endl;
}
}
}
return 0;
}

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: