B4066 [GESP202412 三级] 数字替换

zhaoqiang1234 @ 2024-12-14 17:15:43


by tzlq_chenyixin @ 2024-12-14 17:16:37

wyy,jbl


by LMR_Minecraft @ 2024-12-14 17:27:38

#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> replaceNumbers(int n, int k, const std::vector<int>& sequence) {
    int max_value = *std::max_element(sequence.begin(), sequence.end());
    int min_value = *std::min_element(sequence.begin(), sequence.end());

    std::vector<int> replaced_sequence;
    for (int num : sequence) {
        if (num > k) {
            replaced_sequence.push_back(max_value);
        } else if (num < k) {
            replaced_sequence.push_back(min_value);
        } else {
            replaced_sequence.push_back(num);
        }
    }

    return replaced_sequence;
}

int main() {
    int n, k;
    std::cin >> n >> k;

    std::vector<int> sequence(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> sequence[i];
    }

    std::vector<int> result = replaceNumbers(n, k, sequence);

    for (int num : result) {
        std::cout << num << " ";
    }

    return 0;
}

by LMR_Minecraft @ 2024-12-14 17:28:00

@zhaoqiang1234 我过了,看我程序


by zhaoqiang1234 @ 2024-12-14 17:49:11

@LMR_Minecraft 牛逼


by Earth2023 @ 2024-12-14 18:28:03

@LMR_Minecraft太长了些,而且可以不用vector。

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
    int n, k, Min = 2147483647, Max = -9999999;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        Min = min(Min, a[i]);
        Max = max(Max, a[i]);
    }
    for (int i = 1; i <= n; i++) {
        if (a[i] < k) a[i] = Min;
        if (a[i] > k) a[i] = Max;
        cout << a[i] << ' ';
    }
    return 0;
}

by LMR_Minecraft @ 2024-12-15 11:05:30

@Earth2023有实力


by qxx13840998428 @ 2024-12-15 18:26:24


#include<bits/stdc++.h>
using namespace std;
const int A=1e5+1;
int n;
int k;
int arr[A];
int main(){
    cin>>n>>k;
    int mx=INT_MIN;
    int mi=INT_MAX; 
    for(int i=1;i<=n;i++){
        cin>>arr[i];
        mi=min(arr[i],mi);
        mx=max(arr[i],mx);
    }
    for(int i=1;i<=n;i++){
        if(arr[i]>k) arr[i]=mx;
        if(arr[i]<k) arr[i]=mi;
        cout<<arr[i]<<" ";
    }
}

by yyuny17 @ 2024-12-17 20:34:16

@Earth2023我觉得你可以在13~15行直接输出,把a[i]=MIN改成cout>>MIN;


by Earth2023 @ 2024-12-17 21:37:12

@yyuny17谢谢改进!


|