public class ZhiJieTiaoDong { /* 给定一个数组:组合成最大数值 */ public String szpj(int[] args){ if(null == args || args.length == 0){ return ""; } StringBuilder stringBuilder = new StringBuilder(); int[] yxsz = compareSZ(args); for (int i = args.length - 1; i >= 0; i--){ stringBuilder.append(String.valueOf(args[i])); } return stringBuilder.toString(); } /* 排序 */ private int[] compareSZ(int[] args){ if(null == args){ throw new NullPointerException(); } if (args.length == 1){ return args; } int change = 0; for(int i = 0; i < args.length - 1; i++){ for(int j = i + 1; j < args.length; j++){ if(compare(args[i], args[j])){ change = args[i]; args[i] = args[j]; args[j] = change; } } } return args; } /* 比较两个数字哪个作高位排列 example: 9, 98 --> 998; 15, 18 --> 1815 */ private boolean compare(int a, int b){ String aa = String.valueOf(a); String bb = String.valueOf(b); int ab = Integer.valueOf(aa + bb); int ba = Integer.valueOf(bb + aa); if(ab >= ba){ return true; }else { return false; } } public static void main(String[] args){ ZhiJieTiaoDong zhiJieTiaoDong = new ZhiJieTiaoDong(); int[] a = {10,21,13,24}; int[] b = {35,11,23,4,34}; int[] c = {0, 4,2, 32, 11, 22, 11}; System.out.println(zhiJieTiaoDong.szpj(c)); } }
原文地址:https://www.cnblogs.com/fqfanqi/p/10693444.html
时间: 2024-10-31 15:10:21