top1编程
← 返回题目
题解

【基础】等比数

1 条题解

  • 0
    @ 2026-7-29 0:15:14
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    
    using namespace std;
    
    // 判断是否使用了1~9且每个数字只出现一次
    bool isValid(int a, int b, int c) {
        int digits[10] = {0}; // 用于记录每个数字出现的次数
        int nums[3] = {a, b, c};
    
        for (int i = 0; i < 3; ++i) {
            int num = nums[i];
            while (num > 0) {
                int d = num % 10;
                if (d == 0 || digits[d] > 0) return false; // 0不合法或者重复了
                digits[d] = 1;
                num /= 10;
            }
        }
    
        // 检查是否所有1~9都用到了
        for (int i = 1; i <= 9; ++i) {
            if (digits[i] == 0) return false;
        }
    
        return true;
    }
    
    int main() {
        for (int a = 123; a <= 333; ++a) { // 最大的a不能超过333,否则3*a就不是三位数了
            int b = 2 * a;
            int c = 3 * a;
            if (b > 999 || c > 999) continue; // 必须都是三位数
            if (isValid(a, b, c)) {
                cout << a << " " << b << " " << c << endl;
            }
        }
    
        return 0;
    }
    
    • 1