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

最佳答案

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

关于c - 递归函数C排名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29307358/

10-12 05:30