文件out.txt中,有如下内容:055 079 080 081 085 089 128 129 192 219
258 281 287 289 290 478 508 709 782 784
。
。
。
文件中有多行三个数字的内容,每行10组数据,
要求:将每个三位数所有的排列形式按从小到大的顺序显示出来,
如:058,最后显示为058 085 508 580 805 850055,最后显示为
055 505 550上面内容最后显示如下:(每行10组数据)
008 018 029 055 058 079 080 081 085 089
092 097 098 108 128 129 180 182 192 209
218 219 258 278 281 285 287 289 290 291
298 478 487 505 508 528 550 580 582 709
728 748 782 784 790 800 801 805 809 810
812 821 825 827 829 847 850 852 872 874
890 892 902 907 908 912 920 921 928 970
980 982等待大侠们的指导!!!!!!
258 281 287 289 290 478 508 709 782 784
。
。
。
文件中有多行三个数字的内容,每行10组数据,
要求:将每个三位数所有的排列形式按从小到大的顺序显示出来,
如:058,最后显示为058 085 508 580 805 850055,最后显示为
055 505 550上面内容最后显示如下:(每行10组数据)
008 018 029 055 058 079 080 081 085 089
092 097 098 108 128 129 180 182 192 209
218 219 258 278 281 285 287 289 290 291
298 478 487 505 508 528 550 580 582 709
728 748 782 784 790 800 801 805 809 810
812 821 825 827 829 847 850 852 872 874
890 892 902 907 908 912 920 921 928 970
980 982等待大侠们的指导!!!!!!
没有用基数排序的思想:参考 public static void main(String[] args) {
List list = new ArrayList();
List list1 = new ArrayList();
try {
String encoding = "GBK";
File file = new File("c:/aa.txt");
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
while ((lineTXT = bufferedReader.readLine()) != null) {
list.add(lineTXT.toString().trim());
}
read.close();
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
String[] s = list.get(i).toString().split(" ");
for (String ss : s) {
list1.add(ss);
} }
Collections.sort(list1);
int count=0;
for(int i=0;i<list1.size();i++){
System.out.print(list1.get(i)+" ");
count++;
if(count%10==0){
System.out.println();
}
}
}
class StringPermutation implements Iterable<String> { private static final long[] FACT_POOL;
static {
FACT_POOL = new long[21];
FACT_POOL[0] = 1;
for(int i=1; i<FACT_POOL.length; i++)
FACT_POOL[i] = FACT_POOL[i-1] * i;
}
private final String str;
StringPermutation(String str) {
assert str != null;
assert str.length() <= 20;
this.str = str;
}
@Override
public Iterator<String> iterator() {
return this.new IndexIterator();
}
public long getPermutationCount() {
return FACT_POOL[str.length()];
}
public String getPermutation(long index) {
char[] chars = str.toCharArray();
permute(chars, index);
return new String(chars);
}
private void permute(char[] arr, long index) {
for(int i = 0; i < arr.length - 1; i++) { int left = arr.length - i - 1;
long leftCount = FACT_POOL[left];
int curr = (int)(index / leftCount);
index %= leftCount;
if( curr > 0 ) { char temp = arr[curr + i];
for(int j = curr + i; j > i; j--)
arr[j] = arr[j - 1];
arr[i] = temp;
}
}
}
private class IndexIterator implements Iterator<String> {
private long index = 0; @Override
public boolean hasNext() {
return index < getPermutationCount();
} @Override
public String next() {
String result = getPermutation(index);
index++;
return result;
} @Override
public void remove() {
throw new UnsupportedOperationException("Not supported.");
}
}
}
则问题解决如下:import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;/**
*
* @date 14/03/2013
*/
public class NumberSort {
public static void main(String[] args) {
String[] strs = "055 079 080 081 085 089 128 129 192 219 258 281 287 289 290 478 508 709 782 784".split(" ");
Set<String> set = new TreeSet<String>();
for(String s : strs)
for(String perm : new StringPermutation(s))
set.add(perm);
String[] result = set.toArray(new String[set.size()]);
for(int i=0, len=result.length; i<len; i++) {
System.out.print(result[i]);
System.out.print(" ");
if( (i + 1) % 10 == 0 )
System.out.println("");
}
}
}
输出如下:run:
008 018 029 055 058 079 080 081 085 089
092 097 098 108 128 129 180 182 192 209
218 219 258 278 281 285 287 289 290 291
298 478 487 505 508 528 550 580 582 709
728 748 782 784 790 800 801 805 809 810
812 821 825 827 829 847 850 852 872 874
890 892 902 907 908 912 920 921 928 970
980 982 BUILD SUCCESSFUL (total time: 3 seconds)
如果嫌tree的建立效率太低,可以用hash set,然后再用quick sort或merge sort排序。
每次调用 str.toCharArray() 是我的失误,成员变量应该直接存储 char[],每次做array copy :
class StringPermutation implements Iterable<String> { private static final long[] FACT_POOL;
static {
FACT_POOL = new long[21];
FACT_POOL[0] = 1;
for(int i=1; i<FACT_POOL.length; i++)
FACT_POOL[i] = FACT_POOL[i-1] * i;
}
private final char[] str, copy;
StringPermutation(String str) {
assert str != null;
assert str.length() <= 20;
this.str = str.toCharArray();
this.copy = (char[])this.str.clone();
}
@Override
public Iterator<String> iterator() {
return this.new IndexIterator();
}
public long getPermutationCount() {
return FACT_POOL[str.length];
}
public String getPermutation(long index) {
System.arraycopy(str, 0, copy, 0, str.length);
permute(copy, index);
return new String(copy);
}
private void permute(char[] arr, long index) {
for(int i = 0; i < arr.length - 1; i++) { int left = arr.length - i - 1;
long leftCount = FACT_POOL[left];
int curr = (int)(index / leftCount);
index %= leftCount;
if( curr > 0 ) { char temp = arr[curr + i];
for(int j = curr + i; j > i; j--)
arr[j] = arr[j - 1];
arr[i] = temp;
}
}
}
private class IndexIterator implements Iterator<String> {
private long index = 0; @Override
public boolean hasNext() {
return index < getPermutationCount();
} @Override
public String next() {
String result = getPermutation(index);
index++;
return result;
} @Override
public void remove() {
throw new UnsupportedOperationException("Not supported.");
}
}
}
258 281 287 289 290 478 508 709 782 784
。
。
。
文件中有多行三个数字的内容,每行10组数据,
要求:将每个三位数所有的排列形式按从小到大的顺序显示出来,
如:058,最后显示为058 085 508 580 805 850055,最后显示为
055 505 550上面内容最后显示如下:(每行10组数据)
008 018 029 055 058 079 080 081 085 089
092 097 098 108 128 129 180 182 192 209
218 219 258 278 281 285 287 289 290 291
298 478 487 505 508 528 550 580 582 709
728 748 782 784 790 800 801 805 809 810
812 821 825 827 829 847 850 852 872 874
890 892 902 907 908 912 920 921 928 970
980 982等待大侠们的指导!!!!!!
要eclipse能直接编译过的代码!
public class Test {
static List list1 = new ArrayList(); public static void main(String[] args) { List list = new ArrayList();
try {
String encoding = "GBK";
File file = new File("c:/aa.txt");
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
while ((lineTXT = bufferedReader.readLine()) != null) {
list.add(lineTXT.toString().trim());
}
read.close();
} else {
}
} catch (Exception e) {
e.printStackTrace();
} for (int i = 0; i < list.size(); i++) {
String[] s = list.get(i).toString().split(" ");
for (String ss : s) {
String[] is;
is = ss.split("");
sort("", is);
} } Set set = new HashSet();
set.addAll(list1);
List newlist = new ArrayList();
newlist.addAll(set);
Collections.sort(newlist);
int count = 0;
for (int i = 0; i < newlist.size(); i++) {
System.out.print(newlist.get(i) + " ");
count++;
if (count % 10 == 0) {
System.out.println();
}
} } private static void sort(String prefix, String[] is2) {
if (is2.length == 1) {
list1.add(prefix + is2[0]);
} for (int i = 0; i < is2.length; i++) {
sort(prefix + is2[i], copy(is2, i));
}
} private static String[] copy(String[] is2, int index) {
String[] b = new String[is2.length - 1];
System.arraycopy(is2, 0, b, 0, index);
System.arraycopy(is2, index + 1, b, index, is2.length - index - 1);
return b;
}}
out.text文件中
第一行数据为空,或者数据中间行为空时,即有空行时,显示如下: 008 018 029 055 058 079 080 081 085
089 092 097 098 108 128 129 180 182 192
209 218 219 258 278 281 285 287 289 290
291 298 478 487 505 508 528 550 580 582
709 728 748 782 784 790 800 801 805 809
810 812 821 825 827 829 847 850 852 872
874 890 892 902 907 908 912 920 921 928
970 980 982
第一行数据,显示有点问题,请处理下!
public class Test {
static List list1 = new ArrayList(); public static void main(String[] args) { List list = new ArrayList();
try {
String encoding = "GBK";
File file = new File("c:/aa.txt");
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
while ((lineTXT = bufferedReader.readLine()) != null) {
if (!lineTXT.toString().trim().equals("")) {
list.add(lineTXT.toString().trim());
}
}
read.close();
} else {
}
} catch (Exception e) {
e.printStackTrace();
} for (int i = 0; i < list.size(); i++) {
String[] s = list.get(i).toString().trim().split(" +");
for (String ss : s) {
String[] is;
is = ss.split("");
sort("", is);
} } Set set = new HashSet();
set.addAll(list1);
List newlist = new ArrayList();
newlist.addAll(set);
Collections.sort(newlist);
int count = 0;
for (int i = 0; i < newlist.size(); i++) {
System.out.print(newlist.get(i).toString().trim() + " ");
count++;
if (count % 10 == 0) {
System.out.println();
}
} } private static void sort(String prefix, String[] is2) {
if (is2.length == 1) {
list1.add(prefix + is2[0]);
} for (int i = 0; i < is2.length; i++) {
sort(prefix + is2[i], copy(is2, i));
}
} private static String[] copy(String[] is2, int index) {
String[] b = new String[is2.length - 1];
System.arraycopy(is2, 0, b, 0, index);
System.arraycopy(is2, index + 1, b, index, is2.length - index - 1);
return b;
}}