【问题标题】:recursive function C ranking递归函数C排名
【发布时间】:2015-03-27 18:06:15
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

int cnt = 0; Count // global variable declaration

int find_max(int n, int arr[]); // (Recursive) function declaration circulation

int main() {

    // Insert code here ...

    int number; // Generate sequence number
    int * score; // Declare the game
    int i; // Loop variable
    int max; // Function return value

    scanf("% d \ n", &number); // Input (number of sequence)

    score = (int *)malloc(sizeof(int) * number); // Allocate memory scores


    for (i = 0; i < number; i++) {
        scanf("% d", &score[i]);
    } // Scores input

    max = find_max(number, score); // Recursive function call.

    printf("% d% d \ n", max, cnt); // Count value and second value, etc.


    return 0;
}


int find_max(int n, int arr[]) {

    int maxnum1 = 0; // Maximum value of the partial sequence 1
    int maxnum2 = 0; // Maximum value of the partial sequence 2
    int max = 0; // Maximum value
    int secondMax = 0; // 2 deunggap
    int * s1, *s2, sn1, sn2; // Memory allocation variables
    int i, j; // Loop variable

    cnt++; // If the sequence number is not zero and the count + 1.

    if (n == 1) {
        return arr[0]; // The number of returns a value of 1 when the sequence.

    }
    else if (n % 2 == 0) {// if even

        s1 = (int *)malloc(sizeof(int) * n / 2); // Split assignment

        for (i = 0; i < n / 2; i++) {
            s1[i] = arr[i];
        } // Where assigned sequences into storage

        sn1 = n / 2;

        s2 = (int *)malloc(sizeof(int) * n / 2); // Split assignment

        for (j = 0; j < n / 2; j++)
        {
            s2[j] = arr[i];
            i++;
        } // Where assigned sequences into storage

        sn2 = n / 2;

    }
    else {

        s1 = (int *)malloc(sizeof(int) * (n + 1) / 2); // Split assignment

        for (i = 0; i < ((n + 1) / 2); i++) {
            s1[i] = arr[i];
        } // Where assigned sequences into storage

        sn1 = ((n + 1) / 2);
        i = ((n + 1) / 2);

        s2 = (int *)malloc(sizeof(int) * (n - 1) / 2); // Split assignment

        for (j = 0; j < ((n - 1) / 2); j++)
        {
            s2[j] = arr[i];
            i++;
        } // Where assigned sequences into storage

        sn2 = ((n - 1) / 2);
    }




    maxnum1 = find_max(sn1, s1); // Partial recursive sequence maximum value twirl
    maxnum2 = find_max(sn2, s2); // Partial recursive sequence maximum value twirl





    for (i = 0; i < n; i++) {

        // If the value of the current index is greater than the maximum value
        if (arr[i] > = max) {
            // Sets the maximum value previously stored before the update of the maximum value.

            secondMax = max;
            // Maximum updates
            max = arr[i];


        }
        else if ((arr[i] > secondMax && arr[i] < max) || max == secondMax) {// if the value is greater than ten thousand and one memories of the calculated value max
            secondMax = arr[i];
        }
    }

    if (secondMax == 0) {
        return max;
    }
    else {
        return secondMax; // 2 deunggap return
    }
}

我将在c中使用递归函数,排名第二。不是第一个。 但是,输入和输出是

4
9 0 0 0
9(score) 7(recursive function count)

但是,输出是 9。我不想要这个结果。
不是第一,第二是0

正确的结果是 0 7。
我如何获得正确的结果 0 7.
请帮帮我。

【问题讨论】:

  • main() 中有malloc(),在递归函数中还有更多……但在任何地方都没有free()
  • 又一个罗宾死了...不要投出malloc的结果。 s1 = (int *)malloc(sizeof(int) * (n + 1) / 2); 应该是 s1 = malloc (sizeof *s1 * (n + 1) / 2); Casting malloc 只会插入潜在的错误并使调试更加困难。 并且..如另一篇文章所述,每次有人施放malloc 时,知更鸟就会死去!(不要杀死知更鸟..)

标签: c function recursion


【解决方案1】:

这是一种在数组中查找第二大整数的非常复杂的方法,但我认为问题就在这里。第二个最高值 0,但您将其丢弃,取而代之的是最高值。如果您已将 maxsecondMax 初始化为 -1(并测试 -1)就可以解决问题。

if (secondMax == 0) {
        return max;
    }
    else {
        return secondMax;
    }

这是一个更简单的方法:

#include<stdio.h>
#include<limits.h>

int main(void)
{
    int i, max1 = INT_MIN, max2 = INT_MIN;
    int score[] = { 9, 0, 0, 0 };
    int number = sizeof(score) / sizeof(score[0]);
    for (i=0; i<number; i++) {
        if (max1 < score[i])
            max1 = score[i];
        if (max2 < score[i] && max1 > score[i])
            max2 = score[i];
    }
    if (max2 == INT_MIN)
        max2 = max1;
    printf ("max1 = %d, max2 = %d\n", max1, max2);
    return 0;
}

程序输出:

max1 = 9, max2 = 0

【讨论】:

    【解决方案2】:

    问题是secondMaxmax 被初始化为您可以在数组中找到的数字。

    您可以通过使用数组中的第一个数字来初始化maxsecondMax,然后从数组的第二个数字开始循环来避免这个问题。你函数的最后一个循环可能是:

    max = arr[0];
    secondMax = max;
    for (i = 1; i < n; i++) {
    
        // If the value of the current index is greater than the maximum value
        if (arr[i] > = max) {
            // Sets the maximum value previously stored before the update of the maximum value.
            secondMax = max;
            // Maximum updates
            max = arr[i];
        }
        else if ((arr[i] > secondMax && arr[i] < max) || max == secondMax) {// if the value is greater than ten thousand and one memories of the calculated value max
            secondMax = arr[i];
        }
    }
    
    if (secondMax == max) {
        return max;
    }
    else {
        return secondMax; // 2 deunggap return
    }
    

    【讨论】:

    • 但是,如果第一个输入 "4" 和第二个输入 1 0 1 0 ,结果是 0(no​​t second..) 7 (count) 。正确的结果是 1 7 。
    猜你喜欢
    • 2020-09-24
    • 2011-07-21
    • 1970-01-01
    • 2016-02-13
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多