//用优先队列可以秒杀此题 #include <bits/stdc++.h> using namespace std; priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> Q; int main(){ int n,x,y; cin>>n; while(n--){ cin>>x>>y; pair<int,int> a(x,y); Q.push(a); } cout<<Q.top().first<<" "<<Q.top().second; return 0; }
#include <iostream> #include <algorithm> #include <vector> using namespace std; struct nums{ int x; int y; }; bool cmp(const nums num1,const nums num2); bool cmp(const nums num1,const nums num2){ if(num1.x==num2.x) return num1.y<num2.y; return num1.x<num2.x; } int main(){ int n; while(cin>>n){ vector<nums> a(n); for(int i=0;i<n;i++) cin>>a[i].x>>a[i].y; stable_sort(a.begin(),a.end(),cmp); cout<<a[0].x<<" "<<a[0].y<<endl; } return 0; }
#include <iostream> using namespace std; int main() { int n; while(cin>>n) { int x[n],y[n]; for(int i=0;i<n;i++) { cin>>x[i]; cin>>y[i]; } int min_x=0x7ffffff,min_y=0x7ffffff; for(int i=0;i<n;i++) { if(x[i]<min_x||(min_x==x[i]&&y[i]<min_y)) { min_x=x[i]; min_y=y[i]; } } cout<<min_x<<" "<<min_y<<endl; } return 0; }
import java.util.*; /** * 找最小数 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int n = scanner.nextInt(); List<MyPackage> dataList = new ArrayList<>(n); for (int i = 0; i < n; ++i) { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); MyPackage myPackage = new MyPackage(num1, num2); dataList.add(myPackage); } Comparator<MyPackage> myPackageComparator = (o1, o2) -> { if (o1.getNum1() != o2.getNum1()) { return o1.getNum1() - o2.getNum1(); } else { return o1.getNum2() - o2.getNum2(); } }; Collections.sort(dataList, myPackageComparator); MyPackage result = dataList.get(0); String formatString = String.format("%d %d", result.getNum1(), result.getNum2()); System.out.println(formatString); } } private static class MyPackage { private int num1; private int num2; public MyPackage(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public int getNum1() { return num1; } public void setNum1(int num1) { this.num1 = num1; } public int getNum2() { return num2; } public void setNum2(int num2) { this.num2 = num2; } } }
#include <stdio.h> #include <stdlib.h> typedef struct all{ int x; int y; }all; int cmp(const void *a,const void *b){ all *c=(all *)a; all *d=(all *)b; if(c->x!=d->x) return c->x - d->x; else return c->y - d->y; } int main(){ int n; while(scanf("%d",&n)!=EOF){ all num[1000]; for(int i=0;i<n;i++){ scanf("%d %d\n",&num[i].x,&num[i].y); } qsort(num,n,sizeof(num[0]),cmp); printf("%d %d\n",num[0].x,num[0].y); } return 0; }法二:边输入边比较
#include <stdio.h> #include <stdlib.h> #define INT_MAX 0x7fffffff int main(){ int n,a,b,mina,minb; mina=minb=INT_MAX; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ scanf("%d %d\n",&a,&b); if(mina>a){ mina=a; minb=b; }else if(mina==a){ if(minb>b){ minb=b; } } } printf("%d %d\n",mina,minb); } return 0; }注意:没有搞懂二维数组怎么写!
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; struct data{ int x; int y; }; bool compare1(data x,data y){ return x.x<y.x; } bool compare2(data x,data y){ return x.y<y.y; } struct data a[1000]; int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ scanf("%d%d",&a[i].x,&a[i].y); } sort(a,a+n,compare1); int z=1; for(int i=1;i<n;i++){ if(a[i].x==a[0].x){ z++; } } sort(a,a+z,compare2); printf("%d %d",a[0].x,a[0].y); } return 0; }
#include<iostream> #include<queue> using namespace std; //用优先队列重载operator typedef struct node { int x,y; node(int x = 0,int y = 0):x(x),y(y){}; bool operator > (const node &a) const { if(x != a.x) return x > a.x; else//不能用else if()因为编译器会认为还有其它的情况,会报错not all control paths return a value 这句话的意思是函数并不是所有分支都有返回值。 return y > a.y; } }; int main(void) { int n; priority_queue<node,vector<node>,greater<node>> q;//小顶堆 while(cin >> n) { getchar(); for(int i = 0;i < n;i++) { int x,y; cin >> x >> y; q.push(node(x,y)); } cout << q.top().x << ' ' << q.top().y << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int arr[1001][2]; int main() { arr[0][0]={{2147483647,2147483647}}; int n; int i; scanf("%d",&n); for(i = 1; i < n+1; i++) { scanf("%d %d",&arr[i][0],&arr[i][1]); } for(i = 1;i < n+1; i++) { if(arr[i][0] < arr[0][0]) { arr[0][0] = arr[i][0]; arr[0][1] = arr[i][1]; } else if(arr[i][0] == arr[0][0]) { if(arr[i][1] < arr[0][1]) { arr[0][0] = arr[i][0]; arr[0][1] = arr[i][1]; } } } printf("%d %d\n",arr[0][0],arr[0][1]); return 0; }方便理解,实际上可以在输入时就进行比较,但是我这样写时间复杂度也是O(n)
#include<bits/stdc++.h> //万能头 #include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF) { int a[n][2]; for(int i=0;i<n;i++) { scanf("%d%d",&a[i][0],&a[i][1]); } int x_min=a[0][0], y_min=a[0][1]; for(int i=1;i<n;i++) { if(a[i][0]<x_min) { x_min=a[i][0]; y_min=a[i][1]; } else if(a[i][0]==x_min) { if(a[i][1]<y_min) { y_min=a[i][1]; } } } printf("%d %d\n",x_min,y_min); } return 0; }
#include<bits/stdc++.h> using namespace std; struct Pair{ int x; int y; }; bool cmp(Pair a,Pair b){ if(a.x<b.x) return 1; else if(a.x==b.x){ if(a.y<b.y) return 1; else return 0; } else return 0; } int main(){ int n; while(cin>>n){ Pair *p=new Pair[n]; for(int i=0;i<n;i++) cin>>p[i].x>>p[i].y; sort(p,p+n,cmp); cout<<p[0].x<<" "<<p[0].y<<endl; } }
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct data
{
int x,y;
bool operator < (data &A) const//重载<操作符
{
if(x!=A.x)
return x<A.x;
return y<A.y;
}
}num[1000];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d",&num[i].x,&num[i].y);
sort(num,num+n);//利用库函数进行快排
printf("%d %d\n",num[0].x,num[0].y);//第一个即为最小
}
return 0;
}
#include<iostream>
#include<stdio.h> #include<algorithm> using namespace std; struct pa{ int x; int y; }p[1000]; bool cmp(pa a,pa b){ if(a.x==b.x) return a.y<b.y; else return a.x<b.x; } int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); sort(p,p+n,cmp); printf("%d %d\n",p[0].x,p[0].y); return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] nums = new int[2]; nums[0] = sc.nextInt(); nums[1] = sc.nextInt(); int x,y; for(int i=0;i<n-1;i++){ x=sc.nextInt(); y=sc.nextInt(); if(x<nums[0]){ nums[0]=x; nums[1]=y; }else if(x==nums[0]){ if(y<nums[1]){ nums[1]=y; } } } System.out.println(nums[0]+" "+nums[1]); } } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); Map<Integer, Integer> map = new HashMap<>(); int min = Integer.MAX_VALUE; for(int i = 0; i < n; i++) { int x = scan.nextInt(); int y = scan.nextInt(); if(map.containsKey(x) && y < map.get(x) || !map.containsKey(x)) map.put(x, y); if(min > x) min = x; } System.out.println(min + " " + map.get(min)); scan.close(); } }
#include <stdio.h> int main() { int n; int x, y; int minx, miny; while(scanf("%d", &n)!=EOF) { scanf("%d", &x); scanf("%d", &y); n--; minx=x; miny=y; while(n--) { scanf("%d", &x); scanf("%d", &y); 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; }
//我在eclipse上面答案显示是正确,但是在这里显示不通过,在最后加了一个换行才通过的
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=sc.nextInt();
//定义一个二维数组来放数据
int[][] arr=new int[a][2];
//通过循环,将这些数据输入
for(int i=0;i<a;i++){
for(int j=0;j<2;j++){
arr[i][j]=sc.nextInt();
}
}
//开始比较第一列的最小值
int temp=arr[0][0];
int row=0;
for(int h=1;h<a;h++){
if(arr[h][0]<temp){
temp=arr[h][0];
row=h;
}
//如果第一列相等,就判断第二列
if(arr[h][0]==temp){
if(arr[h][1]<arr[row][1]){
row=h;
temp=arr[h][0];
}
}
}
//输出数据
System.out.print(arr[row][0]+" "+arr[row][1]);
System.out.println();
}
}
}
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); while(input.hasNext()){ int [][] a = new int[1000][2]; int x,y; int n = input.nextInt(); a[0][0]=x=input.nextInt(); a[0][1]=y=input.nextInt(); for(int i=1;i<n;i++) { a[i][0] = input.nextInt(); a[i][1] = input.nextInt(); if(a[i][0]<x) { x=a[i][0]; y=a[i][1]; } else if(a[i][0]==x&&a[i][1]<y) { x=a[i][0]; y=a[i][1]; } } System.out.println(x+" "+y); } input.close(); } }