第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x y。输出一组x y,该组数据是所有数据中x最小,且在x相等的情况下y最小的。
输入描述:
输入有多组数据。
每组输入n,然后输入n个整数对。
输出描述:
输出最小的整数对。
示例1
输入
5
3 3
2 2
5 5
2 1
3 6
输出
2 1
题解
struct 每行xy,bool cmp排序方法,cin>>n,n–作为循环条件,输入每行struct,排序,输出第一组元素
c++11
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
struct Line{
int x;
int y;
}line[200];
bool cmp(Line a,Line b){
if(a.x==b.x){
return a.y<b.y;
}else{return a.x<b.x;}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
//在这个模板的同一级目录下建一个'1.txt'的文件
//然后把输入全部放进去保存,之后运行代码完全不用输入任何数据
#endif
int n;
while (cin>>n){
for(int i=0;i<n;i++){
cin>>line[i].x>>line[i].y;
}
sort(line,line+n,cmp);
cout<<line[0].x<<" "<<line[0].y<<endl;
}
return 0;
}
总结
思想很简单,看似是查找问题,其实先按要求排序,再取出下标为0的元素组即可