题目描述
给定 N个三元组 (x,y,z),其中 x是整数,y是浮点数,z是字符串。
请你按照 x从小到大的顺序将这些三元组打印出来。
数据保证不同三元组的 x值互不相同。
样例
输入样例:
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
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
const int N=10010;//将N作为全局变量
using namespace std;
struct Data//定义一个结构体
{
int x;
double y;
string z;
bool operator< (const Data//结构体的名称 &t) const//重载小于号
{
return x<t.x;
}
}a[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)//输入三元数组
{
cin>>a[i].x>>a[i].y>>a[i].z;
}
sort(a,a+n);//将这个数组排序
for(int i=0;i<n;i++)//输出三元数组
{
cout<<a[i].x<<' ';
printf("%.2lf ",a[i].y);//注意:这里要输出保留两位小数的浮点数
cout<<a[i].z<<endl;}
return 0;
}