蓝桥杯 1434 回文数字

题目描述

观察数字:12321,123321  都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的。这样的数字叫做:回文数字。

本题要求你找到一些5位或6位的十进制数字。满足如下要求:
该数字的各个数位之和等于输入的整数。

输入

一个正整数  n  (10< n< 100),  表示要求满足的数位和。

输出

若干行,每行包含一个满足要求的5位或6位整数。 
数字按从小到大的顺序排列。 
如果没有满足条件的,输出:-1 

样例输入

44 

样例输出

99899
499994
589985
598895
679976
688886
697796
769967
778877
787787
796697
859958
868868
877778
886688
895598
949949
958859
967769
976679
985589
994499

代码:

#include<iostream>

using namespace std;
int main() {
    int n,m,tag=0;
    cin >> n;

    if (n <= 45) {//有5位数的可能性
        for (int i = 10; i <= 99; i++) {
            int s = i / 10, g = i % 10, q = s + g;
            q = q * 2;
            if (n - q >= 0 && n - q <= 9) {
                cout << i << n - q << g<<s<<endl;
                tag++;
            }
            else continue;
        }//5位数字
        if (n % 2 == 0) {
            m = n / 2;//前三位数字相加的结果
            for (int i = 100; i <= 999; i++) {
                int g = i % 10, sh = (i / 10) % 10, b = i / 100;
                if (g + sh + b == m) {
                    tag++;
                    cout << i << g << sh << b<<endl;
                }
                else continue;
            }
        }
    }
    else {//只能有6位数的可能性
        if (n % 2 != 0)cout << "-1";
        else if (n % 2 == 0) {
            m = n / 2;//前三位数字相加的结果
            for (int i = 100; i <= 999; i++) {
                int g = i % 10, sh = (i / 10) % 10, b = i / 100;
                if (g + sh + b == m) {
                    tag++;
                    cout << i << g << sh << b<<endl;
                }
                else continue;
            }
        }
    }

    if (tag == 0)cout << "-1"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42082542/article/details/87898538