40 找最小数 北邮复试 c++

第一行输入一个数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的元素组即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值