用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
2007-02-26 11:19
1186 查看
package JavaSf;
/**
* 用1、2、2、3、4、5这六个数字,
* 用java写一个main函数,打印出所有不同的排列,
* 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
*/
import java.util.Iterator;
import java.util.LinkedList;
public class Test {
private LinkedList list = new LinkedList();
/**
* 进行排列
*/
private void PaiLie(String startStr, String str) {
if (str.length() > 1) {
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
char[] ch = str.toCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
String startStr1 = startStr + String.valueOf(ch[0]);
String s = (new String(ch)).substring(1,
(new String(ch)).length());
if ((!list.contains(startStr1 + s)) &&
isRight(startStr1 + s))
list.add(startStr1 + s);
PaiLie(startStr1, s);
}
}
}
}
/**
* 判断要求
*/
private boolean isRight(String s) {
if (s.charAt(2) == '4') {
return false;
}
if (s.indexOf("35") != -1 || s.indexOf("53") != -1) {
return false;
}
return true;
}
/**
* 打印
*/
private void display() {
Iterator it = list.listIterator();
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
public static void main(String[] args) {
if (args.length > 1) {
System.out.println("输入的参数不符合要求");
} else if (args[0].trim().length() < 3) {
System.out.println("输入的参数不符合要求");
} else {
Test test = new Test();
test.PaiLie("",args[0].trim());
test.display();
}
}
}
/**
* 用1、2、2、3、4、5这六个数字,
* 用java写一个main函数,打印出所有不同的排列,
* 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
*/
import java.util.Iterator;
import java.util.LinkedList;
public class Test {
private LinkedList list = new LinkedList();
/**
* 进行排列
*/
private void PaiLie(String startStr, String str) {
if (str.length() > 1) {
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
char[] ch = str.toCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
String startStr1 = startStr + String.valueOf(ch[0]);
String s = (new String(ch)).substring(1,
(new String(ch)).length());
if ((!list.contains(startStr1 + s)) &&
isRight(startStr1 + s))
list.add(startStr1 + s);
PaiLie(startStr1, s);
}
}
}
}
/**
* 判断要求
*/
private boolean isRight(String s) {
if (s.charAt(2) == '4') {
return false;
}
if (s.indexOf("35") != -1 || s.indexOf("53") != -1) {
return false;
}
return true;
}
/**
* 打印
*/
private void display() {
Iterator it = list.listIterator();
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
public static void main(String[] args) {
if (args.length > 1) {
System.out.println("输入的参数不符合要求");
} else if (args[0].trim().length() < 3) {
System.out.println("输入的参数不符合要求");
} else {
Test test = new Test();
test.PaiLie("",args[0].trim());
test.display();
}
}
}
相关文章推荐
- Java经典算法集——如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"与"5"不能相连.
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。用1、2、2、3、4、5这六
- 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- // 40、用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, // 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- 用1、2、3、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、3、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 要求:"4"不能在第三位,"3"与"5"不能相连。
- 该公司笔试题就1个,要求在10分钟内作完。 题目如下:用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, 如:512234、412325等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 用 1,2,2,3,4,5 六个数字,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 【源码】用1,2,2,3,4,5这六个数字,写一个函数,打印出所有不同的排序,要求:4不能放在第三位,3与5不能相连(C语言实现)
- 2013年用友校招编程题:用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列
- 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列
- 面试题─用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列
- "4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列