Это программа, которая пытается выяснить все отклонения массива {10, 11, 12, 13}
Но это не работает, потому что, когда дело доходит до depth==0, i==0
и Peano[]=={0, 1, 2, 3}
, для l oop не закончился, тогда я уменьшается до -1. Что, черт возьми, происходит?
#include<stdio.h>
#include<stdlib.h>
unsigned long long derange(int *array, int LEN, _Bool display, int *Peano, unsigned depth);
int main(void){
int i ,LEN=4, *Peano=malloc(sizeof(int));
int array[4]={10,11,12,13};
for(i=0; i<LEN; i++)Peano[i]=i;
derange(array, LEN, 1, Peano, 0);
printf("this derangement of the array total of %llu.\n", derange(array, LEN, 0, Peano, 0));
return 0;}
unsigned long long derange(int *array, int LEN, _Bool display, int *Peano, unsigned depth){
int i, temp;
unsigned long long count=0;
if(depth==LEN){
if(display){
for(i=0; i<LEN; i++)
fprintf(stdout, "%d\040", array[i]);
putchar('\n');}
return 1;}
for(i=LEN-1; i>=depth; i--){
if(i==Peano[depth])continue;
temp=Peano[i]; Peano[i]=Peano[depth]; Peano[depth]=temp;
temp=array[i]; array[i]=array[depth]; array[depth]=temp;
count += derange(array, LEN, display, Peano, depth+1);
temp=Peano[i]; Peano[i]=Peano[depth]; Peano[depth]=temp;
temp=array[i]; array[i]=array[depth]; array[depth]=temp;}
return count;}