给定一个任意长度的java数组,求数组内的数能组合出来的最大整数
给点高端的~小弟就会一点点 展开
随便给个例子,就能测试不通过 {878, 88, 8789,9}
/**
* 这里要返回字符串,因为int型能表示的范围过小
* @param nums
* @return
*/
public static String getMaxSum(int ... nums)
{
if (nums == null)
{
return "";
}
String[] numStrs = new String[nums.length];
int i = 0;
for (int num : nums) numStrs[i++] = num + "";
Arrays.sort(numStrs, new Comparator<String>()
{
/**
* 比较规则,从首字符开始比较,谁大谁优先
* 如果所有字符一样,长度相等,返回0,如果长度不一样,将长度长一些的多余部分与相同部分首字符比较,谁大谁优先。
* 例如:98 < 99 878 > 87 877 < 87 888 = 88
*
* 简单归纳一下可以看出一个比较简单的算法
* 先将两个字符串无限重复直至双方长度一致,然后比较大小即可
* @param o1
* @param o2
* @return
*/
@Override
public int compare(String o1, String o2)
{
int length1 = o1.toCharArray().length;
int length2 = o2.toCharArray().length;
//这里直接就取双方的乘积吧,楼主有兴趣可以写个方法来求最小公倍数
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < length2; i++) sb1.append(o1);
for (int i = 0; i < length1; i++) sb2.append(o2);
return sb2.toString().compareTo(sb1.toString());
}
});
StringBuilder sb = new StringBuilder();
for (String s : numStrs)
{
sb.append(s);
}
return sb.toString();
}
package zhidao;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
public class RecursionSubNSort
{
public static void main ( String[] args )
{
String string = "878, 88, 8789,9";
String[] A = string.split (",");
LinkedList<String[]> list = new LinkedList<String[]> ();
recursionSub (list, A.length, A, 0, -1);
LinkedList<BigInteger> result = new LinkedList<BigInteger> ();
for ( String[] strings : list )
{
String temp = Arrays.toString (strings).replaceAll ("[\\[\\],\\s]", "");
result.add (new BigInteger (temp));
}
System.out.println ("最大的数是:" + Collections.max (result));
}
private static LinkedList<String[]> recursionSub ( LinkedList<String[]> list, int count, String[] array, int ind,
int start, int... indexs )
{
start++;
if (start > count - 1)
{
return null;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = 0; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, 0, start, indexs);
if (start == count - 1)
{
String[] temp = new String[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
boolean flag = true;
L: for ( int i = 0; i < temp.length; i++ )
{
for ( int j = i + 1; j < temp.length; j++ )
{
if (temp[i] == temp[j])
{
flag = false;
break L;
}
}
}
if (flag)
{
list.add (temp);
}
}
}
return list;
}
}
1.第一个类
package com.me.small_test;
public class MyString implements Comparable<MyString> {
private String content;
public MyString(String content) {
this.content = content;
}
public MyString(int value) {
this.content = String.valueOf(value);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int compareTo(MyString o) {
if (content.charAt(0) == o.getContent().charAt(0)) {
if (content.length() > o.getContent().length()) {
return compareStrings(content, o.getContent());
} else if (content.length() < o.getContent().length()) {
return -compareStrings(o.getContent(), content);
}
}
return content.compareTo(o.getContent());
}
public int compareStrings(String s1, String s2) {
String suffix = s1.substring(s2.length());
for (int i = 0; i < suffix.length(); i++) {
if (s2.charAt(s2.length() - 1) > suffix.charAt(i)) {
return -1;
}
}
return 1;
}
}
2.第二个类
package com.me.small_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
Test t = new Test();
int[] numbers = new int[] { 9, 98, 123, 32 };
List<MyString> nums = t.convertToMyStringArray(numbers);
StringBuffer sb = new StringBuffer("");
for (int i = nums.size() - 1; i >= 0; i--) {
sb.append(nums.get(i).getContent());
}
System.out.println(sb.toString());
}
public List<MyString> convertToMyStringArray(int[] a) {
List<MyString> list = new ArrayList<MyString>();
for (int value : a) {
list.add(new MyString(value));
}
Collections.sort(list);
return list;
}
}
改了。
给的是整数的话,很好处理的,假设数组是arr
java.util.Arrays.sort(arr);
StringBuilder strb = new StringBuilder();
for (int i = arr.length - 1; i >= 0; i--) {
strb.append(arr[i]);
}
System.out.println(strb.toString());
你自己定义数组arr就好了,我测试过的,能得到结果,求采纳哦
首先拿出首位最大的那些数字,如果首位相同,长度的小的应该更大,放大前面。
1.第一个类
package com.me.small_test;
public class MyString implements Comparable<MyString> {
private String content;
public MyString(String content) {
this.content = content;
}
public MyString(int value) {
this.content = String.valueOf(value);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int compareTo(MyString o) {
if (content.charAt(0) == o.getContent().charAt(0)) {
if (content.length() > o.getContent().length()) {
return compareStrings(content, o.getContent());
} else if (content.length() < o.getContent().length()) {
return -compareStrings(o.getContent(), content);
}
}
return content.compareTo(o.getContent());
}
public int compareStrings(String s1, String s2) {
String suffix = s1.substring(s2.length());
for (int i = 0; i < suffix.length(); i++) {
if (s2.charAt(s2.length() - 1) > suffix.charAt(i)) {
return -1;
}
}
return 1;
}
}
2.第二个类
package com.me.small_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
Test t = new Test();
int[] numbers = new int[] { 9, 98, 123, 32 };
List<MyString> nums = t.convertToMyStringArray(numbers);
StringBuffer sb = new StringBuffer("");
for (int i = nums.size() - 1; i >= 0; i--) {
sb.append(nums.get(i).getContent());
}
System.out.println(sb.toString());
}
public List<MyString> convertToMyStringArray(int[] a) {
List<MyString> list = new ArrayList<MyString>();
for (int value : a) {
list.add(new MyString(value));
}
Collections.sort(list);
return list;
}
}