top1编程
← 返回题目
题解

【入门】数组查找及替换

1 条题解

  • 0
    @ 2026-7-29 0:17:50
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int n, b;
        cin >> n >> b;
    
        vector<int> arr;
        for (int i = 0; i < n; ++i) {
            int x;
            cin >> x;
            arr.push_back(x);
        }
    
        // 筛选:删除能被 b 整除的元素
        vector<int> filtered;
        for (int num : arr) {
            if (num % b != 0) {  // 不能被 b 整除才保留
                filtered.push_back(num);
            }
        }
    
        // 排序
        sort(filtered.begin(), filtered.end());
    
        // 输出:将 65~90 的数替换为对应字母,其余保持数字
        bool first = true;
        for (int x : filtered) {
            if (!first) cout << " ";
            first = false;
    
            if (x >= 65 && x <= 90) {
                cout << (char)x;
            } else {
                cout << x;
            }
        }
        cout << endl;
    
        return 0;
    }
    
    • 1