new
This commit is contained in:
parent
8dc01fc60e
commit
4c63bf22af
|
@ -0,0 +1,88 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define MIN(i, j) (((i) < (j)) ? (i) : (j))
|
||||
|
||||
struct student {
|
||||
int id, c;
|
||||
float x, y, z;
|
||||
float sum;
|
||||
char name[233];
|
||||
} stu[4];
|
||||
|
||||
int cmp(char s1[], char s2[]) {
|
||||
int l1 = strlen(s1), l2 = strlen(s2);
|
||||
for (int i = 0; i < MIN(l1, l2); i++) {
|
||||
if (s1[i] > s2[i])
|
||||
return 1;
|
||||
else if (s1[i] < s2[i])
|
||||
return -1;
|
||||
}
|
||||
if (l1 == l2)
|
||||
return 0;
|
||||
else if (l1 > l2)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
void swap(struct student* a, struct student* b) {
|
||||
struct student c;
|
||||
c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
void sort() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4 - i - 1; j++) {
|
||||
if (stu[j].c > stu[j + 1].c || (stu[j].c == stu[j + 1].c && cmp(stu[j].name, stu[j + 1].name) > 0)) {
|
||||
swap(&stu[j], &stu[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float sum(int i) {
|
||||
return stu[i].x + stu[i].y + stu[i].z;
|
||||
}
|
||||
|
||||
int main() {
|
||||
stu[0].c = 11;
|
||||
stu[0].id = 1001;
|
||||
stu[0].x = 92.5;
|
||||
stu[0].y = 82.5;
|
||||
stu[0].z = 96;
|
||||
strcpy(stu[0].name, "Zhao");
|
||||
|
||||
stu[1].c = 12;
|
||||
stu[1].id = 1002;
|
||||
stu[1].x = 82.5;
|
||||
stu[1].y = 87.5;
|
||||
stu[1].z = 93.5;
|
||||
strcpy(stu[1].name, "Qian");
|
||||
|
||||
stu[2].c = 13;
|
||||
stu[2].id = 1003;
|
||||
stu[2].x = 97;
|
||||
stu[2].y = 84.5;
|
||||
stu[2].z = 88.5;
|
||||
strcpy(stu[2].name, "Sun");
|
||||
|
||||
stu[3].c = 12;
|
||||
stu[3].id = 1004;
|
||||
stu[3].x = 95.8;
|
||||
stu[3].y = 85.6;
|
||||
stu[3].z = 74.9;
|
||||
strcpy(stu[3].name, "Li");
|
||||
|
||||
stu[0].sum = sum(0);
|
||||
stu[1].sum = sum(1);
|
||||
stu[2].sum = sum(2);
|
||||
stu[3].sum = sum(3);
|
||||
|
||||
sort();
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
printf("%d,%d,%s,%.1f,%.1f,%.1f,%.1f\n", stu[i].id, stu[i].c,stu[i].name, stu[i].x, stu[i].y, stu[i].z, stu[i].sum);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int p[5];
|
||||
|
||||
int sum(int* p, int n) {
|
||||
int ans = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
ans += p[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
scanf("%d", &p[i]);
|
||||
}
|
||||
printf("%d", sum(p, 5));
|
||||
}
|
Loading…
Reference in New Issue