首页 > 试题广场 >

找最小数

[编程题]找最小数
  • 热度指数:20910 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
第一行输入一个数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
给一个Java 版本的:

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;
        }
    }
}


发表于 2022-06-02 21:27:51 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
    static class pairs {
        int x;
        int y;

        public pairs(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return x + " " + y;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<pairs> list = new ArrayList<>();
        int n = Integer.parseInt(br.readLine());
        for (int i = 0; i < n; i++) {
            String[] s = br.readLine().split(" ");
            pairs p = new pairs(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
            list.add(p);
        }
        list.sort(new Comparator<pairs>() {
            @Override
            public int compare(pairs o1, pairs o2) {
                int sum1 = o1.x - o2.x;
                int sum2 = sum1 == 0 ? o1.y - o2.y : sum1;
                return sum2;
            }
        });
        System.out.println(list.get(0));

    }
}


发表于 2021-02-23 18:24:22 回复(0)
Java解法,使用TreeMap
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            if (map.get(x)==null) map.put(x,y);
            else if (map.get(x)>y) map.put(x,y);
        }
        Map.Entry<Integer, Integer> entry = map.firstEntry();
        System.out.println(entry.getKey()+" "+entry.getValue());
    }
}


发表于 2020-03-13 18:55:26 回复(0)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class problem12 {
    public static class Node{
        int first;
        int second;
        Node(int f,int s){
            this.first=f;
            this.second=s;
        }
    }
    public static class MyComparator implements Comparator<Node>{

    
        @Override
        public int compare(Node o1, Node o2) {
            // TODO Auto-generated method stub
            return o1.first!=o2.first?o1.first-o2.first:o1.second-o2.second;
        }
        
    }
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n =sc.nextInt();
            List<Node> list = new ArrayList<>();
            for(int i=0;i<n;i++) {
                int f = sc.nextInt();
                int s=sc.nextInt();
                Node node = new Node(f,s);
                list.add(node);
            }
            list.sort(new MyComparator());
            Node result = list.get(0);
            System.out.println(result.first+" "+result.second);
        }
        sc.close();
    }
}

发表于 2020-03-06 11:23:19 回复(0)
 import java.util.Scanner;
import java.util.LinkedList;
public class Main{
    private static int search(int[] a){
        int len = a.length;
        int min = a[0];
        for(int i = 0;i<len;i++){
            if(a[i]<min){
                min = a[i];
            }
        }
        return min;
    }
    private static int search(LinkedList<Integer> a){
        int len = a.size();
        int min = a.get(0);
        for(int i = 0;i<len;i++){
            int num = a.get(i);
            if(num<min){
                min = num;
            }
        }
        return min;
    }
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] x = new int[n];
        int[] y = new int[n];
        for(int i = 0;i<n;i++){
            x[i] = scanner.nextInt();
            y[i] = scanner.nextInt();
        }
        int min_x = search(x);
        LinkedList<Integer> list = new LinkedList<>();
        for(int i = 0;i<n;i++){
            if(x[i]==min_x){
                list.add(y[i]);
            }
        }
        System.out.print(min_x+" "+search(list));
        
    }
}

发表于 2018-12-29 19:28:14 回复(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]);
        }
    }
}

发表于 2018-09-28 10:52:28 回复(0)

额感觉也没什么好说的
就是先找到行值最小值
然后再找行值最小值条件下的列最小值
就用普通数组实现

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int n = scan.nextInt();
            int[][] data = new int[n][2];
            for (int i = 0; i < n; i++) {
                data[i][0] = scan.nextInt();
                data[i][1] = scan.nextInt();
            }
            int min = data[0][0];
            int temp=0;
            for (int i = 1; i < n; i++) {
                if (data[i][0] < min) {
                    min = data[i][0];
                    temp=i;
                }
            }
            int min2 = data[temp][1];
            for (int i = 0; i < n; i++) {
                    if (data[i][0] == min) {
                        if (data[i][1] < min2) {
                            min2 = data[i][1];
                        }
                    }
            }
            System.out.println(min + " " + min2);
        }
    }
}
发表于 2018-05-07 14:37:08 回复(0)
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();
    }
}

发表于 2017-04-02 14:25:36 回复(0)
import java.util.Scanner;

/**
 * Created by fhqplzj on 17-2-19 at 下午8:32.
 */
public class My10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            int min0 = Integer.MAX_VALUE, min1 = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                int a = scanner.nextInt();
                int b = scanner.nextInt();
                if (a < min0 || (a == min0 && b < min1)) {
                    min0 = a;
                    min1 = b;
                }
            }
            System.out.println(String.format("%d %d", min0, min1));
        }
    }
}
编辑于 2017-02-19 20:45:13 回复(0)