三元组排序(acwing)c++
- 手机
- 2025-09-18 00:00:02

给定 NN 个三元组 (x,y,z)(x,y,z),其中 xx 是整数,yy 是浮点数,zz 是字符串。
请你按照 xx 从小到大的顺序将这些三元组打印出来。
数据保证不同三元组的 xx 值互不相同。
输入格式第一行包含整数 NN。
接下来 NN 行,每行包含一个整数 xx,一个浮点数 yy,一个字符串 zz,表示一个三元组,三者之间用空格隔开。
输出格式共 NN 行,按照 xx 从小到大的顺序,每行输出一个三元组。
注意,所有输入和输出的浮点数 yy 均保留两位小数。
数据范围1≤N≤100001≤N≤10000, 1≤x,y≤1051≤x,y≤105, 字符串总长度不超过 105105。
输入样例: 5 32 1.36 nsyiupnnhc 18 4.53 fmofzwrah 33 4.86 wzuymbm 1 3.93 gtnrwcebt 31 4.53 gcllxioc 输出样例: 1 3.93 gtnrwcebt 18 4.53 fmofzwrah 31 4.53 gcllxioc 32 1.36 nsyiupnnhc 33 4.86 wzuymbm代码:
#include<bits/stdc++.h> using namespace std; const int N=100010; int n; struct Sanyuan{ int x; double y; string s; }sanyuan[N]; bool cmp(Sanyuan a,Sanyuan b){ if(a.x!=b.x)return a.x<b.x; } int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>sanyuan[i].x>>sanyuan[i].y>>sanyuan[i].s; } sort(sanyuan,sanyuan+n,cmp); for(int i=0;i<n;i++){ cout<<sanyuan[i].x<<" "<<std::fixed<<std::setprecision(2)<<sanyuan[i].y<<" "<<sanyuan[i].s<<endl; } return 0; }代码2:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=10010; struct node{ int a; double b; string c; }; node s[N]; bool cmp(node x,node y){//需要调用函数 return x.a<y.a; } int main(){ int n; cin>>n; struct node temp; for(int i=0;i<n;i++){ cin>>s[i].a>>s[i].b>>s[i].c; } sort(s,s+n,cmp); for(int i=0;i<n;i++){ printf("%d %.2f %s\n",s[i].a,s[i].b,s[i].c.c_str());//字符串输出要加c_str(); } return 0; }愿我们都能成为我们想要去成为的人!
永远不要降低对自己的要求,也永远不要停止向前的脚步!
三元组排序(acwing)c++由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“三元组排序(acwing)c++”
下一篇
netty如何处理粘包半包