1 条题解

  • 0
    @ 2026-1-29 19:05:26
    #include<bits/stdc++.h>
    using namespace std;
    
    // [1] 存储输入的n个正整数(数组大小满足n≤500的题目要求)
    int arr[510];
    // [2] 存储筛选出的奇数
    int odd[510];
    
    int main() {
        int n;
        cin >> n; // [3] 输入序列长度n
        // [4] 循环读取n个正整数并存储到数组
        for(int i = 0; i < n; i++) {
            cin >> arr[i];
        }
    
        int count = 0; // [5] 初始化奇数的个数为0
        // [6] 遍历输入数组,筛选出所有奇数
        for(int i = 0; i < n; i++) {
            if(arr[i] % 2 == 1) { // 判断是否为奇数
                odd[count++] = arr[i]; // 存入奇数数组并计数
            }
        }
    
        // [7] 对奇数数组进行升序排序
        sort(odd, odd + count);
    
        // [8] 输出排序后的奇数,用逗号分隔(避免末尾多余逗号)
        for(int i = 0; i < count; i++) {
            if(i > 0) {
                cout << ","; // 非第一个元素前输出逗号
            }
            cout << odd[i];
        }
        cout << endl;
    
        return 0;
    }
    
    • 1

    信息

    ID
    665
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    3
    已通过
    1
    上传者