算法编程题:
输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个
输入:
32,231
输出:
23132
import java.util.Arrays;
import java.util.Scanner;
public class Main7
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
// 读取输入,直到没有整型数据可读
// while(cin.hasNextInt())
// while (cin.hasNextLine())
while (cin.hasNextLine())
{
String s = cin.nextLine();
String[] str = sort(s);
if (str.length > 0)
{
System.out.println(Arrays.toString(str));
}
}
}
public static String[] sort(String s)
{
String[] arr = s.split(",");
while (true)
{
boolean ok = true;
for (int j = 0; j < arr.length - 1; j++)
{
if (!checkSwap(arr[j], arr[j + 1]))
{
String tmp = "";
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
ok = false;
System.out.println(Arrays.toString(arr));
}
}
if (ok) break;
}
return arr;
}
/**
*
* @Description: 检查是否需要交换两个数
* @param str1
* @param str2
* @return boolean
* @throws
*/
public static boolean checkSwap(String str1, String str2)
{
long int1 = Long.parseLong(str1 + str2);
long int2 = Long.parseLong(str2 + str1);
if (int2 > int1)
{
return true;
}
else
{
return false;
}
}
}
输出结果如下:
输入: 9191,1912,8,7,32,182,13,56,6,5,211,812
第一趟 [1912, 9191, 8, 7, 32, 182, 13, 56, 6, 5, 211, 812]
第二趟 [1912, 8, 9191, 7, 32, 182, 13, 56, 6, 5, 211, 812]
[1912, 8, 7, 9191, 32, 182, 13, 56, 6, 5, 211, 812]
[1912, 8, 7, 32, 9191, 182, 13, 56, 6, 5, 211, 812]
[1912, 8, 7, 32, 182, 9191, 13, 56, 6, 5, 211, 812]
[1912, 8, 7, 32, 182, 13, 9191, 56, 6, 5, 211, 812]
[1912, 8, 7, 32, 182, 13, 56, 9191, 6, 5, 211, 812]
[1912, 8, 7, 32, 182, 13, 56, 6, 9191, 5, 211, 812]
[1912, 8, 7, 32, 182, 13, 56, 6, 5, 9191, 211, 812]
[1912, 8, 7, 32, 182, 13, 56, 6, 5, 211, 9191, 812]
[1912, 8, 7, 32, 182, 13, 56, 6, 5, 211, 812, 9191]
[1912, 7, 8, 32, 182, 13, 56, 6, 5, 211, 812, 9191]
[1912, 7, 32, 8, 182, 13, 56, 6, 5, 211, 812, 9191]
[1912, 7, 32, 182, 8, 13, 56, 6, 5, 211, 812, 9191]
[1912, 7, 32, 182, 13, 8, 56, 6, 5, 211, 812, 9191]
[1912, 7, 32, 182, 13, 56, 8, 6, 5, 211, 812, 9191]
[1912, 7, 32, 182, 13, 56, 6, 8, 5, 211, 812, 9191]
[1912, 7, 32, 182, 13, 56, 6, 5, 8, 211, 812, 9191]
[1912, 7, 32, 182, 13, 56, 6, 5, 211, 8, 812, 9191]
[1912, 7, 32, 182, 13, 56, 6, 5, 211, 812, 8, 9191]
[1912, 32, 7, 182, 13, 56, 6, 5, 211, 812, 8, 9191]
[1912, 32, 182, 7, 13, 56, 6, 5, 211, 812, 8, 9191]
[1912, 32, 182, 13, 7, 56, 6, 5, 211, 812, 8, 9191]
[1912, 32, 182, 13, 56, 7, 6, 5, 211, 812, 8, 9191]
[1912, 32, 182, 13, 56, 6, 7, 5, 211, 812, 8, 9191]
[1912, 32, 182, 13, 56, 6, 5, 7, 211, 812, 8, 9191]
[1912, 32, 182, 13, 56, 6, 5, 211, 7, 812, 8, 9191]
[1912, 182, 32, 13, 56, 6, 5, 211, 7, 812, 8, 9191]
[1912, 182, 13, 32, 56, 6, 5, 211, 7, 812, 8, 9191]
[1912, 182, 13, 32, 56, 5, 6, 211, 7, 812, 8, 9191]
[1912, 182, 13, 32, 56, 5, 211, 6, 7, 812, 8, 9191]
[182, 1912, 13, 32, 56, 5, 211, 6, 7, 812, 8, 9191]
[182, 13, 1912, 32, 56, 5, 211, 6, 7, 812, 8, 9191]
[182, 13, 1912, 32, 5, 56, 211, 6, 7, 812, 8, 9191]
[182, 13, 1912, 32, 5, 211, 56, 6, 7, 812, 8, 9191]
[13, 182, 1912, 32, 5, 211, 56, 6, 7, 812, 8, 9191]
[13, 182, 1912, 32, 211, 5, 56, 6, 7, 812, 8, 9191]
[13, 182, 1912, 211, 32, 5, 56, 6, 7, 812, 8, 9191]
[13, 182, 1912, 211, 32, 5, 56, 6, 7, 812, 8, 9191]