卷王竟是我自己

This commit is contained in:
juan 2021-12-16 17:27:53 +08:00
parent 4d49180857
commit 53b7792b4e
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 377 additions and 0 deletions

View file

@ -0,0 +1,72 @@
#include <stdio.h>
#pragma GCC optimize("Ofast,inline")
#define MAX 21
typedef struct STUDENT {
char name[MAX];
char id[MAX];
int scores[5];
float avg;
int sum;
} STUDENT;
void sort(int *arr, int n);
int main(void) {
STUDENT dude;
// Get name
char ch;
int loc = 0;
while ((ch = getc(stdin)) != '\n') {
dude.name[loc++] = ch;
}
dude.name[loc] = '\0';
// Get id
loc = 0;
while ((ch = getc(stdin)) != '\n') {
dude.id[loc++] = ch;
}
dude.id[loc] = '\0';
// Get scores
int sum = 0;
for (int i = 0; i < 5; i++) {
scanf("%d", &dude.scores[i]);
sum += dude.scores[i];
}
// Populate avg and sum;
dude.sum = sum;
dude.avg = (float)sum / 5;
sort(dude.scores, 5);
// Print answer
printf("Name:%s\n", dude.name);
printf("ID:%s\n", dude.id);
printf("Score:");
for (int i = 0; i < 4; i++) {
printf("%d ", dude.scores[i]);
}
printf("%d\n", dude.scores[4]);
printf("average:%.2f total:%d\n", dude.avg, dude.sum);
return 0;
}
void sort(int *arr, int n) {
int tmp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if(arr[j] < arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}

View file

@ -0,0 +1,91 @@
#include <stdio.h>
#pragma GCC optimize("Ofast,inline")
#define MAX 21
typedef struct STUDENT {
char name[MAX];
char id[MAX];
int scores[5];
float avg;
int sum;
} STUDENT;
void sort(int *arr, int n);
void GetInput(STUDENT *dude);
void print(STUDENT dude);
int main(void) {
int n;
scanf("%d", &n);
STUDENT students[n];
for (int i = 0; i < n; i++) {
getc(stdin);
GetInput(&students[i]);
}
for (int i = 0; i < n; i++) {
print(students[i]);
printf("\n");
}
return 0;
}
void sort(int *arr, int n) {
int tmp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[j] < arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
void GetInput(STUDENT *dude) {
// Get name
char ch;
int loc = 0;
while ((ch = getc(stdin)) != '\n') {
dude->name[loc++] = ch;
}
dude->name[loc] = '\0';
// Get id
loc = 0;
while ((ch = getc(stdin)) != '\n') {
dude->id[loc++] = ch;
}
dude->id[loc] = '\0';
// Get scores
int sum = 0;
for (int i = 0; i < 5; i++) {
scanf("%d", &(dude->scores[i]));
sum += dude->scores[i];
}
// Populate avg and sum;
dude->sum = sum;
dude->avg = (float)sum / 5;
// We don't need to sort this time
/* sort(dude->scores, 5); */
}
void print(STUDENT dude) {
// Print answer
printf("Name:%s\n", dude.name);
printf("ID:%s\n", dude.id);
printf("Score:");
for (int i = 0; i < 4; i++) {
printf("%d ", dude.scores[i]);
}
printf("%d\n", dude.scores[4]);
printf("average:%.2f total:%d\n", dude.avg, dude.sum);
}

View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma GCC optimize("Ofast,inline")
#define MAX 21
typedef struct STUDENT {
char name[MAX];
int score;
} STUDENT;
void GetInput(STUDENT **dude);
void sort(STUDENT *students, int n);
void PrintStudent(STUDENT dude);
void swap(STUDENT *left, STUDENT *right);
int main(void) {
int n;
scanf("%d", &n);
STUDENT students[n];
for (int i = 0; i < n; i++) {
scanf("%s", students[i].name);
scanf("%d", &(students[i].score));
}
sort(students, n);
for (int i = 0; i < n; i++) {
printf("Name:%s\n", students[i].name);
printf("total:%d\n", students[i].score);
printf("\n");
}
return 0;
}
void sort(STUDENT *students, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (students[j].score < students[j + 1].score) {
swap(&students[j], &students[j + 1]);
} else if (students[j].score == students[j + 1].score) {
if (strcmp(students[j].name, students[j + 1].name) > 0) {
swap(&students[j], &students[j + 1]);
}
}
}
}
}
void PrintStudent(STUDENT dude) {
printf("Name:%s\n", dude.name);
printf("total:%d\n", dude.score);
}
void swap(STUDENT *left, STUDENT *right) {
STUDENT tmp;
// Swap name
strcpy(tmp.name, left->name);
strcpy(left->name, right->name);
strcpy(right->name, tmp.name);
// Swap score
int temp = left->score;
left->score = right->score;
right->score = temp;
}

