
显示1 2 3 4 5 6六个数字所有的排列组合
给定1,2,3,4,5,6六个数字,显示出这六个数字所组成的六位数排列组合,要求:4不能在第二位,6不能在第4位,3和5不能靠在一起例。使用六重循环的那个方法就不必答了。...
给定1,2,3,4,5,6六个数字,显示出这六个数字所组成的六位数排列组合,
要求:4不能在第二位,6不能在第4位,3和5不能靠在一起例。
使用六重循环的那个方法就不必答了。
还有一个思路是把所有可能的组合放到一个数组中,再把不合要求的剔除出去,但是不知道如何编码实现。
希望能够给个编码实现出来, 还有其它的方法也可以。
回答能够用的再追加30分吧。
fancy7x - 这个程序运行了一下, 只有几个百个结果, 像111111,222222,122221,等等这种结果根本就没有出来。
而且把数据改成1,2,3,4,5,6后5和3不能靠在一起的约束没有实现
而且用递归的代价比循环还高,这样的话,还不如直接用循环,而且程序看起来更直观, 展开
要求:4不能在第二位,6不能在第4位,3和5不能靠在一起例。
使用六重循环的那个方法就不必答了。
还有一个思路是把所有可能的组合放到一个数组中,再把不合要求的剔除出去,但是不知道如何编码实现。
希望能够给个编码实现出来, 还有其它的方法也可以。
回答能够用的再追加30分吧。
fancy7x - 这个程序运行了一下, 只有几个百个结果, 像111111,222222,122221,等等这种结果根本就没有出来。
而且把数据改成1,2,3,4,5,6后5和3不能靠在一起的约束没有实现
而且用递归的代价比循环还高,这样的话,还不如直接用循环,而且程序看起来更直观, 展开
展开全部
#include <stdio.h>
#define max 100000
int panduan1(long x)//判断3和5不在一起
{
long y;
int flag;
while(x!=0)
{
y=x;
if((y%100==35)||(y%100==53))
{
flag=0;
break;
}
x/=10;
}
if(x==0)
flag=1;
return flag;
}
int panduan2(long x)//去除包含0,7,8,9的数
{
long y;
int flag;
while(x!=0)
{
y=x;
if(y%10==7||y%10==8||y%10==9||y%10==0)
{
flag=0;
break;
}
x/=10;
}
if(x==0)
flag=1;
return flag;
}
void main()
{
long i,a[max],k=0;
for(i=111111;i<=666666;i++)
{
if(((i/10)%10!=4)&&((i/1000)%10!=6)&&(panduan1(i)==1)&&(panduan2(i)==1))
a[k++]=i;
}
for(i=0;i<k;i++)
printf("%ld ",a[i]);
}
#define max 100000
int panduan1(long x)//判断3和5不在一起
{
long y;
int flag;
while(x!=0)
{
y=x;
if((y%100==35)||(y%100==53))
{
flag=0;
break;
}
x/=10;
}
if(x==0)
flag=1;
return flag;
}
int panduan2(long x)//去除包含0,7,8,9的数
{
long y;
int flag;
while(x!=0)
{
y=x;
if(y%10==7||y%10==8||y%10==9||y%10==0)
{
flag=0;
break;
}
x/=10;
}
if(x==0)
flag=1;
return flag;
}
void main()
{
long i,a[max],k=0;
for(i=111111;i<=666666;i++)
{
if(((i/10)%10!=4)&&((i/1000)%10!=6)&&(panduan1(i)==1)&&(panduan2(i)==1))
a[k++]=i;
}
for(i=0;i<k;i++)
printf("%ld ",a[i]);
}
展开全部
你说的是可重排列吗?
fancy7x的程序实现的应该是只是一个全排列,不是可重排列。
可重排列就会有大概6^6种。
fancy7x的程序实现的应该是只是一个全排列,不是可重排列。
可重排列就会有大概6^6种。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Iterator;
import java.util.TreeSet;
public class Sort {
private String[] b = new String[] {"1","2","2","3","4","6"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet<String> set = new TreeSet<String>();
public static void main(String[] args) {
new Sort().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[j] = 0;
} else {
a[j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
System.out.println(string);
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// "4" can not be the third position.
if (result.indexOf(" 4 ") != 2) {
// Filt the duplicate value.
set.add(result);
}
}
for (int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() - 1);
visited[startIndex] = false;
}
}
接分。
import java.util.TreeSet;
public class Sort {
private String[] b = new String[] {"1","2","2","3","4","6"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet<String> set = new TreeSet<String>();
public static void main(String[] args) {
new Sort().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[j] = 0;
} else {
a[j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
System.out.println(string);
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// "4" can not be the third position.
if (result.indexOf(" 4 ") != 2) {
// Filt the duplicate value.
set.add(result);
}
}
for (int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() - 1);
visited[startIndex] = false;
}
}
接分。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询