#include <stdio.h>
void ScanArray (int* , int*);
int Pair (int* , int);
int main () {
int a [15] , n;
ScanArray (a , &n);
// printf("\nHello World !!!\n");
if(Pair(a , n) == 0)
printf("The array fulfills the requirements");
else
printf("The array does not fulfills the requirements");
return 0;
}
void ScanArray (int *a , int *n) {
int i , f = 0;
do {
printf("Enter the number of integers : ");
scanf("%d",n);
if (*n > 15)
printf("Enter number bellow 15 \n");
else
f=1;
} while(f == 0);
printf("Enter the %d integers : ",*n);
for (i = 0 ; i < *n ; i++)
scanf("%d",a+i);
}
int Pair (int *a , int n) {
if (n <= 1)
return 0;
else
if (*a-*(a+1) != 1 && *a-*(a+1) != -1)
return 1;
else
return Pair(a++ , n--);
}
不知道为什么它不起作用。
段错误(核心转储)。
else
return Pair(a++ , n--);
使用后缀递增和递减运算符将导致递归调用处理相同的值。您应该使用前缀运算符,或者更好,只需加减 1。
else
return Pair(a + 1 , n - 1);
我说这样更好,因为认为修改值很重要是一种误导;递归的关键是递归调用将有它们自己的 a
和 n
副本,因此在父项中修改它们对子项没有影响。
我是一名优秀的程序员,十分优秀!