时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
输入
第一行有一个整数 0< n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0< m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,
第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
样例输出
1 1 1
1 2 1
1 2 2
2 1 1
2 2 1
解法一:用三个数组存储编号、长、宽,先对编号排序,编号位置变换的同时变换对应长和宽的位置,同理再对长、宽排序,最后输出。
import java.util.Scanner;
public class YiZhongPaiXu {
static int[] num; // 存储编号
static int[] len; // 存储长
static int[] wid; // 存储宽
static String[] res; // 存储答案
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
res = new String[n];
for (int i = 0; i < n; i++) { // n组测试用例
Input(sc); // 输入第i+1组数据
// 先对编号从小到大排序
sort(1,num.length-1,num,len,wid);
// 再对长从小到大排序(编号相同则排序,不同跳过)
int lastIndex = 1; // 记录不同编号的第一个编号位置
for (int j = 1; j < num.length-1; j++) {
if(num[j]!=num[j+1]){
sort(lastIndex,j,len,num,wid);
lastIndex = j+1;
}
}
sort(lastIndex,num.length-1,len,num,wid);// 对最后一个排序
// 最后对宽排序
lastIndex = 1;
for (int j = 1; j < num.length-1; j++) {
if(num[j]!=num[j+1] || (num[j]==num[j+1] && len[j]!=len[j+1])){
sort(lastIndex,j,wid,num,len);
lastIndex = j+1;
}
}
sort(lastIndex,num.length-1,wid,num,len);
// 存放答案
res[i] = "";
String lastStr = ""; // 记录之前的存储,便于处理相同结果
for (int j = 1; j < num.length; j++) {
String t = num[j]+" "+len[j]+" "+wid[j];
if(!t.equals(lastStr)){
lastStr = t;
res[i] += t+"\n";
}
}
}
sc.close();
for (int i = 0; i < res.length; i++)
System.out.print(res[i]);
}
static void Input(Scanner sc){
int m = sc.nextInt();
num = new int[m+1];
len = new int[m+1];
wid = new int[m+1];
for (int j = 1; j <= m; j++) {
num[j] = sc.nextInt();
int t1 = sc.nextInt();
int t2 = sc.nextInt();
len[j] = Math.max(t1, t2);
wid[j] = Math.min(t1, t2);
}
}
static void swap(int[] arr, int a, int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
/**
* 冒泡排序,排序中交换num中数值的同时也交换对应的len、wid中的数值
* @param startIndex 排序数组的起始位置
* @param endIndex 排序数组的结束位置
* @param arr 排序的数组
* @param swapArr1 与排序数组对应的数组1
* @param swapArr2 与排序数组对应的数组2
*/
static void sort(int startIndex, int endIndex, int[] arr, int[] swapArr1, int[] swapArr2){
for (int i = startIndex, index = 1; i < endIndex; i++, index++) {
for (int j = startIndex; j <= endIndex-index; j++) {
if(arr[j]>arr[j+1]){
swap(arr,j,j+1);
swap(swapArr1,j,j+1);
swap(swapArr2,j,j+1);
}
}
}
}
}
解法二:创建数组对象,将编号、长、宽存到同一对象中,重写Comparable接口中的compareTo()方法,调用sort()函数对数组对象排序。(复制的代码,明显比第一种占用的内存少)
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
while(m-->0) {
int n = in.nextInt();
A8[] a = new A8[n];
for(int i=0;i<n;i++) {
a[i] = new A8(in.nextInt(),in.nextInt(),in.nextInt());
}
Arrays.sort(a);
System.out.println(a[0].b+" "+a[0].c+" "+a[0].k);
int pre = 0;
for(int i=1;i<n;i++) {
if(a[i].b!=a[pre].b||a[i].c!=a[pre].c||a[i].k!=a[pre].k) {
System.out.println(a[i].b+" "+a[i].c+" "+a[i].k);
}
pre++;
}
}
}
}
class A8 implements Comparable<A8> {
int b;
int c;
int k;
A8(int i,int j,int z) {
b = i;
c = Math.max(j, z);
k = Math.min(j, z);
}
@Override
public int compareTo(A8 a) {
if(b!=a.b) {
return b-a.b;
}
if(c!=a.c) {
return c-a.c;
}
return k-a.k;
}
}