#include <stdlib.h>
int sum = 0;
void restSort(int* staffs_ptr,int staff_size,int* rest_staffs_ptr,int rest_staff_size);
void print(int* rest_ptr, int rest_size);
int main() {
int init_size = 12;
int *staffs_ptr = (int*)malloc(sizeof(int) * init_size);
if (staffs_ptr != NULL) {
for (int i = 0; i < init_size; i++) {
*(staffs_ptr + i) = i + 1;
}
restSort(staffs_ptr,init_size,NULL,0);
free(staffs_ptr);
printf("sum = %d\n",sum);
}
return 0;
}
void restSort(int* staffs_ptr, int staff_size, int* rest_staffs_ptr, int rest_staff_size) {
if (staff_size <= 2) {
if (rest_staffs_ptr != NULL && rest_staff_size > 0) {
int yet_rest_staff_size = rest_staff_size + staff_size;
int* yet_rest_staffs_ptr = (int*)malloc(sizeof(int) * yet_rest_staff_size);
if (yet_rest_staffs_ptr != NULL) {
for (int rest_index = 0; rest_index < rest_staff_size; rest_index++) {
yet_rest_staffs_ptr[rest_index] = rest_staffs_ptr[rest_index];
}
for (int index = rest_staff_size; index < yet_rest_staff_size; index++) {
yet_rest_staffs_ptr[index] = staffs_ptr[index - rest_staff_size];
}
print(yet_rest_staffs_ptr,yet_rest_staff_size);
free(yet_rest_staffs_ptr);
}
}
else {
print(staffs_ptr,staff_size);
}
return;
}
for (int i = 0; i < staff_size - 1; i++) {
for (int j = i + 1; j < staff_size; j++) {
int* residue_staff_ptr = (int*)malloc(sizeof(int) * (staff_size - 2));
if (residue_staff_ptr != NULL) {
int residue_size = 0;
for (int index = 0; index < staff_size; index++) {
if (index == i || index == j)
continue;
residue_staff_ptr[residue_size] = staffs_ptr[index];
residue_size++;
}
int yet_rest_staff_size = rest_staff_size + 2;
int* yet_rest_staffs_ptr = (int*)malloc(sizeof(int) * yet_rest_staff_size);
if (yet_rest_staffs_ptr != NULL) {
if (rest_staffs_ptr != NULL && rest_staff_size > 0) {
for (int rest_index = 0; rest_index < rest_staff_size; rest_index++) {
yet_rest_staffs_ptr[rest_index] = rest_staffs_ptr[rest_index];
}
}
yet_rest_staffs_ptr[rest_staff_size] = staffs_ptr[i];
yet_rest_staffs_ptr[rest_staff_size + 1] = staffs_ptr[j];
restSort(residue_staff_ptr, residue_size,yet_rest_staffs_ptr,yet_rest_staff_size);
free(yet_rest_staffs_ptr);
}
free(residue_staff_ptr);
}
}
}
}
void print(int* rest_ptr, int rest_size) {
for (int i = 1; i <= 7; i++) {
int day = 1;
for (int j = 0; j < rest_size;) {
if (day == i) {
printf("星期 %d: 無 ",day);
}
else {
printf("星期 %d: %d ",day,rest_ptr[j]);
j++;
if (j < rest_size) {
printf("%d ", rest_ptr[j]);
j++;
}
}
day++;
}
if (day == 7) {
printf("星期 7: 無");
}
printf("\n");
sum++;
}
}