当前位置:网站首页>输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
2022-07-26 06:16:00 【枯木何日可逢春】
题目(软件精P109题12):
输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
代码实现:
/* 输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序 */
#include<iostream>
using namespace std;
const int size = 5;
typedef struct {
int id;
int score;
}Student;
void selectSort(Student* students, int n) {
for (int i = 0; i < n - 1; ++i) {
Student temp = students[i];
int loc = i;
for (int j = i + 1; j < n; ++j) {
if (students[j].score > students[loc].score) {
loc = j;
}
}
if (loc != i) {
students[i] = students[loc];
students[loc] = temp;
}
}
}
int main() {
Student students[size];
students[0] = {
0, 60};
students[1] = {
1, 70};
students[2] = {
2, 90};
students[3] = {
3, 30};
students[4] = {
4, 50};
selectSort(students, size);
for (int i = 0; i < size; ++i) {
cout << students[i].id << "\t" << students[i].score << endl;
}
return 0;
}
运行结果:

边栏推荐
- The time complexity of two recursive entries in a recursive function
- PG Vacuum 杂谈之 auto vacuum
- Introduction to three feasible schemes of grammatical generalization
- What is KVM? What is KVM virtual machine?
- 逆序打印链表
- 英语句式参考纯享版 - 定语从句
- 【Day_04 0421】进制转换
- How to divide the disks under the devices and drives in win10 new computer
- Print linked list in reverse order
- [highly available MySQL solution] centos7 configures MySQL master-slave replication
猜你喜欢

Niuke network: TOPK problem of additive sum between two ordinal groups

Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!

分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)
[day04_0421] C language multiple choice questions

2022年下半年系统集成项目管理工程师(软考中级)报名条件

Implementation of PHP multitask second timer

逆序打印链表

Print linked list in reverse order

VRRP protocol and experimental configuration

Do it yourself smart home: intelligent air conditioning control
随机推荐
Alibaba cloud OSS binding custom domain name
【pytorch】CNN实战-花朵种类识别
BPG笔记(四)
二叉树的前中后序遍历——本质(每个节点都是“根”节点)
Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!
WebAPI整理
Mysql45 talks about transaction isolation: why can't I see it after you change it?
A tool for quickly switching local host files -- switchhosts
What are the aspects of performance testing? What are the classification and testing methods?
[Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
Interview difficulties: difficulties in implementing distributed session, this is enough!
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Widget is everything, widget introduction
【无标题】
机械制造企业如何借助ERP系统,做好生产管理?
[day_020419] inverted string
【pytorch】微调技术
【Oracle SQL】计算同比与环比(列转行进行偏移)
【无标题】
CCTV dialogue ZTE: why must the database be in your own hands?