1592 - 找最小数
时间限制 : 1 秒
内存限制 : 32 MB
第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x y。输出一组x y,该组数据是所有数据中x最小,且在x相等的情况下y最小的。
<br />
题目输入
输入有多组数据。<br />
每组输入n,然后输入n个整数对。
<br />
题目输出
输出最小的整数对。
<br />
输入/输出样例
输入格式
1 35 68 5 25 70 59 79 65 63 46 6 28 82
输出格式
35 68 25 70
C语言解答
#include<stdio.h> #include<string.h> #include<math.h> main(){ int n,i,num,min1,min2,k1,k2; while(scanf("%d",&n)!=EOF){ scanf("%d%d",&min1,&min2); for(i=1;i<n;i++){ scanf("%d%d",&k1,&k2); if(k1<min1){ min1=k1; min2=k2; }else if(k1==min1){ if(k2<min2){ min2=k2; } } } printf("%d %d\n",min1,min2); } return 0; }
C++解答
//北邮2010计算机:1170:找最小数 //(1<=n<=1000) #include <fstream> #include <algorithm> #include <iostream> using namespace std; struct NUMBER{ int x, y; }; NUMBER a[1000]; bool cmp( NUMBER a, NUMBER b ){ if( a.x == b.x ) return a.y < b.y; else return a.x < b.x; }; int main() { int i, j, k, n, m; while( cin >> n ){ for( i=0; i<n; i++ ) cin >> a[i].x >> a[i].y; sort(a,a+n,cmp); cout << a[0].x << " " << a[0].y << endl; } return 0; }
Java解答
import java.util.Arrays; import java.util.Scanner; public class Main { private static Scanner s = new Scanner(System.in) ; public static void main(String[] args) { while(s.hasNext()){ int t = s.nextInt() ; Aa a[] = new Aa[t] ; for (int i = 0; i < t; i++) { int x = s.nextInt() ; int y = s.nextInt() ; a[i] = new Aa(x, y) ; } Arrays.sort(a) ; System.out.println(a[0].x+" "+a[0].y) ; } } } class Aa implements Comparable{ int x ; int y ; public Aa(int x, int y) { super(); this.x = x; this.y = y; } @Override public int compareTo(Object o) { Aa a = (Aa)o ; if(this.x<a.x){ return -1 ; }else if(this.x==a.x){ if(this.y>a.y){ return 1 ; }else if(this.y==a.y){ return 0 ; }else return -1 ; } else return 1 ; } }