C语言编程题求教,给定一个整型数组A和整数N,求A中N个数相加的和的最大值,要求递归实现

如题,有大神帮忙解答一下吗,急求!

第1个回答  2016-06-07
#include <stdio.h>
#include <stdlib.h>

void get_array_from(int a[], int b[], int num, int length)
{
int i = 0;
for(i=0; i<num; i++){
b[i] = 0;
}
if(length>=num){
for(i=0; i<num; i++){
b[i] = a[i];
}
}
return b;

}

int sum(int b[], int length){
int index = 0, sum = 0;
for(index=0; index<length; index++){
sum += b[index];
}
return sum;
}

void sort(int b[], int length){
int i=0, j=0, temp=0, temp_index = 0;
for(;i<length; i++)
{
j = i;
temp_index = i;
for(;j<length;j++){
if(b[temp_index]>b[j]){
temp_index = j;
}
}
if(temp_index != i){
temp = b[i];
b[i] = b[temp_index];
b[temp_index] = temp;
}
}
}

void iterator_get(int a[], int b[], int num, int a_length, int b_length){
if (a_length == num){
return;
}else{
sort(b, b_length);
if(b[0] < a[num]){
b[0] = a[num];
}
iterator_get(a, b, num+1, a_length, b_length);
}

}

int main()
{
int a[] = {3,5,8,1,2,9,23,35,21,38};
int a_length = sizeof(a)/sizeof(int);
int num;
printf("please input number:");
scanf("%d", &num);
int b[num];
get_array_from(a, b, num, a_length);
iterator_get(a, b, num, a_length, num);
printf("%d", sum(b, num));
return 0;
}本回答被提问者和网友采纳
相似回答