- 问题描述
输入N个日期,每个以年、月、日的顺序读入,打印输出最晚的日期、最早的日期。 - 输入说明
你的程序需要从标准输入设备(通常为键盘)中读入多组测试数据。每组输入数据由多行组成。每组测试数据的第一行输入一个整数N(0<N<20),表示有N个日期。其后N行每行有三个整数Y(1≤Y≤2015),M(1≤M≤12),D(1≤D≤31)表示一个日期。 - 输出说明
对每组测试数据,你的程序需要向标准输出设备(通常为启动该程序的文本终端)输出两行,每行包括3个整数,第一行为最晚日期,第二行为最早日期,整数之间以一个空格分隔,行首与行尾无空格,所有数据前后没有多余的空行,两组数据之间也没有多余的空行。 - 输入范例
cpp
3
2015 3 2
2011 4 15
1 1 1
- 输出范例
cpp
2015 3 2
1 1 1
感想:
代码如下:
cpp
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct date {
int y;
int m;
int d;
};
bool cmp(const date& a,const date&b) {
if(a.y != b.y)
return a.y>b.y ; //descend
else if(a.m != b.m)
return a.m>b.m; //if a.y == b.y 按照m 降序排列
else
return a.d>b.d;//if a.y == b.y and a.m == b.m 按d降序排列
}
int main( ) {
int n;
while(cin>>n) {
vector<date> arr(n);
for(int i = 0; i<n; ++i) {
cin>>arr[i].y>>arr[i].m>>arr[i].d;
}
sort(arr.begin(),arr.end(),cmp);
cout<<arr[0].y<<" "<<arr[0].m<<" "<<arr[0].d<<endl;
cout<<arr[n-1].y<<" "<<arr[n-1].m<<" "<<arr[n-1].d<<endl;
}
return 0;
}