diff --git a/semester1/pset14/1-1-swap.c b/semester1/pset14/1-1-swap.c new file mode 100644 index 0000000..119357e --- /dev/null +++ b/semester1/pset14/1-1-swap.c @@ -0,0 +1,37 @@ +#include + +typedef struct { + char id[16]; //学生账号 + int total; //综合成绩 + int ce; //机试成绩 + int ws; //加权成绩 +} STUDENT; + +void Sort(STUDENT a[], int size); +void Swap(STUDENT *s1, STUDENT *s2); +int Comp(STUDENT *s1, STUDENT *s2); + +int main() { + STUDENT stu[100]; + int i, n; + + scanf("%d", &n); + for (i = 0; i < n; i++) { + scanf("%s%d%d", stu[i].id, &stu[i].ce, &stu[i].ws); + stu[i].total = stu[i].ce + stu[i].ws; + } + + Sort(stu, n); + for (i = 0; i < n; i++) + printf("%s %d %d %d\n", stu[i].id, stu[i].total, stu[i].ce, stu[i].ws); + + return 0; +} + +/* 请在这里填写答案 */ +void Swap(STUDENT *s1, STUDENT *s2) { + STUDENT tmp = *s1; + *s1 = *s2; + *s2 = tmp; + return; +} diff --git a/semester1/pset14/1-2-comp.c b/semester1/pset14/1-2-comp.c new file mode 100644 index 0000000..79b519f --- /dev/null +++ b/semester1/pset14/1-2-comp.c @@ -0,0 +1,43 @@ +#include + +typedef struct { + char id[16]; //学生账号 + int total; //综合成绩 + int ce; //机试成绩 + int ws; //加权成绩 +} STUDENT; + +void Sort(STUDENT a[], int size); +void Swap(STUDENT *s1, STUDENT *s2); +int Comp(STUDENT *s1, STUDENT *s2); + +int main() { + STUDENT stu[100]; + int i, n; + + scanf("%d", &n); + for (i = 0; i < n; i++) { + scanf("%s%d%d", stu[i].id, &stu[i].ce, &stu[i].ws); + stu[i].total = stu[i].ce + stu[i].ws; + } + + Sort(stu, n); + for (i = 0; i < n; i++) + printf("%s %d %d %d\n", stu[i].id, stu[i].total, stu[i].ce, stu[i].ws); + + return 0; +} + +/* 请在这里填写答案 */ +int Comp(STUDENT *s1, STUDENT *s2) { + if (s1->total > s2->total) { + return 1; + } else if (s1->total == s2->total) { + if (s1->ce > s2->ce) { + return 1; + } else + return 0; + } else { + return 0; + } +} diff --git a/semester1/pset14/1-3-sort.c b/semester1/pset14/1-3-sort.c new file mode 100644 index 0000000..809a775 --- /dev/null +++ b/semester1/pset14/1-3-sort.c @@ -0,0 +1,40 @@ +#include + +typedef struct { + char id[16]; //学生账号 + int total; //综合成绩 + int ce; //机试成绩 + int ws; //加权成绩 +} STUDENT; + +void Sort(STUDENT a[], int size); +void Swap(STUDENT *s1, STUDENT *s2); +int Comp(STUDENT *s1, STUDENT *s2); + +int main() { + STUDENT stu[100]; + int i, n; + + scanf("%d", &n); + for (i = 0; i < n; i++) { + scanf("%s%d%d", stu[i].id, &stu[i].ce, &stu[i].ws); + stu[i].total = stu[i].ce + stu[i].ws; + } + + Sort(stu, n); + for (i = 0; i < n; i++) + printf("%s %d %d %d\n", stu[i].id, stu[i].total, stu[i].ce, stu[i].ws); + + return 0; +} + +/* 请在这里填写答案 */ +void Sort(STUDENT a[], int size) { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size - 1; j++) { + if (!Comp(&a[j], &a[j + 1])) { + Swap(&a[j], &a[j + 1]); + } + } + } +} diff --git a/semester1/pset14/1-4-ArraySort.c b/semester1/pset14/1-4-ArraySort.c new file mode 100644 index 0000000..70ad95d --- /dev/null +++ b/semester1/pset14/1-4-ArraySort.c @@ -0,0 +1,64 @@ +#include +#include +#include + +#define MAX 101 + +char **create1(int n); +char *create2(int n); +void sort(char **strArray, int size); + +int main() { + char **strArray; + int n, i; + + scanf("%d", &n); + strArray = create1(n); + if (strArray != NULL) { + for (i = 0; i < n; i++) { + strArray[i] = create2(MAX); + if (strArray[i] == NULL) + return -1; + } + } else + return -1; //这里两个 return -1 都是无法获得内存时直接结束程序 + + getchar(); //吃掉前边输入的回车符 + + for (i = 0; i < n; i++) + fgets(strArray[i], MAX, stdin); //读入字符串 + + sort(strArray, n); //排序 + + for (i = 0; i < n; i++) + printf("%s\n", strArray[i]); //输出 + + for (i = 0; i < n; i++) + free(strArray[i]); + free(strArray); + + return 0; +} + +/* 请在这里填写答案 */ +char **create1(int n) { + char **tmp = malloc(sizeof(char *) * n); + return tmp; +} + +char *create2(int n) { + char *tmp = malloc(sizeof(char) * n); + return tmp; +} + +void sort(char **strArray, int size) { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size - 1; j++) { + if (strcmp(strArray[j], strArray[j + 1]) > 0) { + char *tmp = strArray[j]; + strArray[j] = strArray[j + 1]; + strArray[j + 1] = tmp; + } + } + } +}