java中排列组合问题
请输出由2,4,4,6,8,9组成的所有6不在千位切8,9不相临的的六位数,并统计个数注意:有两个4...
请输出由2,4,4,6,8,9组成的所有6不在千位切8,9不相临的的六位数,并统计个数
注意:有两个4 展开
注意:有两个4 展开
展开全部
这个程序希望能帮到你。
我的output
.....(太多了,省略)
442869
442869
424869
244869
424869
244869
Total weird numbers: 396
............permu time: 13 milliseconds..........
程序代码如下:
package test;
import java.util.Date;
/**
*
* @author liang.rao@gmail.com
* @date 16.Nov.2007
* @why zhidao.baidu.com : question/39707738.html
*
*/
public class WeirdNumbers {
public static int counter = 0;
// print N! permutation of the elements of array a (not in order)
public static void perm2(String s) {
int N = s.length();
char[] a = new char[N];
for (int i = 0; i < N; i++)
a[i] = s.charAt(i);
perm2(a, N);
}
private static void perm2(char[] a, int n) {
if (n == 1 && numberControl(a)) {
System.out.println(a);
counter++;
return;
}
for (int i = 0; i < n; i++) {
swap(a, i, n - 1);
perm2(a, n - 1);
swap(a, i, n - 1);
}
}
// swap the characters at indices i and j
private static void swap(char[] a, int i, int j) {
char c;
c = a[i];
a[i] = a[j];
a[j] = c;
}
private static boolean numberControl(char[] a) {
boolean allOk = true;
if (a[1]=='6')
allOk = false;
for(int i = 0; i < a.length-1; i++ ) {
if((a[i]=='8' && a[i+1]=='9')||(a[i]=='9' && a[i+1]=='8'))
allOk = false;
}
return allOk;
}
public static void main(String[] args) {
String elements = "244689";
Date dt3 = new Date();
perm2(elements);
Date dt4 = new Date();
System.out.println("Total weird numbers: " + counter);
System.out.println("............permu time: " + (dt4.getTime() - dt3.getTime())
+ " milliseconds..........");
}
}
要得到完整的output还有两个办法一是把它写入
new PrintStream(new FileOutputStream("output.txt")); 对象里面
或者用下面这个办法进行排版
if (n == 1 && numberControl(a)) {
System.out.print(a);
System.out.print(" ");
if (counter % 10 == 9)
System.out.println("");
counter++;
return;
}
我的output
.....(太多了,省略)
442869
442869
424869
244869
424869
244869
Total weird numbers: 396
............permu time: 13 milliseconds..........
程序代码如下:
package test;
import java.util.Date;
/**
*
* @author liang.rao@gmail.com
* @date 16.Nov.2007
* @why zhidao.baidu.com : question/39707738.html
*
*/
public class WeirdNumbers {
public static int counter = 0;
// print N! permutation of the elements of array a (not in order)
public static void perm2(String s) {
int N = s.length();
char[] a = new char[N];
for (int i = 0; i < N; i++)
a[i] = s.charAt(i);
perm2(a, N);
}
private static void perm2(char[] a, int n) {
if (n == 1 && numberControl(a)) {
System.out.println(a);
counter++;
return;
}
for (int i = 0; i < n; i++) {
swap(a, i, n - 1);
perm2(a, n - 1);
swap(a, i, n - 1);
}
}
// swap the characters at indices i and j
private static void swap(char[] a, int i, int j) {
char c;
c = a[i];
a[i] = a[j];
a[j] = c;
}
private static boolean numberControl(char[] a) {
boolean allOk = true;
if (a[1]=='6')
allOk = false;
for(int i = 0; i < a.length-1; i++ ) {
if((a[i]=='8' && a[i+1]=='9')||(a[i]=='9' && a[i+1]=='8'))
allOk = false;
}
return allOk;
}
public static void main(String[] args) {
String elements = "244689";
Date dt3 = new Date();
perm2(elements);
Date dt4 = new Date();
System.out.println("Total weird numbers: " + counter);
System.out.println("............permu time: " + (dt4.getTime() - dt3.getTime())
+ " milliseconds..........");
}
}
要得到完整的output还有两个办法一是把它写入
new PrintStream(new FileOutputStream("output.txt")); 对象里面
或者用下面这个办法进行排版
if (n == 1 && numberControl(a)) {
System.out.print(a);
System.out.print(" ");
if (counter % 10 == 9)
System.out.println("");
counter++;
return;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询