#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARGS_COUNT 15
#define ERROR_PARAMS -1
#define ILLEGAL_PARAMS -2
enum order {
UP, DOWN
};
* 排序
* or 排序方式
* arr 待排序的二维数组的二级指针
* len 一维数组的长度(依业务设计时一维数组的长度都一样)
*/
void sort(enum order const or, int const ** const arr, int const len) {
while (*(*(arr + 1) + len++) != '\0') {}
if (or == UP)
quickSort(0, len - 2, UP, arr, len);
else
quickSort(0, len - 2, DOWN, arr, len);
}
* 快速排序
* start 待排序的数组的元素的起始位置
* end 待排序的数组的元素的结束位置
* or 排序方式
* arr 待排序的二维数组的二级指针
* len 一维数组的长度(依业务设计时一维数组的长度都一样)
*/
void quickSort(int const start, int const end, enum order const or, int const ** const arr, int const len) {
if (start < end) {
int pivotPos = partition(start, end, or, arr);
quickSort(start, pivotPos - 1, or, arr, len);
quickSort(pivotPos + 1, end, or, arr, len);
}
}
* 快速排序的核心算法--分组(选取轴关键元素,后面的依次与轴关键元素比较,小的放它前面,大的的放它后面)
* low 待分组的数组的元素的起始位置
* high 待分组的数组的元素的结束位置
* or 排序方式
* arr 待排序的二维数组的二级指针
* return 该次分组完成后轴关键元素的位置
*/
int partition(int low, int high, enum order const or, int ** const arr) {
int pivot = *(*arr + low);
int pivotKey = *(*(arr + 1) + low);
int pivotKey_ = *(*(arr + 2) + low);
while (low < high) {
int i;
while (low < high &&
(or == UP ? *(*(arr + 1) + high) >= pivotKey : *(*(arr + 1) + high) <= pivotKey))
--high;
*(*arr + low) = *(*arr + high);
*(*(arr + 1) + low) = *(*(arr + 1) + high);
*(*(arr + 2) + low) = *(*(arr + 2) + high);
while (low < high &&
(or == UP ? *(*(arr + 1) + low) <= pivotKey : *(*(arr + 1) + low) >= pivotKey))
++low;
*(*arr + high) = *(*arr + low);
*(*(arr + 1) + high) = *(*(arr + 1) + low);
*(*(arr + 2) + high) = *(*(arr + 2) + low);
}
*(*arr + low) = pivot;
*(*(arr + 1) + low) = pivotKey;
*(*(arr + 2) + low) = pivotKey_;
return low;
}
* 校验输入参数,并将其转换成整形数组
* argvArr 转换后的整形数组
* argv 程序输入参数的字符串
* return 数据校验的结果,如果出错则返回-2,成功则返回0
*/
int checkArgs(int *const argvArr, char const * const argv) {
size_t len = strlen(argv);
int i;
char buf[32] = {};
char index = 0;
int count = 0;
for (i = 0; i < len; i++) {
char tmp = argv[i];
if (tmp == ',') {
*(buf+count) = '\0';
*(argvArr+index++) = atoi(buf);
count = 0;
} else if (tmp > 47 && tmp < 58) {
*(buf+count++) = tmp;
continue;
} else
return ILLEGAL_PARAMS;
}
*(buf+count) = '\0';
*(argvArr+index++) = atoi(buf);
return 0;
}
* 初始化各数据结构
* arr 待初始化的二维数组的二级指针
* count 该二维数组的一维数组的长度
* index 初始化动作所需输入(一维整形数组)的起始索引位置
* delta 初始化动作所需输入(一维整形数组)两个元素之间的偏移量
* params 初始化动作所需输入(一维整形数组)
* flag 是否需要初始化二维数组的第二位的第三个一维数组
*/
void init(int **const arr, int const count, int const index, int const delta, int const * const params, char const flag) {
int i;
for (i = 0; i < count; ++i) {
*(*arr + i) = i + 1;
*(*(arr + 1) + i) = *(params + index + i * delta);
if (flag)
*(*(arr + 2) + i) = *(params + index - 1 + i * delta);
}
}
* 调度算法
* tableNo 桌子相关信息的二维数组的二级指针
* taNo 桌子的数量
* teamNo 客人组相关信息的二维数组的二级指针
* teNo 客户组的数量
* return 当前最大盈利额
*/
int schedule(int **const tableNo, int const taNo, int **const teamNo, int const teNo) {
int i;
int profit = 0;
for (i = 0; i < teNo; ++i) {
int curTeamNo = *(*teamNo + i);
int curTeamPay = *(*(teamNo + 1) + i);
int curTeamLen = *(*(teamNo + 2) + i);
int j;
for (j = 0; j < taNo; ++j) {
if (*(*(tableNo + 2) + j) == 0 && *(*(tableNo + 1) + j) >= curTeamLen) {
*(*(tableNo + 2) + j) = i + 1;
profit += curTeamPay;
break;
}
}
}
return profit;
}
int main(int args, char **argv) {
if (args != 2) {
perror("please input the params with ',' spliting. ");
return ERROR_PARAMS;
}
int arr[ARGS_COUNT];
if (checkArgs(arr, *(argv + 1)) < 0) {
perror("please check your params with character of NaN. ");
return ILLEGAL_PARAMS;
}
int i;
int *tableNo[3];
*tableNo = (int *) malloc(arr[0] * sizeof(int));
*(tableNo + 1) = (int *) malloc(arr[0] * sizeof(int));
*(tableNo + 2) = (int *) calloc(arr[0], sizeof(int));
int *teamNo[3];
*teamNo = (int *) malloc(arr[0] * sizeof(int));
*(teamNo + 1) = (int *) malloc(arr[0] * sizeof(int));
*(teamNo + 2) = (int *) malloc(arr[0] * sizeof(int));
init(tableNo, arr[0], 2, 1, arr, 0);
init(teamNo, arr[1], 6, 2, arr, 1);
sort(UP, tableNo, arr[0]);
sort(DOWN, teamNo, arr[1]);
int ret = schedule(tableNo, arr[0], teamNo, arr[1]);
printf("max profit : %d\nscheduling plan:\n", ret);
for (i = 0; i < arr[0]; ++i) {
if (*(*(tableNo + 2) + i) != 0) {
int index = *(*(tableNo + 2) + i);
printf("table %d is disbursed to team %d with %d people and %d payment.\n",
*(*tableNo + i),
*(*teamNo + index - 1),
*(*(teamNo + 2) + index - 1),
*(*(teamNo + 1) + index - 1));
}
}
for (i = 0; i < arr[0]; ++i) {
free(*(tableNo + i));
free(*(teamNo + i));
}
return 0;
}