Loading

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

http://lx.lanqiao.cn/problem.page?gpid=T48

#include<iostream>
using namespace std;
int main()
{
    int sum;
    cin >> sum;
    int x1, x2, x3, x4, x5, x6;
    for (int i = 10000; i < 100000; i++)
    {
        x1 = i % 10;
        x2 = i / 10 % 10;
        x3 = i / 100 % 10;
        x4 = i / 1000 % 10;
        x5 = i / 10000;
        if (x1 == x5 && x2 == x4 && x1 + x2 + x3 + x4 + x5 == sum)
            cout << i << endl;
    }
    for (int i = 100000; i < 1000000; i++)
    {
        x1 = i % 10;
        x2 = i / 10 % 10;
        x3 = i / 100 % 10;
        x4 = i / 1000 % 10;
        x5 = i / 10000 % 10;
        x6 = i / 100000;
        if (x1 == x6 && x2 == x5
            && x3 == x4 && x1 + x2 + x3 + x4 + x5 + x6 == sum)
            cout << i << endl;
    }
    return 0;
}

本来以为直接遍历会超时,在想能不能直接利用回文数的特性“创造”回文数遍历,但我写了个暴力的试了一下,还真的可以,运行时间非常短,看来我还是小白,对算法不太了解哈哈哈

posted @ 2021-01-14 20:03  longwind7  阅读(54)  评论(0编辑  收藏  举报