19 条题解
-
10
P1025 成绩排序
难度:简单,代码和思路在下面,仅供参考~
Difficulty:easy,code and ideas are here,for reference only~ | |
------>>>目录<<<------
- 思路
- AC代码
- 彩蛋 | |
1.思路
定义
先定义一个Student的结构体,用来表示学生。
Student前的struct表示他是一个结构体。
定义每个学生的名字和成绩。
再用数组s储存他们。 | | | |
定义函数cmp,比较两个变量是否符合想要的顺
序。符合顺序返回true,否则返回false。
如果顺序是从大到小,x>y返回true,
如果是从小到大,x<y返回false。 | | | |
核心
按题意输入每个人的名字和成绩
用sort以cmp的排序方式排序 (
sort (s + 1,s + n + 1,cmp);
)输出 | | | |
2.AC代码
3.彩蛋
(见讨论)
编码不易
点赞走起
记得点赞再抱走哦~
The encoding is not easy
you can support me
remember to praise and refer to it~
-
4
P1025 成绩排序
题目描述
给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
思路
结构体部分:
学生的名字和他的成绩两个量,也就是1个string一个int,再定义一个a[105]就可以了。
struct Student { string name; int ach; }s[25];
比较部分:
bool形式,返回是否符合x学生 > y学生,但是这还有一个“则名字字典序小的在前”,所以需要再写个判断名字的。
bool cmp(Student x,Student y) { if(x.ach == y.ach) { if(x.name < y.name) { return true; } else { return false; } } else { return x.ach > y.ach; } }
把他们整合起来,在使用
sort(a + 1,a + n + 1,cmp)
排序就可以了。
参考代码
#include <iostream>//hetao3097453 #include <algorithm> using namespace std; int n; struct Student { string name; int ach; }s[25]; bool cmp(Student x,Student y) { if(x.ach == y.ach) { if(x.name < y.name) { return true; } else { return false; } } else { return x.ach > y.ach; } } int main() { cin >> n; for(int i = 1;i <= n;i++) { cin >> s[i].name >> s[i].ach; } sort(s + 1,s + n + 1,cmp); for(int i = 1;i <= n;i++) { cout << s[i].name << " " << s[i].ach << endl; } return 0; }
hetao3097453
2023年4月29日
-
2
一个赞拿走
``cpp #include <iostream>//hetao3552151 #include <algorithm> using namespace std; int n; struct Student { string name; int ach; }s[25]; bool cmp(Student x,Student y) { if(x.ach == y.ach) { if(x.name < y.name) { return true; } else { return false; } } else { return x.ach > y.ach; } } int main() { cin >> n; for(int i = 1;i <= n;i++) { cin >> s[i].name >> s[i].ach; } sort(s + 1,s + n + 1,cmp); for(int i = 1;i <= n;i++) { cout << s[i].name << " " << s[i].ach << endl; } return 0; }
上期的恐怖故事忘更了,今天,他来了!!!!!!!!!!!! (内涵推理,智力游戏,有答案)
小威和小明在一家海鲜馆吃饭,两人点了一桌丰盛的海鲜,有鱼、虾、蟹,林林总总。
快要吃完的时候,小威突然对小明说,我在米饭里下了毒,但我现在不想让你死了,你把这杯加了解药的橙汁喝了吧。
小明喝完橙汁,结果还是死了。后经检验,橙汁中并没有毒,且小明吃的海鲜小威也都吃过。
为啥小明还是死了?
答案:
海鲜与橙汁(维C)共食会在体内产生砒霜。
还有,吃奶油蛋糕也不要喝橙汁,会腹痛晕厥。
-
1
这道题不用多说了吧,认真上课的都知道
头文件(谁都会吧?)
#include <bits/stdc++.h> using namespace std;
定变量n(也是谁都会吧)
int n;
该定结构体了吧?
struct Cjb { string name; int cj; }s[25];
定排序函数(上了课的就会,不过可能没认真审题)
bool cmt(Cjb x,Cjb y) { if(x.cj==y.cj) return x.name < y.name; else return x.cj > y.cj; }
然后就是主函数和输入排序输出呗,上了课就会。
int main() { cin>>n; for (int i=1;i<=n;i++) cin>>s[i].name>>s[i].cj; sort(s+1,s+n+1,cmt); for (int i=1;i<=n;i++) cout<<s[i].name<<" "<<s[i].cj<<endl; return 0; }
别急……
完整程序这不就来了吗?
#include <bits/stdc++.h> using namespace std; int n; struct Cjb { string name; int cj; }s[25]; bool cmt(Cjb x,Cjb y) { if(x.cj==y.cj) return x.name < y.name; else return x.cj > y.cj; } int main() { cin>>n; for (int i=1;i<=n;i++) cin>>s[i].name>>s[i].cj; sort(s+1,s+n+1,cmt); for (int i=1;i<=n;i++) cout<<s[i].name<<" "<<s[i].cj<<endl; return 0; }
这是本人第一个题解,请多多包涵。
-
1
各位AC了吗?
#include <iostream>//hetao3137705 #include <algorithm> using namespace std; int n; struct Student { string name; int ach; }s[25]; bool cmp(Student x,Student y) { if(x.ach == y.ach) { if(x.name < y.name) { return true; } else { return false; } } else { return x.ach > y.ach; } } int main() { cin >> n; for(int i = 1;i <= n;i++) { cin >> s[i].name >> s[i].ach; } sort(s + 1,s + n + 1,cmp); for(int i = 1;i <= n;i++) { cout << s[i].name << " " << s[i].ach << endl; } return 0; }
-
0
难度:一般 难点:掌握结构体排序即可 AC代码如下:
#include <iostream> #include <algorithm> using namespace std; int n; struct Student { string name; int chengji; }s[10000]; bool cmp(Student x,Student y) { if(x.chengji == y.chengji) { return x.name < y.name; } else { return x.chengji > y.chengji; } } int main() { cin >> n; for(int i = 1;i <= n;i++) { cin >> s[i].name >> s[i].chengji; } sort(s + 1,s + n + 1,cmp); for(int i = 1;i <= n;i++) { cout << s[i].name << " " << s[i].chengji << endl; } return 0; }
-
0
#include<bits/stdc++.h> using namespace std; //long long n,m; int n; struct oi { int c; string name; }q[30];//易错 bool cmp(oi x,oi y) { if(x.c!=y.c)return x.c>y.c; else return x.name<y.name;//字符串字典序比较 } int main() { cin>>n; for(int i=1;i<=n;i++)cin>>q[i].name>>q[i].c; sort(q+1,q+n+1,cmp); for(int i=1;i<=n;i++)cout<<q[i].name<<' '<<q[i].c<<endl; return 0;//错了要反省反省 }
-
0
#include <bits/stdc++.h> #include <algorithm> using namespace std; struct Student { string name; int a; } s[10005]; bool cmp(Student x, Student y) { if (x.a == y.a) return x.name < y.name; return x.a > y.a; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i].name >> s[i].a; } sort(s + 1, s + n + 1, cmp); for (int i = 1; i <= n; i++) { cout << s[i].name << " " << s[i].a << endl; } }
-
0
定义结构体,包含名字和成绩,并定义cmp函数,按题目要求排序即可。
代码
#include<bits/stdc++.h>
using namespace std;
struct S { string name; int m; } s[21];
int n;
bool cmp(S x, S y) { if (x.m == y.m) return x.name < y.name; else return x.m > y.m; }
int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> s[i].name >> s[i].m; sort(s + 1, s + n + 1, cmp); for (int i = 1; i <= n; i++) cout << s[i].name << " " << s[i].m << endl;
return 0; } -
-1
只能说过简单
#include <iostream> #include <algorithm> using namespace std; struct Student { string mingzi; int fenshu; } s[105]; int n; bool cmp(Student x, Student y) { if(x.fenshu>y.fenshu) { return true; } if(x.fenshu==y.fenshu) { if(x.mingzi<y.mingzi) { return true; } } return false; } int main() { cin>>n; for(int i = 1;i<=n;i++) { cin>>s[i].mingzi>>s[i].fenshu; } sort(s+1,s+n+1,cmp); for(int i = 1;i<=n;i++) { cout<<s[i].mingzi<<" "<<s[i].fenshu<<endl; } return 0; }
-
-1
灰常简单: 首先sort用cmp函数简单一些,先判断分数,分数一样看长度,长度一样依次遍历;
上代码!(已AC)
#include <iostream> #include <algorithm> using namespace std; int n; struct Student{ int score; string name; }a[20]; bool cmp(Student x,Student y){ if(x.score!=y.score)return x.score>y.score; else if(x.name.length()!=y.name.length())return x.name.length()<y.name.length(); else for(int i=0;i<min(x.name.length(),y.name.length());i++)if(x.name[i]!=y.name[i])return x.name[i]<y.name[i]; } int main(){ 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; }
也可以这样写:
#include <iostream> #include <algorithm> using namespace std; int n; struct Student{ int score; string name; }a[20]; bool cmp(Student x,Student y){ return x.score!=y.score?x.score>y.score:x.name<y.name; } int main(){ 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; }
我啥时候成管理员了?
-
-1
#include <iostream> #include <algorithm> struct stu{std::string name;int cj;}stud[105]; bool yasuo(stu x,stu y){ if(x.cj!=y.cj)return x.cj>y.cj; return x.name<y.name;} int main(){ int n;std::cin>>n; for(int i=0;i<n;i++)std::cin>>stud[i].name>>stud[i].cj; std::sort(stud,stud+n,yasuo); for(int i=0;i<n;i++)std::cout<<stud[i].name<<" "<<stud[i].cj<<"\n"; return 0;}
-
-1
P1025
-题目回顾-
给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
-分析-
实质就是结构体排序
-代码-
#include <bits/stdc++.h>//by AGOMG(hetao1993656) using namespace std; int n; struct Student{ string name; int grd; }s[100]; bool cmp(Student a, Student b){ if(a.grd == b.grd) return a.name <= b.name; else return a.grd > b.grd; } int main(){ cin >> n; for(int i = 1; i <= n; i++) cin >> s[i].name >> s[i].grd; sort(s + 1, s + n + 1, cmp); for(int i = 1; i <= n; i++) cout << s[i].name << ' ' << s[i].grd << endl; return 0; }
-
-2
也不简单了 ···cpp
#include <bits/stdc++.h> using namespace std; int n; struct Student { string name; int fen; }s[25]; bool cmp(Student x,Student y) { if(x.fen == y.fen) { return x.name < y.name; } else { return x.fen > y.fen; } } int main() { cin >> n; for(int i = 1;i <= n;i++) { cin >> s[i].name >> s[i].fen; } sort(s + 1,s + n + 1,cmp); for(int i = 1;i <= n;i++) { cout << s[i].name << " " << s[i].fen << endl; } return 0; }
-
-2
#include <bits/stdc++.h> using namespace std; struct st { string name; int res; } a[25]; bool cmp(st a,st b) { if(a.res!=b.res) return a.res>b.res; else return a.name<b.name; } int main() { int n; cin>> n; for(int i=1;i<=n;i++) cin>> a[i].name>>a[i].res; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) cout<< a[i].name<<' '<<a[i].res<<'\n'; }
-
-2
我用脚趾尖做的
#include <bits/stdc++.h> using namespace std; struct st{ string name; int fs; }a[105];//定义结构体 bool cmp(const st &x,const st &y){ if(x.fs!=y.fs){ return x.fs>y.fs; } return x.name<y.name; }//结构体排序规则 int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].name>>a[i].fs; } sort(a+1,a+1+n,cmp);//sort排序 for(int i=1;i<=n;i++){ cout<<a[i].name<<" "<<a[i].fs<<endl; } return 0; }
-
-2
#include <iostream> //by 72525 #include <algorithm> using namespace std; struct Student { string name; int cj; } s[105]; int n; bool cmp(Student x, Student y) { if(x.cj!=y.cj) return x.cj>y.cj; else return x.name<y.name; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> s[i].name>> s[i].cj; sort(s+1,s+n+1,cmp); for(int i=1;i<=n;i++) cout<<s[i].name<<" "<<s[i].cj<<endl; return 0; }
-
-2
#include<bits/stdc++.h>//by Chris knowledge using namespace std; struct S { string name; int m; } s[21]; int n; bool cmp(S x, S y) { if (x.m == y.m) return x.name < y.name; else return x.m > y.m; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> s[i].name >> s[i].m; sort(s + 1, s + n + 1, cmp); for (int i = 1; i <= n; i++) cout << s[i].name << " " << s[i].m << endl; return 0; }
- 1
信息
- ID
- 18
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 2698
- 已通过
- 1011
- 上传者