解题思路:
注意事项:
参考代码:
#include <stdio.h> #include <string.h> struct student { char name[20]; char sex[20]; int age; int score; }; void input(struct student *stu, int n) { int i; for (i = 0; i < n; i++) { scanf("%s%s%d%d", stu[i].name, stu[i].sex, &stu[i].age, &stu[i].score); } } void bubbleSort(struct student *stu, int n) { int i, j, a, s; char na[20], se[20]; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if ((stu + j)->score > (stu + j + 1)->score) { strcpy(na, (stu + j)->name); strcpy((stu + j)->name, (stu + j + 1)->name); strcpy((stu + j + 1)->name, na); strcpy(se, (stu + j)->sex); strcpy((stu + j)->sex, (stu + j + 1)->sex); strcpy((stu + j + 1)->sex, se); a = (stu + j)->age; (stu + j)->age = (stu + j + 1)->age; (stu + j + 1)->age = a; s = (stu + j)->score; (stu + j)->score = (stu + j + 1)->score; (stu + j + 1)->score = s; } } } } void output(struct student *stu, int n) { int i; for (i = 0; i < n; i++) { printf("%s %s %d %d\n", (stu + i)->name, (stu + i)->sex, (stu + i)->age, (stu + i)->score); } } int main() { struct student stu[1000]; int n, i; double sum = 0; scanf("%d", &n); input(stu, n); bubbleSort(stu, n); output(stu, n); return 0; }
0.0分
0 人评分
C语言训练-计算1~N之间所有奇数之和 (C语言代码)浏览:757 |
C语言程序设计教程(第三版)课后习题7.2 (Java代码)浏览:694 |
C语言训练-列出最简真分数序列* (C语言代码)浏览:543 |
兰顿蚂蚁 (C++代码)浏览:1225 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)万恶的long long浏览:907 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:631 |
【偶数求和】 (C语言代码)浏览:588 |
C语言考试练习题_一元二次方程 (C语言代码)浏览:606 |
C语言考试练习题_保留字母 (C语言代码)浏览:743 |
杨辉三角 (C语言代码)浏览:505 |