View file

@ -0,0 +1,147 @@
#include <stdio.h>
#include <string.h>
#pragma GCC optimize("Ofast,inline")
#define MAX 11
#define CLASS 3
typedef struct STUDENT {
char name[MAX];
char id[MAX];
int scores[CLASS];
} STUDENT;
int parser(int operation, STUDENT *students, int size);
int match(char *id, STUDENT *students, int size);
// Returns a location of students if found, else return -1
int add(char *id, char *name, int *scores, STUDENT *students, int size); // 1
int delete (char *id, STUDENT *students, int size); // 2
void update(char *id, int *scores, STUDENT *students, int size); // 3
void display(char *id, STUDENT *students, int size); // 4
int main(void) {
int operations;
scanf("%d", &operations);
STUDENT students[operations];
int operation;
int size = 0;
for (int i = 0; i < operations; i++) {
scanf("%d", &operation);
size += parser(operation, students, size);
}
return 0;
}
int parser(int operation, STUDENT *students, int size) {
if (operation == 1) {
char id[MAX];
char name[MAX];
int scores[CLASS];
scanf("%s%s", id, name);
/* printf("id:%s, name:%s\n", id, name); */
for (int i = 0; i < CLASS; i++) {
scanf("%d", &scores[i]);
/* printf("Score[%d]: %d\n", i, scores[i]); */
}
return add(id, name, scores, students, size);
} else if (operation == 2) {
char id[MAX];
scanf("%s", id);
return delete (id, students, size);
} else if (operation == 3) {
char id[MAX];
int scores[CLASS];
scanf("%s", id);
for (int i = 0; i < CLASS; i++) {
scanf("%d", &scores[i]);
}
update(id, scores, students, size);
return 0;
} else {
char id[MAX];
scanf("%s", id);
display(id, students, size);
return 0;
}
}
int add(char *id, char *name, int *scores, STUDENT *students, int size) { // 1
// returns if Students already exist
if (match(id, students, size) != -1) {
printf("Students already exist\n");
return 0;
}
// Add data to the location "size"
strcpy(students[size].id, id);
strcpy(students[size].name, name);
for (int i = 0; i < CLASS; i++) {
students[size].scores[i] = scores[i];
}
printf("Add success\n");
return 1;
}
int delete (char *id, STUDENT *students, int size) { // 2
// Returns if student doesn't exist
int loc = match(id, students, size);
if (loc == -1) {
printf("Students do not exist\n");
return 0;
}
// Shift the array left.
for (int i = loc; i < size - 1; i++) {
students[i] = students[i + 1];
}
printf("Delete success\n");
return -1;
}
void update(char *id, int *scores, STUDENT *students, int size) { // 3
int loc = match(id, students, size);
if (loc == -1) {
printf("Students do not exist\n");
return;
}
// Update the student
for (int i = 0; i < CLASS; i++) {
students[loc].scores[i] = scores[i];
}
printf("Update success\n");
}
void display(char *id, STUDENT *students, int size) { // 4
int loc = match(id, students, size);
if (loc == -1) {
printf("Students do not exist\n");
return;
}
// Display the average score
int sum = 0;
for (int i = 0; i < CLASS; i++) {
/* printf("score[%d]: %d\n", i, students[loc].scores[i]); */
sum += students[loc].scores[i];
}
printf("Student ID:%s\nName:%s\nAverage Score:%.1f\n", id, students[loc].name,
(float)sum / CLASS);
}
int match(char *id, STUDENT *students, int size) {
for (int i = 0; i < size; i++) {
if (strcmp(id, students[i].id) == 0) { // matched
return i;
}
}
return -1;
}