C57-结构体2
2025-05-17 12:12:15
发布于:江苏
11阅读
0回复
0点赞
1.输出第k个人的信息
#include<iostream>
using namespace std;
struct node{
string name;
int a, b, c;
}s[105];
int main(){
int n, k;
cin >> n >> k;
for (int i=1; i<=n; i++){
cin >> s[i].name >> s[i].a >> s[i].b >> s[i].c;
}
//输出第k个人的信息
cout << s[k].name <<" "<< s[k].a <<" "<< s[k].b <<" "<< s[k].c;
return 0;
}
2. 结构体分数排序
#include <bits/stdc++.h>
using namespace std;
int n;
struct node{
string name;
int score;
}a[105];
bool cmp(node x, node y) { //自定义比较函数规则
if (x.score == y.score) {
return x.name < y.name; //如果分数想通过,按照名字字典序进行排序
}
return x.score > y.score; //按照分数
}
int main(){
cin >> n;
for (int i=1; i<=n; i++){
cin>>a[i].name>>a[i].score;
}
sort(a+1, a+n+1, cmp);
for (int i=1; i<=n; i++){
cout << a[i].name << ' ' <<a[i].score << endl;
}
return 0;
}
3. string的占位符
#include<iostream>
using namespace std;
int main(){
// char s[] = "hello xiaomawang";
string s = "你好 小码王";
cout << s << endl;
printf("%s\n", s.c_str());
return 0;
}
这里空空如也
有帮助,赞一个