太奇怪了

P1923 【深基9.例4】求第 k 小的数

lly0329 @ 2023-11-17 22:47:38

一个用不到的变量没赋值会改变分数

#include<stdio.h>

int main() {
    int n, k, i;
    scanf("%d %d", &n, &k);

    int a[5000];
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    for (i = 1; i <= k; i++) {
        int min = a[0], min_index = 0;
        for (int j = 1; j < n; j++) {
            if (a[j] < min) {
                min = a[j];
                min_index = j;
            }
        }
        a[min_index] = 100000000;
    }

    int min = a[0];
    int min_index=0;
    for (i = 1; i < n; i++) {
        if (a[i] < min) {
            min = a[i];
        }
    }

    printf("%d", min);

    return 0;
}

这个能有20分

#include<stdio.h>

int main() {
    int n, k, i;
    scanf("%d %d", &n, &k);

    int a[4999];
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    for (i = 1; i <= k; i++) {
        int min = a[0], min_index = 0;
        for (int j = 1; j < n; j++) {
            if (a[j] < min) {
                min = a[j];
                min_index = j;
            }
        }
        a[min_index] = 100000000;
    }

    int min = a[0];
    //int min_index=0;
    for (i = 1; i < n; i++) {
        if (a[i] < min) {
            min = a[i];
        }
    }

    printf("%d", min);

    return 0;
}

注释掉24行变成0分,我看后面也没用到min_index 啊怎么分数有区别


by Bramble_Marshall @ 2023-11-21 19:30:17

@lly0329 我试了,2种代码都是全RE,但我建议把min改成mini试一下


by lly0329 @ 2023-11-21 20:47:19

@Bramble_Marshall 你试试语言c++14(gcc9)关闭o2优化


by hexuchen @ 2023-11-25 11:33:50

@lly0329 你有用到啊


by Bramble_Marshall @ 2023-11-27 23:13:54

@lly0329 应该是min重名了,改成mini就全RE了

#include<stdio.h>

int main() {
    int n, k, i;
    scanf("%d %d", &n, &k);

    int a[5000];
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    for (i = 1; i <= k; i++) {
        int mini = a[0], min_index = 0;
        for (int j = 1; j < n; j++) {
            if (a[j] < mini) {
                mini = a[j];
                min_index = j;
            }
        }
        a[min_index] = 100000000;
    }

    int mini = a[0];
    int min_index=0;
    for (i = 1; i < n; i++) {
        if (a[i] < mini) {
            mini = a[i];
        }
    }

    printf("%d", mini);

    return 0;
}

by Bramble_Marshall @ 2024-07-29 20:39:15

@lly0329 未定义行为会导致未知的结果,可能是 WA,RE 等。编译器通常会假定你的程序不会出现未定义行为,因此出现开 O2 与不开 O2 代码行为不一致的情况。——OI wiki


|