【问题标题】:getting the wrong output in program在程序中得到错误的输出
【发布时间】:2018-09-06 07:25:38
【问题描述】:

我最近写了一个程序,输出错误,我不知道为什么。

这个程序检查以下内容:给定一些'k'(值)和两个数组A和B,检查是否有一些'x'属于数组A 和 'y' 属于 B,所以 xk=y.

这是我的代码:

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

        int* buildArray(int size);
        int partition(int arr[], int low, int high);
        void quickSort(int *arr, int low, int high);
        int findValuesForDifference(int* A, int n, int* B, int m, int k);

        void main()
        {
            int n, m, k;
            int *A, *B;
            printf("please enter a number for the size of array A : ");
            scanf("%d", &n);
            printf("\nenter %d numbers for array A: ", n);
            A = buildArray(n);
            printf("please enter a number for the size of array B : ");
            scanf("%d", &m);
            printf("\nenter %d numbers for array A: ", m);
            B = buildArray(m);
            printf("\nplease enter a number for k: ");
            scanf("%d", &k);
            if (findValuesForDifference(A, n, B, m, k))
                printf("\nthere are some x which belongs to A and y which belongs to B such that x-y=k\n");
            else
                printf("\nthere are not any x which belongs to A and y which belongs to B such that x-y=k\n");

            free(B);
            free(A);
        }

        int findValuesForDifference(int* A, int n, int* B, int m, int k)
        {
            int low = 0, high = n - 1, middle, i;

            quickSort(A, low, high);

    /*using binary search sorted Array A, for each element of array B*/
            for (i = 0; i < m; i++)
            {
                while (low <= high)
                {
                    middle = (low + high) / 2;
                    if (k + B[i] == A[middle])
                        return 1;
                    else if (k + B[i] < A[middle])
                        high = middle - 1;
                    else
                        low = middle + 1;
                }
            }
            return 0;
        }

            int partition(int arr[], int low, int high)
            {
                int pivot = arr[high], i = (low - 1), j;

                for (j = low; j <= high - 1; j++)
                {

                    if (arr[j] <= pivot)
                    {
                        i++;    
                        swap(&arr[i], &arr[j]);
                    }
                }
                swap(&arr[i + 1], &arr[high]);
                return (i + 1);
            }

            void quickSort(int* arr, int low, int high)
            {
                int pivot;

                if (low < high)
                {
                    pivot = partition(arr, low, high);

                    quickSort(arr, low, pivot - 1);
                    quickSort(arr, pivot + 1, high);
                }
            }

           int* buildArray(int size)

           { int i; 
             int* arr = (int*)malloc(size * sizeof(int));       
             if (!arr) 
                 { printf("ERROR! Not enough memory!\n");  
                   exit(1); 
                 } 
             for (i = 0; i < size; i++) 
                  scanf("%d", &arr[i]); return arr; 
            }

对于大小为n=4 和元素:14 2 12 2 的数组A,以及大小为m=6 和元素:25 11 2 25 17 8 和k=3 的数组B, 我得到以下错误输出
there are not any x which belongs to A and y which belongs to B such that x-y=k,

而预期的输出是 there are some x which belongs to A and y which belongs to B such that x-y=k,因为 - 例如,有 14 个属于 A,11 个属于 B,因此 14-11=3。

【问题讨论】:

  • 这个程序比较大。你有没有试过检查他的中间结果,看看哪里出了问题?

标签: c arrays output


【解决方案1】:

在您的findValuesForDifference() 函数中,在定义这些变量时,您只需设置low 和high 的值一次。

你需要在主循环的每次迭代中重置它们的值,否则你的二分搜索只会工作一次:

int findValuesForDifference(int *A, int n, int *B, int m, int k)
{
    int low, high, middle, i;

    quickSort(A, low, high);

    /* using binary search sorted Array A, for each element of array B */
    for (i = 0; i < m; i++) {
        low = 0;
        high = n - 1
        while (low <= high) {
            middle = (low + high) / 2;
            if (k + B[i] == A[middle])
                return 1;
            else if (k + B[i] < A[middle])
                high = middle - 1;
            else
                low = middle + 1;
        }
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多