-
题目描述:
-
第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x y。输出一组x y,该组数据是所有数据中x最小,且在x相等的情况下y最小的。
-
输入:
-
输入有多组数据。
每组输入n,然后输入n个整数对。
-
输出:
-
输出最小的整数对。
-
样例输入:
-
5 3 3 2 2 5 5 2 1 3 6
-
样例输出:
-
2 1
代码一:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n,i;
int x,y,minx,miny;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
if(i==0)
{
minx=x;
miny=y;
}
else
{
if(x<minx)
{
minx=x;
miny=y;
}
else if(x==minx && y<miny)
{
minx=x;
miny=y;
}
}
}
printf("%d %d\n",minx,miny);
}
return 0;
}
代码二:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node{
int x;
int y;
bool operator <(const node &anum)const
{
if(x==anum.x)
{
return y<anum.y;
}
return x<anum.x;
}
};
node nums[1001];
int main()
{
int n,i;
int x,y;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d %d",&nums[i].x,&nums[i].y);
}
sort(nums,nums+n);
printf("%d %d\n",nums[0].x,nums[0].y);
}
return 0;
}