算法竞赛入门-盒子(Box )

1、题目

给定6个矩形的长和宽wi和hi(1<=w, hi <= 1000),判断它们能否构成长方体的六个面。

输入:

1345 2584
 2584 683
 2584 1345
 683 1345
 683 1345

 2584 683

输出:

 POSSIBLE

2、思路

 两两比较,在之前排序,然后进行0-1比较,2-3比较,4-5比较。

最后再判断一次0里面的长宽对应2的长,4的宽或者2和4宽(必须在之前排序);

3、代码

package basic.第三章;


import java.util.Arrays;
import java.util.Scanner;

/**
 * 盒子
 * Created by Administrator on 2018/05/02.
 * 给定6个矩形的长和宽wi和hi(1<=w, hi <= 1000),判断它们能否构成长方体的六个面。
 * Sample Input
 * 1345 2584
 * 2584 683
 * 2584 1345
 * 683 1345
 * 683 1345
 * 2584 683
 * 

* 1234 4567 * 1234 4567 * 4567 4321 * 4322 4567 * 4321 1234 * 4321 1234 * Sample Output * POSSIBLE * IMPOSSIBLE * 思路: 根据矩形的特点,两两匹配即可 * * @author 春风吹又生 */ public class Box { public static void main(String[] args) { Scanner read = new Scanner(System.in); int count = 0; Rectan[] rectans = new Rectan[6]; while (count <= 5) { String[] data = read.nextLine().split(" "); rectans[count] = new Rectan(); if (Integer.parseInt(data[0]) > Integer.parseInt(data[1])) { rectans[count].l = Integer.parseInt(data[0]); rectans[count++].w = Integer.parseInt(data[1]); } else { rectans[count].l = Integer.parseInt(data[1]); rectans[count++].w = Integer.parseInt(data[0]); } } Arrays.sort(rectans); /* for (Rectan rectan : rectans) { System.out.println(rectan); }*/ // 如果两两不等 if ((rectans[0].l != rectans[1].l || rectans[0].w != rectans[1].w) || (rectans[2].l != rectans[3].l || rectans[2].w != rectans[3].w) || (rectans[4].l != rectans[5].l || rectans[4].w != rectans[5].w)) { System.out.println("IMPOSSIBLE"); } else {// 因为排序,此时三条边最长的数据在第一条,然后进行匹配数据 if ((rectans[0].l != rectans[2].l) || (rectans[0].w != rectans[4].l) || (rectans[2].w != rectans[4].w)) System.out.println("IMPOSSIBLE"); else System.out.println("POSSIBLE"); } // }     // 长方形类 static class Rectan implements Comparable { int l; int w; public int compareTo(Rectan o) { return -((this.l - o.l) + this.w - o.w); } public String toString() { return "Rectan{" + "l=" + l + ", w=" + w + '}'; } } }


你可能感兴趣的:(Java,算法,算法竞赛入门)