11 solutions
-
1
啊这,如果是比赛的话,肯定就用 sort 函数了。写个 cmp 就随便打。
#include <iostream> #include <algorithm> using namespace std; struct node2 { string name;// 姓名 int score;// 乘积 }; bool cmp(node2 a, node2 b) { return a.score > b.score; } int main(void) { int N; cin >> N; node2* arr = new node2[N]; for (int i = 0; i < N; ++i) { string name; int score; cin >> arr[i].name >> arr[i].score; } sort(arr,arr+N,cmp); for (int i = 0; i < N; ++i) { string str = ""; int score = arr[i].score; if (score == 0) { str = "Bad"; }else if (score >= 1 && score < 200) { str = "Not good"; }else if (score >= 200 && score < 300) { str = "Bronze medal"; }else if (score >= 300 && score < 400) { str = "Silver medal"; }else if (score >= 400) { str = "Gold medal"; } cout << arr[i].name << " " << arr[i].score << " " << str << endl; } return 0; }
-
1
思路:
结构体存名字和成绩,然后sort成绩从大到小(cmp函数),对应输出就行
#include<bits/stdc++.h> using namespace std; struct stu{ int num; char name[25]; }st[300]; bool cmp(stu x,stu y){ return x.num>y.num; } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",st[i].name); scanf("%d",&st[i].num); } sort(st,st+n,cmp); for(int i=0;i<n;i++) { printf("%s ",st[i].name); if(st[i].num==0)printf("%d Bad\n",st[i].num); else if(st[i].num>=1&&st[i].num<200)printf("%d Not good\n",st[i].num); else if(st[i].num>=200&&st[i].num<300)printf("%d Bronze medal\n",st[i].num); else if(st[i].num>=300&&st[i].num<400)printf("%d Silver medal\n",st[i].num); else printf("%d Gold medal\n",st[i].num); } return 0; }
-
0
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define LL long long #define ph push_back #define INF 0x3f3f3f3f #define PII pair<int, int> const int N =310; int n; struct stu{ string name; int grade; string level; bool operator <(const stu & W) const{ return grade>W.grade; } } stu[N]; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin>>n; for(int i=1;i<=n;i++){ cin>>stu[i].name>>stu[i].grade; } for(int i=1;i<=n;i++){ if(stu[i].grade==0) stu[i].level="Bad"; if(stu[i].grade>0&&stu[i].grade<200) stu[i].level="Not good"; if(stu[i].grade>=200&&stu[i].grade<300) stu[i].level="Bronze medal"; if(stu[i].grade>=300&&stu[i].grade<400) stu[i].level="Silver medal"; if(stu[i].grade>=400)stu[i].level="Gold medal"; } sort(stu+1,stu+1+n); for(int i=1;i<=n;i++){ cout<<stu[i].name<<' '<<stu[i].grade<<' '<<stu[i].level<<endl; } return 0; }
-
0
#include<bits/stdc++.h> using namespace std; struct pp{ string a; int c; }i[305]; bool cmp(pp j,pp k){ return j.c>k.c; } int main() { int n; cin>>n; for(int c=0;c<n;c++){ cin>>i[c].a>>i[c].c; } sort(i,i+n,cmp); for(int c=0;c<n;c++){ cout<<i[c].a<<" "<<i[c].c<<" "; if(i[c].c==0) cout<<"Bad"; else if(i[c].c>=1&&i[c].c<200) cout<<"Not good"; else if(i[c].c>=200&&i[c].c<300) cout<<"Bronze medal"; else if(i[c].c>=300&&i[c].c<400) cout<<"Silver medal"; else if(i[c].c>=400) cout<<"Gold medal"; cout<<endl; } return 0; }
-
0
题解
这个题用结构体储存每个同学的名字和成绩,然后根据每位同学的成绩进行排序(比如sort排序,cmp大法),最后根据每位同学的分数判断奖项进行输出即可,上代码:
代码
#include<bits/stdc++.h> #include<iostream> #include<algorithm> #include<iomanip> using namespace std; int x[100000000],y[17]; char a[11],b[18]; struct Main{ char a[20]; int cheng; }pp[300]; int cmp(Main x,Main y){ return x.cheng>y.cheng; } int main() { int n; cin>>n; for(int i=0;i<n;i++){ cin>>pp[i].a>>pp[i].cheng; } sort(pp,pp+n,cmp); for(int i=0;i<n;i++){ if(pp[i].cheng==0){ cout<<pp[i].a<<" "<<pp[i].cheng<<" Bad"<<endl; } else if(pp[i].cheng>=1&&pp[i].cheng<200){ cout<<pp[i].a<<" "<<pp[i].cheng<<" Not good"<<endl; }else if(pp[i].cheng>=200&&pp[i].cheng<300){ cout<<pp[i].a<<" "<<pp[i].cheng<<" Bronze medal"<<endl; }else if(pp[i].cheng>=300&&pp[i].cheng<400){ cout<<pp[i].a<<" "<<pp[i].cheng<<" Silver medal"<<endl; }else if(pp[i].cheng>=400){ cout<<pp[i].a<<" "<<pp[i].cheng<<" Gold medal"<<endl; } } return 0; }
-
0
利用结构体sort排序,在用条件语句判断
#include<stdio.h> #include<algorithm> #include<string> #include<vector> #include<iostream> using namespace std; int n; struct cj { string name; int grade; }s[1000]; int cmp(cj a,cj b) { return a.grade>b.grade; } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { cin>>s[i].name>>s[i].grade; } sort(s,s+n,cmp); for(int i=0;i<n;i++) { if(s[i].grade>=1&&s[i].grade<200) { cout<<s[i].name<<" "<<s[i].grade<<" "<<"Not good"<<endl; }else if(s[i].grade>=200&&s[i].grade<300) { cout<<s[i].name<<" "<<s[i].grade<<" "<<"Bronze medal"<<endl; }else if(s[i].grade>=300&&s[i].grade<400) { cout<<s[i].name<<" "<<s[i].grade<<" "<<"Silver medal"<<endl; }else if(s[i].grade>=400) { cout<<s[i].name<<" "<<s[i].grade<<" "<<"Gold medal"<<endl; }else cout<<s[i].name<<" "<<s[i].grade<<" "<<"Bad"<<endl; } return 0; }
-
0
总的来说,这道题挺简单的,但是Wrong了很多次,事实证明对map的理解还不够,最开始用map来做的,又忽视了题意中的如果总成绩相等,按输入顺序排序
不废话了,开始正题吧,总体来说,用结构体来做,就会变得异常简单。
#include<bits/stdc++.h> using namespace std; struct student { string name; int score; string id; }arr[500],temp; int main() { int a,b,m,n; cin>>m; for(int i=0;i<m;i++) { cin>>arr[i].name>>arr[i].score; } for(int i=0;i<m;i++) { for(int j=0;j<m-i;j++) { if(arr[j+1].score>arr[j].score) { temp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=temp; } } } for(int i=0;i<m;i++) { cout<<arr[i].name<<" "<<arr[i].score<<" "; if(arr[i].score==0) { cout<<"Bad"; } else if(arr[i].score>=1&&arr[i].score<200) { cout<<"Not good"; } else if(arr[i].score>=200&&arr[i].score<300) { cout<<"Bronze medal"; } else if(arr[i].score>=300&&arr[i].score<400) { cout<<"Silver medal"; } else if(arr[i].score>=400) { cout<<"Gold medal"; } cout<<endl; } return 0; }
是不是超级简单了! 不要被一种方法卡死,好好学习,天天向上!
-
0
题目解析: 本题是一个很明显的结构体排序,由于数据较小,冒泡排序应该也可以,不过一般是用sort快捷完成排序。构建结构体Person,内含成员姓名,成绩和获得的奖项,由于是根据成绩由大到小排序,需要改写一下cmp,最后就是简单的if判断了,注意赋值给奖项时,如果使用的是字符数组,要用strcpy来赋值,如果是string类直接赋值就行了。
可行代码:
#include <bits/stdc++.h> using namespace std; typedef struct { char name[20]; int score; string get; }Person; Person a[500]; bool cmp(Person a, Person b) { return a.score > b.score; } int main(){ int n; 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++) { if(a[i].score < 1) a[i].get = "Bad"; else if(a[i].score < 200) a[i].get = "Not good"; else if(a[i].score < 300) a[i].get = "Bronze medal"; else if(a[i].score < 400) a[i].get = "Silver medal"; else a[i].get = "Gold medal"; cout << a[i].name << " " << a[i].score << " " << a[i].get << endl; } return 0; }
- 1
Information
- ID
- 110
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 215
- Accepted
- 79
- Uploaded By