据说是菊花机试题:求最小长方形的左下右上坐标

最小长方形

Problem Description
给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内。长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内。

Input
测试输入包含若干测试用例,每个测试用例由一系列坐标组成,每对坐标占一行,其中|x|和|y|小于 231;一对0 坐标标志着一个测试用例的结束。注意(0, 0)不作为任何一个测试用例里面的点。一个没有点的测试用例标志着整个输入的结束。

Output
对每个测试用例,在1行内输出2对整数,其间用一个空格隔开。第1对整数是长方形框左下角的坐标,第2对整数是长方形框右上角的坐标。

Sample Input
12 56
23 56
13 10
0 0
12 34
0 0
0 0

Sample Output
12 10 23 56
12 34 12 34




java answer

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class TestMain {

    public static boolean flag = true;
    public static List<Integer> xPos=new ArrayList<>();
    public  static List<Integer> yPos=new ArrayList<>();
    public static int count=0;
    public static void main(String[] args) {

        while (flag) {
            count =0;
                Scanner scan = new Scanner(System.in);
                String i = scan.nextLine();
                int x=Integer.parseInt(i.substring(0,i.indexOf(" ")));
                int y=Integer.parseInt(i.substring(i.indexOf(" ")+1,i.length()));
                if(x==0&&y==0){
                    count ++;
                }
                if(count!=1){
                    xPos.add(x);
                    yPos.add(y);
                }
            if(count==1){
                flag=false;
            }
        }
        System.out.println(xPos.toString());
        System.out.println(yPos.toString());
        Collections.sort(xPos);
        Collections.sort(yPos);
        if(xPos.size()!=0){
        System.out.println(xPos.get(0)+" "+yPos.get(0)+" "+xPos.get(xPos.size()-1)+" "+yPos.get(yPos.size()-1));
        }
    }
    
}

运行结果:



猜你喜欢

转载自blog.csdn.net/always_myhometown/article/details/78059414