我想检查是否可以使用回溯功能将array []
分成两个平均数相同的数组。
我在纸上写了类似tihs的字样,但它似乎不起作用
它告诉我countSum函数中的BadAcces
#define N 8
float countSum (int arr[],int n){
float sum = 0;
for (int i=0;i<n;i++){
sum+=arr[i];
}
return sum;
}
int sameAvgGroups(int arr[], int n1, int res[],int n2){
if ( (countSum(arr,n1)/n1) == (countSum(res,n2)/n2) || (n1<0) || (n2>=N) )
return n2;
for (int i=0;i<N;i++){
res[i] = arr[i];
sameAvgGroups(arr+i,N-i,res,i);
res[i] = 0;
}
return -1;
}
int main (){
int arr [N] = {5, 3, 12, 4, 12, 6, 1, -3};
int res[N] = {0};
printf("%d",(sameAvgGroups(arr,N,res,0)));
}