162. 排列所有組合

 avatar
user_6817964
c_cpp
2 years ago
636 B
3
Indexable
Never
void swap(int* x, int* y);
void permute(int* a, int l, int r, int n);


void swap(int* x, int* y) {
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}

void permute(int* a, int l, int r, int n) {
	if (l == r) {
		for (int i = 0; i < n; i++) {
			printf("%d ", a[i]);
		}
		printf("\n");
	}
	else {
		for (int i = l; i < n; i++) {
			swap(&a[i], &a[l]);
			permute(a, l + 1, r, n);
			swap(&a[i], &a[l]);
		}
	}
}



int main() {
	int n, i;
	scanf("%d", &n);
	int* a = (int*)malloc(n * sizeof(int));
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	permute(a, 0, n - 1, n);
	free(a);
	return 0;
}