Add function problems

This commit is contained in:
juan 2021-12-16 15:04:08 +08:00
parent 66238736d8
commit 4d49180857
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,37 @@
#include <stdio.h>
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;
}

View file

@ -0,0 +1,43 @@
#include <stdio.h>
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;
}
}

View file

@ -0,0 +1,40 @@
#include <stdio.h>
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]);
}
}
}
}

View file

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
}
}
}