根据您提供的代码,我注意到可能是由于包含了 'StdAfx.h' 头文件导致程序没有输出结果。

请尝试将代码中的 #include 'StdAfx.h' 移除,并重新编译运行程序,看看是否能够正确输出最小值和最大值。

以下是修改后的代码示例:

#include <stdio.h>
#include <stdlib.h>

struct MinMax {
    int min;
    int max;
};

struct MinMax findMinMax(int arr[], int low, int high) {
    struct MinMax result, leftResult, rightResult;
    int mid;

    // 当只有一个元素时,最大值和最小值都是该元素本身
    if (low == high) {
        result.min = arr[low];
        result.max = arr[low];
        return result;
    }

    // 当有两个元素时,比较二者大小
    if (high == low + 1) {
        if (arr[low] < arr[high]) {
            result.min = arr[low];
            result.max = arr[high];
        }
        else {
            result.min = arr[high];
            result.max = arr[low];
        }
        return result;
    }

    // 分割数组并递归求解左子问题和右子问题
    mid = (low + high) / 2;
    leftResult = findMinMax(arr, low, mid);
    rightResult = findMinMax(arr, mid + 1, high);

    // 比较左子问题的最大值和右子问题的最大值
    if (leftResult.max > rightResult.max) {
        result.max = leftResult.max;
    }
    else {
        result.max = rightResult.max;
    }

    // 比较左子问题的最小值和右子问题的最小值
    if (leftResult.min < rightResult.min) {
        result.min = leftResult.min;
    }
    else {
        result.min = rightResult.min;
    }
    
    return result;
}

int main() {
    int arr[] = { 5, 3, 8, 2, 1, 7, 6, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    struct MinMax result = findMinMax(arr, 0, n - 1);

    printf("最小值:%d\n", result.min);
    printf("最大值:%d\n", result.max);

    return 0;
}

请尝试运行修改后的代码,并检查控制台窗口中的输出结果。

如果您仍有其他问题或需要进一步的帮助,请随时提问。

C语言代码无法输出结果?可能是`StdAfx.h`惹的祸!

原文地址: https://www.cveoy.top/t/topic/oM4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录