递归函数C排名

时间:2015-03-27 18:06:16

标签: c function recursion

#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.
请帮帮我。

2 个答案:

答案 0 :(得分:0)

这是查找数组中第二高整数的一种非常复杂的方法,但我认为问题出在这里。第二个最高值 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

答案 1 :(得分:0)

问题是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
}