JAVA中的结构体排序 【java】【基础】

这里我们来举几个例题来解决这个问题

例一:HRBUST 1095 最麻烦了

最麻烦了

 
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 1853(910 users)Total Accepted: 963(841 users)Rating:Special Judge: No
Description

有道是:“勤能补拙,熟能生巧;细节决定成败,态度决定一切,现象决定本质,现在决定未来;是游戏的围墙,最麻烦了。”
对于“最麻烦了”:
在这里我们需要对N个男人进行排序,规则如下:
每个男人有3个属性,ACM成绩,金钱,人品。
优先按照ACM成绩由大到小排名,成绩相同的则按照金钱由大到小排序,金钱相同的则按照人品有大到小排序。

Input

对于每组测试数据:
第一行,输入一个整数N表示有N个男人。(1 <= N <= 1000)
接下来N行,每行3个数字,表示该男人的ACM成绩,金钱,人品。(0 <= ACM成绩、金钱、人品 <= 10^9)

处理到文件结束
Output

对于每组测试数据:
输出N行,每行输出该男人的ACM成绩,金钱、人品。

Sample Input

3
100 50 1000000000
1000000000 50 100
10 50 100

Sample Output

1000000000 50 100
100 50 1000000000
10 50 100


这题是一题典型的结构体排序题:

上代码:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.*;  

class shu ///创建类
{
	int acm;
	int mon;
	int rp;
}

class cmp implements Comparator<shu> {
	public int compare(shu A, shu B) ///降序排序
	{
			if(A.acm==B.acm)
			{
				if(A.mon==B.mon)
				{
					if(A.rp<B.rp)
					{
						return 1;
					}
					else if(A.rp==B.rp)
					{
						return 0;
					}
					else
					{
						return -1;
					}
				}
				else
				{
					if(A.mon<B.mon)
					{
						return 1;
					}
					else if(A.mon==B.mon)
					{
						return 0;
					}
					else
					{
						return -1;
					}
				}
			}
			else
			{
				if(A.acm<B.acm)
				{
					return 1;
				}
				else if(A.acm==B.acm)
				{
					return 0;
				}
				else
				{
					return -1;
				}
			}

	}
}

public class Main {
	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);

		while (in.hasNext()) {
			
			shu num[] = new shu[1005];///创建类数组

			int n;
			n = in.nextInt();

			for (int i = 0; i < n; i++) {
				
				num[i]=new shu();///这个地方容易漏
				
				num[i].acm = in.nextInt();
				num[i].mon = in.nextInt();
				num[i].rp  = in.nextInt();
			}
			
			Arrays.sort(num, 0, n, new cmp());
			
			for (int i = 0; i < n; i++) {
				System.out.println(num[i].acm+" "+num[i].mon+" "+num[i].rp);
			}

		}
	}
}
//int compare(Object o1, Object o2) 返回一个基本类型的整型
//如果要按照升序排序,
//则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
//如果要按照降序排序
// 则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
//


不过这题中没有字符串的输入与输出。。。

我们这里补充一题带上字符串输入的题::HRBUST 1023 JiaoZhu and CS


在上代码前补充个函数的用法。

java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值。通过这个函数,我们可以实现字符串的字典序排序。




上代码:(这个代码提交会超时!!!!!!!!!!!!!

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.*;  

class shu ///创建类
{
	String name;///比较时用String
	int mon;
	int hunt;
}

class cmp implements Comparator<shu> {
	public int compare(shu A, shu B) 
	{
			if(A.hunt==B.hunt)
			{
				if(A.mon==B.mon)
				{
					int flag=(A.name).compareTo(B.name);///按字典序排序
					if(flag==0)
					{
						return 0;
					}
					else if(flag<0)
					{
						return -1;
					}
					else
					{
						return 1;
					}
				}
				else
				{
					if(A.mon==B.mon)
					{
						return 0;

					}
					else if(A.mon<B.mon)
					{
						return -1;
					}
					else
					{
						return 1;
					}
						
				}
			}
			else
			{

				if(A.hunt==B.hunt)
				{
					return 0;

				}
				else if(A.hunt<B.hunt)
				{
					return 1;
				}
				else
				{
					return -1;
				}
			}

			
	}


}

public class Main {
	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);

		while (in.hasNext()) {
			
			shu num[] = new shu[100005];///创建类数组

			int n;
			n = in.nextInt();

			for (int i = 0; i < n; i++) {
				
				num[i]=new shu();///这个地方容易漏
				
				num[i].name=in.next();
				num[i].hunt=in.nextInt();
				num[i].mon=in.nextInt();
			}
			
			Arrays.sort(num, 0, n, new cmp());
			
			for (int i = 0; i < n; i++) {
				System.out.println(num[i].name);
			}

		}
	}
}


大约就是酱紫。。♪(^∇^*)




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值