竞赛班要出一张光荣榜,按成绩从高到低排,分数一样时按姓名字典序排。请你写一个 cmp 比较函数交给 sort 使用。
第一行一个整数 n(1 ≤ n ≤ 100);接下来 n 行,每行一个字符串 name 和一个整数 score(0 ≤ score ≤ 100)。
输出 n 行,每行「name score」,按成绩从高到低;成绩相同时按姓名从小到大。
3 xiaoming 92 xiaohong 92 xiaogang 88
xiaohong 92 xiaoming 92 xiaogang 88
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
int score;
};
bool cmp(Student a, Student b) {
if (a.score != b.score) return a.score > b.score;
return a.name < b.name;
}
int main() {
int n;
Student a[105];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].name >> a[i].score;
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) cout << a[i].name << " " << a[i].score << endl;
return 0;
}老师想给同学演示冒泡排序的工作量:n 个数排序,一共要比较多少次?请你用冒泡排序实现,并把比较次数统计出来。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(0 ≤ 数值 ≤ 1000)。
输出两个整数,用空格隔开:排序后的第一个数、总比较次数(固定为 n(n-1)/2)。
4 4 3 2 1
1 6
#include <iostream>
using namespace std;
int main() {
int n, a[1005], cnt = 0, t;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n - 1; i++)
for (int j = 1; j <= n - i; j++) {
cnt++;
if (a[j] > a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
cout << a[1] << " " << cnt << endl;
return 0;
}教务系统要画一张成绩分布图,需要知道每个具体分数有几个人。分数范围是 0~100,正好可以用桶排序统计。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(0 ≤ 分数 ≤ 100)。
按分数从小到大,只输出人数不为 0 的分数和人数,每行「分数 人数」。
5 95 88 92 88 60
60 1 88 2 92 1 95 1
#include <iostream>
using namespace std;
int main() {
int n, x, b[105] = {0};
cin >> n;
for (int i = 1; i <= n; i++) { cin >> x; b[x]++; }
for (int i = 0; i <= 100; i++)
if (b[i] > 0) cout << i << " " << b[i] << endl;
return 0;
}体育课站队,老师用「每次从剩下的人里挑最矮的站到前面」的办法整队,这就是选择排序。请你实现它并按身高升序输出。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(100 ≤ 身高 ≤ 200)。
在一行内从小到大输出,用空格隔开。
4 150 142 160 138
138 142 150 160
#include <iostream>
using namespace std;
int main() {
int n, a[1005], t;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n - 1; i++) {
int k = i;
for (int j = i + 1; j <= n; j++) if (a[j] < a[k]) k = j;
if (k != i) { t = a[i]; a[i] = a[k]; a[k] = t; }
}
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
return 0;
}图书角要把一堆书卡按编号理顺:每次拿起一张,插到前面已经排好的合适位置。这就是插入排序。请你实现并输出结果。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(1 ≤ 编号 ≤ 10000)。
在一行内从小到大输出,用空格隔开。
5 5 2 9 1 7
1 2 5 7 9
#include <iostream>
using namespace std;
int main() {
int n, a[1005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; }
a[j + 1] = key;
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
return 0;
}评优排名规则:先比总分,总分相同比语文,语文还相同比学号小的在前。请你写 cmp 函数实现这个多关键字排序。
第一行一个整数 n(1 ≤ n ≤ 100);接下来 n 行,每行三个整数 id、总分、语文(1 ≤ id ≤ 9999,0 ≤ 分数 ≤ 300/100)。
输出 n 行,每行一个学号 id,按规则排好序。
3 1026 260 88 1024 260 90 1025 270 85
1025 1024 1026
#include <iostream>
#include <algorithm>
using namespace std;
struct S {
int id, sum, ch;
};
bool cmp(S a, S b) {
if (a.sum != b.sum) return a.sum > b.sum;
if (a.ch != b.ch) return a.ch > b.ch;
return a.id < b.id;
}
int main() {
int n;
S a[105];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].id >> a[i].sum >> a[i].ch;
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) cout << a[i].id << endl;
return 0;
}运动会成绩录入时重复提交了好几次,老师要去掉重复项并从小到大排列。请你用「排序后去重」的办法处理。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(0 ≤ 数值 ≤ 1000)。
第一行输出不同数值的个数,第二行输出这些数值(升序,空格隔开)。
6 3 1 3 2 1 5
4 1 2 3 5
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[1005], m = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; i++)
if (i == 0 || a[i] != a[i - 1]) { a[m] = a[i]; m++; }
cout << m << endl;
for (int i = 0; i < m; i++) cout << a[i] << " ";
cout << endl;
return 0;
}老师想知道这次考试的「中间水平」——中位数。n 个数排序后,奇数个取正中间,偶数个取中间两个的平均值(向下取整)。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(0 ≤ 分数 ≤ 100)。
输出一个整数,表示中位数。
4 90 80 70 100
85
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[1005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
if (n % 2 == 1) cout << a[n / 2] << endl;
else cout << (a[n / 2 - 1] + a[n / 2]) / 2 << endl;
return 0;
}老师想找出这次考试里最「热门」的分数——出现次数最多的那个。如果并列,输出较小的分数。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(0 ≤ 分数 ≤ 100)。
输出两个整数,用空格隔开:出现次数最多的分数、它出现的次数。
6 88 92 88 92 60 88
88 3
#include <iostream>
using namespace std;
int main() {
int n, x, b[105] = {0}, mx = 0, ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) { cin >> x; b[x]++; }
for (int i = 0; i <= 100; i++)
if (b[i] > mx) { mx = b[i]; ans = i; }
cout << ans << " " << mx << endl;
return 0;
}发奖状时,同分的同学名次要一样(比如两人并列第一,下一位就是第三名)。请你按成绩从高到低输出每个人的名次。
第一行一个整数 n(1 ≤ n ≤ 100);第二行 n 个整数(0 ≤ 分数 ≤ 100),按输入顺序给出。
输出 n 个整数,表示每个人对应的名次,用空格隔开。
4 95 88 95 70
1 3 1 4
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[105], b[105];
cin >> n;
for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; }
sort(b, b + n, greater<int>());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (b[j] == a[i]) { cout << j + 1 << " "; break; }
}
cout << endl;
return 0;
}