问题描述
输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩都按先录入排列在前的规则处理。
例示:
输入:
jack 70
peter 96
Tom 70
smith 67
输出:
从高到低 成绩
peter 96
jack 70
Tom 70
smith 67
从低到高
smith 67
Tom 70
jack 70
peter 96
输入描述:
输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
示例1
输入
扫描二维码关注公众号,回复:
7880317 查看本文章
3
0
fang 90
yang 50
ning 70
输出
fang 90
ning 70
yang 50
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <stack> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cstdio> #include <cstdlib> using namespace std;
struct { string name; float scor; };
int cmpASC(const void *c,const void *d) { Node a = *(Node*)c; Node b = *(Node*)d; return a.scor >= b.scor; }
int cmpDES(const void *c,const void *d) { Node a = *(Node*)c; Node b = *(Node*)d; return a.scor <= b.scor; } int main() { int n, m; cin>>n; cin>>m; Node node[n]; for(int i = 0; i < n; i ++) { string s; float a; cin>>s>>a; node[i].name = s; node[i].scor = a; } if(m==0) { qsort(node,n,sizeof(node[0]),cmpDES); } else { qsort(no 大专栏 牛客-清华-01-成绩排序de,n,sizeof(node[0]),cmpASC); } for(int i = 0; i < n; i ++) { cout<<node[i].name<<" "<<node[i].scor<<endl; } return 0; }
|
上边代码,不知道哪里出错,oj上提示指针溢出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <stack> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cstdio> #include <cstdlib> using namespace std;
struct { string name; float scor; } stu[1000]; int main() { int n, m; while(cin>>n) { cin>>m; for(int i = 0; i < n; i ++) { string s; float a; cin>>s>>a; stu[i].name = s; stu[i].scor = a; } if(m==0) { Node tem; for(int i = 1,j; i < n; i ++) { tem = stu[i]; for(j = i; j>0&& stu[j-1].scor<tem.scor; j--) { stu[j] = stu[j-1]; } stu[j] = tem; } } else { Node tem; for(int i = 1,j; i < n; i ++) { tem = stu[i]; for(j = i; j>0&& stu[j-1].scor>tem.scor; j--) { stu[j] = stu[j-1]; } stu[j] = tem; } } for(int i = 0; i < n; i ++) { cout<<stu[i].name<<" "<<stu[i].scor<<endl; } } return 0; }
|