【发布时间】: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);Castingmalloc只会插入潜在的错误并使调试更加困难。 并且..如另一篇文章所述,每次有人施放malloc时,知更鸟就会死去!(不要杀死知更鸟..)