这个我以前做练习做到过 的和你稍微不一样点 运行结果不是在屏幕上显示,是输出到C:\mytest\permuted.txt 里 你看懂了自己改下 import java.io.File; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel;class PermuteWords { // These are static class members so we can reference them in all the methods in the class static String phrase = "a b c"; static ByteBuffer buffer = null; static FileChannel wordsChannel = null; static String[] words = null; // Stores reference to array that will hold words from phrase static String[] permutation = null; // Stores reference to array that will hold a permutation of words from the phrase static int path = -1; // Stores the number of words accumulated in one permutation public static void main(String args[]) { // Extract the words from phrase into an array. // First figure out how many words there are, assuming words are separated by spaces... int fromIndex = -1; while(phrase.charAt(++fromIndex) == ' '); // Skip any leading spaces
int wordCount = 1; // At least one word while((fromIndex = phrase.indexOf(' ', fromIndex)) != -1) { wordCount++; // Skip contiguous spaces while(phrase.charAt(++fromIndex) == ' '); } words = new String[wordCount]; // Array to hold the words from the phrase permutation = new String[wordCount]; // Array to hold permuted words
// Now get the words fromIndex = -1; while(phrase.charAt(++fromIndex) == ' ') // Skip any leading spaces ; int toIndex = 0; // Word end position int index = 0; // Free element in words array while((toIndex = phrase.indexOf(' ', fromIndex)) != -1) { words[index++] = phrase.substring(fromIndex,toIndex); fromIndex = toIndex;
while(phrase.charAt(++fromIndex) == ' ') // Skip contiguous spaces ; } if(fromIndex<phrase.length()) { words[index] = phrase.substring(fromIndex, phrase.length()); } // Now create and write the file String fileName = "permuted.txt"; String directoryName = "C://mytest"; // Make sure we have a directory for the file File directory = new File(directoryName); if(!directory.exists() || (directory.exists() && !directory.isDirectory())) { directory.mkdir(); }
File wordsFile = new File(directory, fileName); // Create the output stream for the file FileOutputStream wordsOutputStream = null; try { wordsOutputStream = new FileOutputStream(wordsFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(1); } wordsChannel = wordsOutputStream.getChannel(); buffer = ByteBuffer.allocate(2*(phrase.length()+1)); // We add one to allow for '\n' at the beginning permute(0); // Create and write all permutations of words array try { wordsOutputStream.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } } private static void permute(int n) { permutation[n] = ++path == 0 ? null: words[path-1]; if(path == words.length) { write(); } else { for (int i = 0; i<words.length ; i++) { if(permutation[i] == null) { permute(i); } } } path--; permutation[n] = null; } // Writes a permutation to the file private static void write() { CharBuffer charBuffer = buffer.asCharBuffer(); for(int i = 0 ; i< permutation.length ; i++) charBuffer.put(i==0?'\n':' ').put(permutation[i]);
晕!复制粘贴是时多复制了一次~看这个 import java.io.File; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel;class PermuteWords { // These are static class members so we can reference them in all the methods in the class static String phrase = "a b c"; static ByteBuffer buffer = null; static FileChannel wordsChannel = null; static String[] words = null; // Stores reference to array that will hold words from phrase static String[] permutation = null; // Stores reference to array that will hold a permutation of words from the phrase static int path = -1; // Stores the number of words accumulated in one permutation public static void main(String args[]) { // Extract the words from phrase into an array. // First figure out how many words there are, assuming words are separated by spaces... int fromIndex = -1; while(phrase.charAt(++fromIndex) == ' '); // Skip any leading spaces
int wordCount = 1; // At least one word while((fromIndex = phrase.indexOf(' ', fromIndex)) != -1) { wordCount++; // Skip contiguous spaces while(phrase.charAt(++fromIndex) == ' '); } words = new String[wordCount]; // Array to hold the words from the phrase permutation = new String[wordCount]; // Array to hold permuted words
// Now get the words fromIndex = -1; while(phrase.charAt(++fromIndex) == ' ') // Skip any leading spaces ; int toIndex = 0; // Word end position int index = 0; // Free element in words array while((toIndex = phrase.indexOf(' ', fromIndex)) != -1) { words[index++] = phrase.substring(fromIndex,toIndex); fromIndex = toIndex;
while(phrase.charAt(++fromIndex) == ' ') // Skip contiguous spaces ; } if(fromIndex<phrase.length()) { words[index] = phrase.substring(fromIndex, phrase.length()); } // Now create and write the file String fileName = "permuted.txt"; String directoryName = "C://mytest"; // Make sure we have a directory for the file File directory = new File(directoryName); if(!directory.exists() || (directory.exists() && !directory.isDirectory())) { directory.mkdir(); }
File wordsFile = new File(directory, fileName); // Create the output stream for the file FileOutputStream wordsOutputStream = null; try { wordsOutputStream = new FileOutputStream(wordsFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(1); } wordsChannel = wordsOutputStream.getChannel(); buffer = ByteBuffer.allocate(2*(phrase.length()+1)); // We add one to allow for '\n' at the beginning permute(0); // Create and write all permutations of words array try { wordsOutputStream.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } } private static void permute(int n) { permutation[n] = ++path == 0 ? null: words[path-1]; if(path == words.length) { write(); } else { for (int i = 0; i<words.length ; i++) { if(permutation[i] == null) { permute(i); } } } path--; permutation[n] = null; } // Writes a permutation to the file private static void write() { CharBuffer charBuffer = buffer.asCharBuffer(); for(int i = 0 ; i< permutation.length ; i++) charBuffer.put(i==0?'\n':' ').put(permutation[i]);
的和你稍微不一样点 运行结果不是在屏幕上显示,是输出到C:\mytest\permuted.txt 里
你看懂了自己改下
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;class PermuteWords { // These are static class members so we can reference them in all the methods in the class
static String phrase = "a b c";
static ByteBuffer buffer = null;
static FileChannel wordsChannel = null; static String[] words = null; // Stores reference to array that will hold words from phrase
static String[] permutation = null; // Stores reference to array that will hold a permutation of words from the phrase
static int path = -1; // Stores the number of words accumulated in one permutation public static void main(String args[]) {
// Extract the words from phrase into an array.
// First figure out how many words there are, assuming words are separated by spaces...
int fromIndex = -1;
while(phrase.charAt(++fromIndex) == ' '); // Skip any leading spaces
int wordCount = 1; // At least one word
while((fromIndex = phrase.indexOf(' ', fromIndex)) != -1) {
wordCount++;
// Skip contiguous spaces
while(phrase.charAt(++fromIndex) == ' ');
}
words = new String[wordCount]; // Array to hold the words from the phrase
permutation = new String[wordCount]; // Array to hold permuted words
// Now get the words
fromIndex = -1;
while(phrase.charAt(++fromIndex) == ' ') // Skip any leading spaces
;
int toIndex = 0; // Word end position
int index = 0; // Free element in words array
while((toIndex = phrase.indexOf(' ', fromIndex)) != -1) {
words[index++] = phrase.substring(fromIndex,toIndex);
fromIndex = toIndex;
while(phrase.charAt(++fromIndex) == ' ') // Skip contiguous spaces
;
}
if(fromIndex<phrase.length()) {
words[index] = phrase.substring(fromIndex, phrase.length());
} // Now create and write the file
String fileName = "permuted.txt";
String directoryName = "C://mytest"; // Make sure we have a directory for the file
File directory = new File(directoryName);
if(!directory.exists() || (directory.exists() && !directory.isDirectory())) {
directory.mkdir();
}
File wordsFile = new File(directory, fileName); // Create the output stream for the file
FileOutputStream wordsOutputStream = null;
try {
wordsOutputStream = new FileOutputStream(wordsFile);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
wordsChannel = wordsOutputStream.getChannel();
buffer = ByteBuffer.allocate(2*(phrase.length()+1)); // We add one to allow for '\n' at the beginning
permute(0); // Create and write all permutations of words array try {
wordsOutputStream.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
private static void permute(int n) {
permutation[n] = ++path == 0 ? null: words[path-1];
if(path == words.length) {
write();
} else {
for (int i = 0; i<words.length ; i++) {
if(permutation[i] == null) {
permute(i);
}
}
}
path--;
permutation[n] = null;
} // Writes a permutation to the file
private static void write() {
CharBuffer charBuffer = buffer.asCharBuffer();
for(int i = 0 ; i< permutation.length ; i++)
charBuffer.put(i==0?'\n':' ').put(permutation[i]);
try {
wordsChannel.write(buffer);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
buffer.clear();
}
}
File wordsFile = new File(directory, fileName); // Create the output stream for the file
FileOutputStream wordsOutputStream = null;
try {
wordsOutputStream = new FileOutputStream(wordsFile);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
wordsChannel = wordsOutputStream.getChannel();
buffer = ByteBuffer.allocate(2*(phrase.length()+1)); // We add one to allow for '\n' at the beginning
permute(0); // Create and write all permutations of words array try {
wordsOutputStream.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
private static void permute(int n) {
permutation[n] = ++path == 0 ? null: words[path-1];
if(path == words.length) {
write();
} else {
for (int i = 0; i<words.length ; i++) {
if(permutation[i] == null) {
permute(i);
}
}
}
path--;
permutation[n] = null;
} // Writes a permutation to the file
private static void write() {
CharBuffer charBuffer = buffer.asCharBuffer();
for(int i = 0 ; i< permutation.length ; i++)
charBuffer.put(i==0?'\n':' ').put(permutation[i]);
try {
wordsChannel.write(buffer);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
buffer.clear();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;class PermuteWords { // These are static class members so we can reference them in all the methods in the class
static String phrase = "a b c";
static ByteBuffer buffer = null;
static FileChannel wordsChannel = null; static String[] words = null; // Stores reference to array that will hold words from phrase
static String[] permutation = null; // Stores reference to array that will hold a permutation of words from the phrase
static int path = -1; // Stores the number of words accumulated in one permutation public static void main(String args[]) {
// Extract the words from phrase into an array.
// First figure out how many words there are, assuming words are separated by spaces...
int fromIndex = -1;
while(phrase.charAt(++fromIndex) == ' '); // Skip any leading spaces
int wordCount = 1; // At least one word
while((fromIndex = phrase.indexOf(' ', fromIndex)) != -1) {
wordCount++;
// Skip contiguous spaces
while(phrase.charAt(++fromIndex) == ' ');
}
words = new String[wordCount]; // Array to hold the words from the phrase
permutation = new String[wordCount]; // Array to hold permuted words
// Now get the words
fromIndex = -1;
while(phrase.charAt(++fromIndex) == ' ') // Skip any leading spaces
;
int toIndex = 0; // Word end position
int index = 0; // Free element in words array
while((toIndex = phrase.indexOf(' ', fromIndex)) != -1) {
words[index++] = phrase.substring(fromIndex,toIndex);
fromIndex = toIndex;
while(phrase.charAt(++fromIndex) == ' ') // Skip contiguous spaces
;
}
if(fromIndex<phrase.length()) {
words[index] = phrase.substring(fromIndex, phrase.length());
} // Now create and write the file
String fileName = "permuted.txt";
String directoryName = "C://mytest"; // Make sure we have a directory for the file
File directory = new File(directoryName);
if(!directory.exists() || (directory.exists() && !directory.isDirectory())) {
directory.mkdir();
}
File wordsFile = new File(directory, fileName); // Create the output stream for the file
FileOutputStream wordsOutputStream = null;
try {
wordsOutputStream = new FileOutputStream(wordsFile);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
wordsChannel = wordsOutputStream.getChannel();
buffer = ByteBuffer.allocate(2*(phrase.length()+1)); // We add one to allow for '\n' at the beginning
permute(0); // Create and write all permutations of words array try {
wordsOutputStream.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
private static void permute(int n) {
permutation[n] = ++path == 0 ? null: words[path-1];
if(path == words.length) {
write();
} else {
for (int i = 0; i<words.length ; i++) {
if(permutation[i] == null) {
permute(i);
}
}
}
path--;
permutation[n] = null;
} // Writes a permutation to the file
private static void write() {
CharBuffer charBuffer = buffer.asCharBuffer();
for(int i = 0 ; i< permutation.length ; i++)
charBuffer.put(i==0?'\n':' ').put(permutation[i]);
try {
wordsChannel.write(buffer);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
buffer.clear();
}
}
好像在讲JList 时讲的, 具体怎么写我也也忘了, 建议参考一下哈
输入一组字符串,将其所有的输出形式都显出出来!!
列如:有一组"abc"字符串,它可以有这样的输出形式:abc,acb,bac,bca,cab,cba
*
*
迭代原理:
1.abc
2.(ab)c, (ac)b,(bc)a//把最后一个元素和这个元素前面的元素一一交换
3.(a,b)c, (b,a)c,(a,c)b,(c,a)b, (b,c)a, (c,b)a.把倒数第二个元素和他前面的元素一一交换
4.迭代,直到要交换的元素是第一个元素
*/package stringderi;import java.util.ArrayList;public class Main {
static String origin = "abcd";
public static void main(String[] args) {
int i,j;
int count = origin.length();
ArrayList current = new ArrayList();
ArrayList next = new ArrayList();
current.add(origin);//作为第一次循环的初始值
while(count !=1 ){//还没有到第一个元素
for(i=0;i<current.size();i++){
for(j=0;j<count;j++){
next.add(exchange((String)current.get(i), j, count-1));
//把要交换的元素(由count指示)和每一个前面的元素(j指示)进行交换
}
}
current.clear();
current.addAll(next);
next.clear();
count--;
}//while
for(i=0;i<current.size();i++){
System.out.println((String)current.get(i));
}
}
public static String exchange(String s, int i, int j){//交换i,j位置的字符
char c1='a';
char c2='a';
StringBuffer buffer = new StringBuffer(s);
if(i<s.length())
c1 = s.charAt(i);
if(j<s.length())
c2 = s.charAt(j);
buffer.setCharAt(i,c2);
buffer.setCharAt(j,c1);
return buffer.toString();
}
}
--------------------------------------------------------
回复人:druglee() ( 一级(初级)) 信誉:100 2006-11-12 19:01:49 得分:0
?
把A B C看成一个全图的3个点就行啦
-------------------------------------------------------------
任意字符串你不会把他们拆开啊~,在组成合适的字符串形式,做人要变通
这是我自己想的一点思路不知道能不能有人能完善它:
就是定义一个数组ARRAY,让数组中的ARRAY[0]与ARRAY[1]交换在用 for(int element;ARRAY) System.out.println(element);将其输出,再将交换后的数组的AYYAY[1]与ARRAY[2]交换再用
for(int element;ARRAY) System.out.println(element);将其输出,依次类推,直到
ARRAY[ARRAY.lenth-2]与ARRAY[ARRAY.lenth-1]交换,这样交换完以后再从前向后在交换^^^^^^^^^^当然这其中要用到循环语句!!!
请大家看看怎么样!过程大概是这样的,
就拿abc为列吧!
首先将a与b交换再输出即为:bac,再将a与c交换再输出即为;bca(这是第一次用a交换完)
将b与c交换再输出即为;cba,再将b与a交换输出;cab(这是第二次用b交换)
将c与a交换输出;acb,再将c与b交换输出;abc(这是第三次用c交换)
最后各种可能都输出了!!
但好象还是有问题呢!!
你们帮我看看吧
各位 如果打出来的全排 肯定会有重复的
请问这个问题 怎么解决?