每一列只能有一个1 其余全是0
怎样才能输出所有可能的排列
据说是有81种?
14 个解决方案
#1
四个C31的组合排列相乘,也就是3的4次方,不就是81了么,也就是说用4个for循环嵌套,每个循环3次,就可以输出了。
#2
还有啥简单算法么?
for循环太多了
for循环太多了
#3
首先,你数据量又不大,简单粗暴是王道,其次,就算数据量很大,你要输出打印81种结果,是不是的print81次,是不是也一样要循环81次,我们在每次循环完就打印输出,或者全部计算完存起来,再打印输出,有多大区别,后者还要占点点内存
#4
、不会写·
#5
想了半天,也没有想出来,哪位大神把程序贴出来,学习一下
#6
搞定了 抛砖
the 1 arry
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
the 2 arry
[0 0 0 0]
[0 0 0 1]
[1 1 1 0]
the 3 arry
[0 0 0 0]
[0 0 1 0]
[1 1 0 1]
the 4 arry
[0 0 0 0]
[0 0 1 1]
[1 1 0 0]
the 5 arry
[0 0 0 0]
[0 1 0 0]
[1 0 1 1]
the 6 arry
[0 0 0 0]
[0 1 0 1]
[1 0 1 0]
the 7 arry
[0 0 0 0]
[0 1 1 0]
[1 0 0 1]
the 8 arry
[0 0 0 0]
[0 1 1 1]
[1 0 0 0]
the 9 arry
[0 0 0 0]
[1 0 0 0]
[0 1 1 1]
the 10 arry
[0 0 0 0]
[1 0 0 1]
[0 1 1 0]
the 11 arry
[0 0 0 0]
[1 0 1 0]
[0 1 0 1]
the 12 arry
[0 0 0 0]
[1 0 1 1]
[0 1 0 0]
the 13 arry
[0 0 0 0]
[1 1 0 0]
[0 0 1 1]
the 14 arry
[0 0 0 0]
[1 1 0 1]
[0 0 1 0]
the 15 arry
[0 0 0 0]
[1 1 1 0]
[0 0 0 1]
the 16 arry
[0 0 0 0]
[1 1 1 1]
[0 0 0 0]
the 17 arry
[0 0 0 1]
[0 0 0 0]
[1 1 1 0]
the 18 arry
[0 0 0 1]
[0 0 1 0]
[1 1 0 0]
the 19 arry
[0 0 0 1]
[0 1 0 0]
[1 0 1 0]
the 20 arry
[0 0 0 1]
[0 1 1 0]
[1 0 0 0]
the 21 arry
[0 0 0 1]
[1 0 0 0]
[0 1 1 0]
the 22 arry
[0 0 0 1]
[1 0 1 0]
[0 1 0 0]
the 23 arry
[0 0 0 1]
[1 1 0 0]
[0 0 1 0]
the 24 arry
[0 0 0 1]
[1 1 1 0]
[0 0 0 0]
the 25 arry
[0 0 1 0]
[0 0 0 0]
[1 1 0 1]
the 26 arry
[0 0 1 0]
[0 0 0 1]
[1 1 0 0]
the 27 arry
[0 0 1 0]
[0 1 0 0]
[1 0 0 1]
the 28 arry
[0 0 1 0]
[0 1 0 1]
[1 0 0 0]
the 29 arry
[0 0 1 0]
[1 0 0 0]
[0 1 0 1]
the 30 arry
[0 0 1 0]
[1 0 0 1]
[0 1 0 0]
the 31 arry
[0 0 1 0]
[1 1 0 0]
[0 0 0 1]
the 32 arry
[0 0 1 0]
[1 1 0 1]
[0 0 0 0]
the 33 arry
[0 0 1 1]
[0 0 0 0]
[1 1 0 0]
the 34 arry
[0 0 1 1]
[0 1 0 0]
[1 0 0 0]
the 35 arry
[0 0 1 1]
[1 0 0 0]
[0 1 0 0]
the 36 arry
[0 0 1 1]
[1 1 0 0]
[0 0 0 0]
the 37 arry
[0 1 0 0]
[0 0 0 0]
[1 0 1 1]
the 38 arry
[0 1 0 0]
[0 0 0 1]
[1 0 1 0]
the 39 arry
[0 1 0 0]
[0 0 1 0]
[1 0 0 1]
the 40 arry
[0 1 0 0]
[0 0 1 1]
[1 0 0 0]
the 41 arry
[0 1 0 0]
[1 0 0 0]
[0 0 1 1]
the 42 arry
[0 1 0 0]
[1 0 0 1]
[0 0 1 0]
the 43 arry
[0 1 0 0]
[1 0 1 0]
[0 0 0 1]
the 44 arry
[0 1 0 0]
[1 0 1 1]
[0 0 0 0]
the 45 arry
[0 1 0 1]
[0 0 0 0]
[1 0 1 0]
the 46 arry
[0 1 0 1]
[0 0 1 0]
[1 0 0 0]
the 47 arry
[0 1 0 1]
[1 0 0 0]
[0 0 1 0]
the 48 arry
[0 1 0 1]
[1 0 1 0]
[0 0 0 0]
the 49 arry
[0 1 1 0]
[0 0 0 0]
[1 0 0 1]
the 50 arry
[0 1 1 0]
[0 0 0 1]
[1 0 0 0]
the 51 arry
[0 1 1 0]
[1 0 0 0]
[0 0 0 1]
the 52 arry
[0 1 1 0]
[1 0 0 1]
[0 0 0 0]
the 53 arry
[0 1 1 1]
[0 0 0 0]
[1 0 0 0]
the 54 arry
[0 1 1 1]
[1 0 0 0]
[0 0 0 0]
the 55 arry
[1 0 0 0]
[0 0 0 0]
[0 1 1 1]
the 56 arry
[1 0 0 0]
[0 0 0 1]
[0 1 1 0]
the 57 arry
[1 0 0 0]
[0 0 1 0]
[0 1 0 1]
the 58 arry
[1 0 0 0]
[0 0 1 1]
[0 1 0 0]
the 59 arry
[1 0 0 0]
[0 1 0 0]
[0 0 1 1]
the 60 arry
[1 0 0 0]
[0 1 0 1]
[0 0 1 0]
the 61 arry
[1 0 0 0]
[0 1 1 0]
[0 0 0 1]
the 62 arry
[1 0 0 0]
[0 1 1 1]
[0 0 0 0]
the 63 arry
[1 0 0 1]
[0 0 0 0]
[0 1 1 0]
the 64 arry
[1 0 0 1]
[0 0 1 0]
[0 1 0 0]
the 65 arry
[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
the 66 arry
[1 0 0 1]
[0 1 1 0]
[0 0 0 0]
the 67 arry
[1 0 1 0]
[0 0 0 0]
[0 1 0 1]
the 68 arry
[1 0 1 0]
[0 0 0 1]
[0 1 0 0]
the 69 arry
[1 0 1 0]
[0 1 0 0]
[0 0 0 1]
the 70 arry
[1 0 1 0]
[0 1 0 1]
[0 0 0 0]
the 71 arry
[1 0 1 1]
[0 0 0 0]
[0 1 0 0]
the 72 arry
[1 0 1 1]
[0 1 0 0]
[0 0 0 0]
the 73 arry
[1 1 0 0]
[0 0 0 0]
[0 0 1 1]
the 74 arry
[1 1 0 0]
[0 0 0 1]
[0 0 1 0]
the 75 arry
[1 1 0 0]
[0 0 1 0]
[0 0 0 1]
the 76 arry
[1 1 0 0]
[0 0 1 1]
[0 0 0 0]
the 77 arry
[1 1 0 1]
[0 0 0 0]
[0 0 1 0]
the 78 arry
[1 1 0 1]
[0 0 1 0]
[0 0 0 0]
the 79 arry
[1 1 1 0]
[0 0 0 0]
[0 0 0 1]
the 80 arry
[1 1 1 0]
[0 0 0 1]
[0 0 0 0]
the 81 arry
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]
#include <stdio.h>
#include <string.h>
//整数转换成二进制串
char* itobs(int n,char * ps)
{
int i;
static int size = 8 * sizeof(int);
for(i=size-1;i>=0;i--,n >>=1)
{
ps[i]=(01&n)+'0';
}
ps[size]='\0';
return ps;
}
main( )
{
int i=0,count=0;
char str[33]={0},real[13]={0};
for (i=0;i<=4095;i++)
{
itobs(i,str);
strncpy(real,&str[20],12);
if ( (real[0]-'0') + (real[4]-'0') + (real[8]-'0')==1 &&
(real[1]-'0') + (real[5]-'0') + (real[9]-'0')==1 &&
(real[2]-'0') + (real[6]-'0') + (real[10]-'0')==1 &&
(real[3]-'0') + (real[7]-'0') + (real[11]-'0')==1)
{
printf("the %d arry\n",(count++)+1);
printf("[%c %c %c %c]\n",real[0],real[1],real[2],real[3]);
printf("[%c %c %c %c]\n",real[4],real[5],real[6],real[7]);
printf("[%c %c %c %c]\n\n",real[8],real[9],real[10],real[11]);
}
}
}
the 1 arry
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
the 2 arry
[0 0 0 0]
[0 0 0 1]
[1 1 1 0]
the 3 arry
[0 0 0 0]
[0 0 1 0]
[1 1 0 1]
the 4 arry
[0 0 0 0]
[0 0 1 1]
[1 1 0 0]
the 5 arry
[0 0 0 0]
[0 1 0 0]
[1 0 1 1]
the 6 arry
[0 0 0 0]
[0 1 0 1]
[1 0 1 0]
the 7 arry
[0 0 0 0]
[0 1 1 0]
[1 0 0 1]
the 8 arry
[0 0 0 0]
[0 1 1 1]
[1 0 0 0]
the 9 arry
[0 0 0 0]
[1 0 0 0]
[0 1 1 1]
the 10 arry
[0 0 0 0]
[1 0 0 1]
[0 1 1 0]
the 11 arry
[0 0 0 0]
[1 0 1 0]
[0 1 0 1]
the 12 arry
[0 0 0 0]
[1 0 1 1]
[0 1 0 0]
the 13 arry
[0 0 0 0]
[1 1 0 0]
[0 0 1 1]
the 14 arry
[0 0 0 0]
[1 1 0 1]
[0 0 1 0]
the 15 arry
[0 0 0 0]
[1 1 1 0]
[0 0 0 1]
the 16 arry
[0 0 0 0]
[1 1 1 1]
[0 0 0 0]
the 17 arry
[0 0 0 1]
[0 0 0 0]
[1 1 1 0]
the 18 arry
[0 0 0 1]
[0 0 1 0]
[1 1 0 0]
the 19 arry
[0 0 0 1]
[0 1 0 0]
[1 0 1 0]
the 20 arry
[0 0 0 1]
[0 1 1 0]
[1 0 0 0]
the 21 arry
[0 0 0 1]
[1 0 0 0]
[0 1 1 0]
the 22 arry
[0 0 0 1]
[1 0 1 0]
[0 1 0 0]
the 23 arry
[0 0 0 1]
[1 1 0 0]
[0 0 1 0]
the 24 arry
[0 0 0 1]
[1 1 1 0]
[0 0 0 0]
the 25 arry
[0 0 1 0]
[0 0 0 0]
[1 1 0 1]
the 26 arry
[0 0 1 0]
[0 0 0 1]
[1 1 0 0]
the 27 arry
[0 0 1 0]
[0 1 0 0]
[1 0 0 1]
the 28 arry
[0 0 1 0]
[0 1 0 1]
[1 0 0 0]
the 29 arry
[0 0 1 0]
[1 0 0 0]
[0 1 0 1]
the 30 arry
[0 0 1 0]
[1 0 0 1]
[0 1 0 0]
the 31 arry
[0 0 1 0]
[1 1 0 0]
[0 0 0 1]
the 32 arry
[0 0 1 0]
[1 1 0 1]
[0 0 0 0]
the 33 arry
[0 0 1 1]
[0 0 0 0]
[1 1 0 0]
the 34 arry
[0 0 1 1]
[0 1 0 0]
[1 0 0 0]
the 35 arry
[0 0 1 1]
[1 0 0 0]
[0 1 0 0]
the 36 arry
[0 0 1 1]
[1 1 0 0]
[0 0 0 0]
the 37 arry
[0 1 0 0]
[0 0 0 0]
[1 0 1 1]
the 38 arry
[0 1 0 0]
[0 0 0 1]
[1 0 1 0]
the 39 arry
[0 1 0 0]
[0 0 1 0]
[1 0 0 1]
the 40 arry
[0 1 0 0]
[0 0 1 1]
[1 0 0 0]
the 41 arry
[0 1 0 0]
[1 0 0 0]
[0 0 1 1]
the 42 arry
[0 1 0 0]
[1 0 0 1]
[0 0 1 0]
the 43 arry
[0 1 0 0]
[1 0 1 0]
[0 0 0 1]
the 44 arry
[0 1 0 0]
[1 0 1 1]
[0 0 0 0]
the 45 arry
[0 1 0 1]
[0 0 0 0]
[1 0 1 0]
the 46 arry
[0 1 0 1]
[0 0 1 0]
[1 0 0 0]
the 47 arry
[0 1 0 1]
[1 0 0 0]
[0 0 1 0]
the 48 arry
[0 1 0 1]
[1 0 1 0]
[0 0 0 0]
the 49 arry
[0 1 1 0]
[0 0 0 0]
[1 0 0 1]
the 50 arry
[0 1 1 0]
[0 0 0 1]
[1 0 0 0]
the 51 arry
[0 1 1 0]
[1 0 0 0]
[0 0 0 1]
the 52 arry
[0 1 1 0]
[1 0 0 1]
[0 0 0 0]
the 53 arry
[0 1 1 1]
[0 0 0 0]
[1 0 0 0]
the 54 arry
[0 1 1 1]
[1 0 0 0]
[0 0 0 0]
the 55 arry
[1 0 0 0]
[0 0 0 0]
[0 1 1 1]
the 56 arry
[1 0 0 0]
[0 0 0 1]
[0 1 1 0]
the 57 arry
[1 0 0 0]
[0 0 1 0]
[0 1 0 1]
the 58 arry
[1 0 0 0]
[0 0 1 1]
[0 1 0 0]
the 59 arry
[1 0 0 0]
[0 1 0 0]
[0 0 1 1]
the 60 arry
[1 0 0 0]
[0 1 0 1]
[0 0 1 0]
the 61 arry
[1 0 0 0]
[0 1 1 0]
[0 0 0 1]
the 62 arry
[1 0 0 0]
[0 1 1 1]
[0 0 0 0]
the 63 arry
[1 0 0 1]
[0 0 0 0]
[0 1 1 0]
the 64 arry
[1 0 0 1]
[0 0 1 0]
[0 1 0 0]
the 65 arry
[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
the 66 arry
[1 0 0 1]
[0 1 1 0]
[0 0 0 0]
the 67 arry
[1 0 1 0]
[0 0 0 0]
[0 1 0 1]
the 68 arry
[1 0 1 0]
[0 0 0 1]
[0 1 0 0]
the 69 arry
[1 0 1 0]
[0 1 0 0]
[0 0 0 1]
the 70 arry
[1 0 1 0]
[0 1 0 1]
[0 0 0 0]
the 71 arry
[1 0 1 1]
[0 0 0 0]
[0 1 0 0]
the 72 arry
[1 0 1 1]
[0 1 0 0]
[0 0 0 0]
the 73 arry
[1 1 0 0]
[0 0 0 0]
[0 0 1 1]
the 74 arry
[1 1 0 0]
[0 0 0 1]
[0 0 1 0]
the 75 arry
[1 1 0 0]
[0 0 1 0]
[0 0 0 1]
the 76 arry
[1 1 0 0]
[0 0 1 1]
[0 0 0 0]
the 77 arry
[1 1 0 1]
[0 0 0 0]
[0 0 1 0]
the 78 arry
[1 1 0 1]
[0 0 1 0]
[0 0 0 0]
the 79 arry
[1 1 1 0]
[0 0 0 0]
[0 0 0 1]
the 80 arry
[1 1 1 0]
[0 0 0 1]
[0 0 0 0]
the 81 arry
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]
#7
就是这样写
#include <stdio.h>
int main( )
{
int cnt = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++){
int pos[] = { i, j, k, l };
printf("\n\cnt:%d\n", ++cnt);
for (int m = 0; m < 3; m++){
for (int n = 0; n < 4; n++){
printf("%d\t", pos[n] == m ? 1 : 0);
}
printf("\n");
}
}
return 0;
}
#8
突然想起楼主喜欢短的程序....
#include <stdio.h>
int main( )
{
for (int i = 0; i < 81; i++){
int pos[] = { i%3, (i/3)%3, (i/9)%3, i/27 };
printf("\narray:%d\n", 1+i);
for (int j = 0; j < 12; ++j){
printf("%d%c", (pos[j % 4] == (j / 4) ? 1 : 0), ((j % 4)==3)?'\n':' ');
}
}
return 0;
}
#9
#10
仅供参考:
//qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
if (w<1 || m<w) w=m;
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//递归式回溯法生成全排列
if (depth==w) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
if (v && depth>0) {
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
}
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);
return;
}
if (argc>=3) w=atoi(argv[2]);
if (-1==w) v=1;
Make_Map(argv[1]);
Find(0);
}
//C:\test>qplw
//qplw 要产生全排列的字符串 [限定长度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//
#11
仅供参考://qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
if (w<1 || m<w) w=m;
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//递归式回溯法生成全排列
if (depth==w) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
if (v && depth>0) {
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
}
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);
return;
}
if (argc>=3) w=atoi(argv[2]);
if (-1==w) v=1;
Make_Map(argv[1]);
Find(0);
}
//C:\test>qplw
//qplw 要产生全排列的字符串 [限定长度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//
#12
我这水是氚!
C:\>qplw 001
001
010
100
// 每列有且仅有一个1的三种可能性,对应二进制的124
C:\>qplw 111122224444 4
1111
1112
1114
1121
1122
1124
1141
1142
1144
1211
1212
1214
1221
1222
1224
1241
1242
1244
1411
1412
1414
1421
1422
1424
1441
1442
1444
2111
2112
2114
2121
2122
2124
2141
2142
2144
2211
2212
2214
2221
2222
2224
2241
2242
2244
2411
2412
2414
2421
2422
2424
2441
2442
2444
4111
4112
4114
4121
4122
4124
4141
4142
4144
4211
4212
4214
4221
4222
4224
4241
4242
4244
4411
4412
4414
4421
4422
4424
4441
4442
4444
//
// 以上为对应4列的所有排列共81种
// 比如
//
// 0000
// 1111对应0000
// 1111
//
// 0000
// 1112对应0001
// 1110
//
// …… ……
//
// 1111
// 4444对应0000
// 0000
#13
???不要随意答复~影响阅读~
#14
大神们的我的确看不懂
#1
四个C31的组合排列相乘,也就是3的4次方,不就是81了么,也就是说用4个for循环嵌套,每个循环3次,就可以输出了。
#2
四个C31的组合排列相乘,也就是3的4次方,不就是81了么,也就是说用4个for循环嵌套,每个循环3次,就可以输出了。
for循环太多了
#3
首先,你数据量又不大,简单粗暴是王道,其次,就算数据量很大,你要输出打印81种结果,是不是的print81次,是不是也一样要循环81次,我们在每次循环完就打印输出,或者全部计算完存起来,再打印输出,有多大区别,后者还要占点点内存
#4
首先,你数据量又不大,简单粗暴是王道,其次,就算数据量很大,你要输出打印81种结果,是不是的print81次,是不是也一样要循环81次,我们在每次循环完就打印输出,或者全部计算完存起来,再打印输出,有多大区别,后者还要占点点内存
、不会写·
#5
想了半天,也没有想出来,哪位大神把程序贴出来,学习一下
#6
搞定了 抛砖
the 1 arry
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
the 2 arry
[0 0 0 0]
[0 0 0 1]
[1 1 1 0]
the 3 arry
[0 0 0 0]
[0 0 1 0]
[1 1 0 1]
the 4 arry
[0 0 0 0]
[0 0 1 1]
[1 1 0 0]
the 5 arry
[0 0 0 0]
[0 1 0 0]
[1 0 1 1]
the 6 arry
[0 0 0 0]
[0 1 0 1]
[1 0 1 0]
the 7 arry
[0 0 0 0]
[0 1 1 0]
[1 0 0 1]
the 8 arry
[0 0 0 0]
[0 1 1 1]
[1 0 0 0]
the 9 arry
[0 0 0 0]
[1 0 0 0]
[0 1 1 1]
the 10 arry
[0 0 0 0]
[1 0 0 1]
[0 1 1 0]
the 11 arry
[0 0 0 0]
[1 0 1 0]
[0 1 0 1]
the 12 arry
[0 0 0 0]
[1 0 1 1]
[0 1 0 0]
the 13 arry
[0 0 0 0]
[1 1 0 0]
[0 0 1 1]
the 14 arry
[0 0 0 0]
[1 1 0 1]
[0 0 1 0]
the 15 arry
[0 0 0 0]
[1 1 1 0]
[0 0 0 1]
the 16 arry
[0 0 0 0]
[1 1 1 1]
[0 0 0 0]
the 17 arry
[0 0 0 1]
[0 0 0 0]
[1 1 1 0]
the 18 arry
[0 0 0 1]
[0 0 1 0]
[1 1 0 0]
the 19 arry
[0 0 0 1]
[0 1 0 0]
[1 0 1 0]
the 20 arry
[0 0 0 1]
[0 1 1 0]
[1 0 0 0]
the 21 arry
[0 0 0 1]
[1 0 0 0]
[0 1 1 0]
the 22 arry
[0 0 0 1]
[1 0 1 0]
[0 1 0 0]
the 23 arry
[0 0 0 1]
[1 1 0 0]
[0 0 1 0]
the 24 arry
[0 0 0 1]
[1 1 1 0]
[0 0 0 0]
the 25 arry
[0 0 1 0]
[0 0 0 0]
[1 1 0 1]
the 26 arry
[0 0 1 0]
[0 0 0 1]
[1 1 0 0]
the 27 arry
[0 0 1 0]
[0 1 0 0]
[1 0 0 1]
the 28 arry
[0 0 1 0]
[0 1 0 1]
[1 0 0 0]
the 29 arry
[0 0 1 0]
[1 0 0 0]
[0 1 0 1]
the 30 arry
[0 0 1 0]
[1 0 0 1]
[0 1 0 0]
the 31 arry
[0 0 1 0]
[1 1 0 0]
[0 0 0 1]
the 32 arry
[0 0 1 0]
[1 1 0 1]
[0 0 0 0]
the 33 arry
[0 0 1 1]
[0 0 0 0]
[1 1 0 0]
the 34 arry
[0 0 1 1]
[0 1 0 0]
[1 0 0 0]
the 35 arry
[0 0 1 1]
[1 0 0 0]
[0 1 0 0]
the 36 arry
[0 0 1 1]
[1 1 0 0]
[0 0 0 0]
the 37 arry
[0 1 0 0]
[0 0 0 0]
[1 0 1 1]
the 38 arry
[0 1 0 0]
[0 0 0 1]
[1 0 1 0]
the 39 arry
[0 1 0 0]
[0 0 1 0]
[1 0 0 1]
the 40 arry
[0 1 0 0]
[0 0 1 1]
[1 0 0 0]
the 41 arry
[0 1 0 0]
[1 0 0 0]
[0 0 1 1]
the 42 arry
[0 1 0 0]
[1 0 0 1]
[0 0 1 0]
the 43 arry
[0 1 0 0]
[1 0 1 0]
[0 0 0 1]
the 44 arry
[0 1 0 0]
[1 0 1 1]
[0 0 0 0]
the 45 arry
[0 1 0 1]
[0 0 0 0]
[1 0 1 0]
the 46 arry
[0 1 0 1]
[0 0 1 0]
[1 0 0 0]
the 47 arry
[0 1 0 1]
[1 0 0 0]
[0 0 1 0]
the 48 arry
[0 1 0 1]
[1 0 1 0]
[0 0 0 0]
the 49 arry
[0 1 1 0]
[0 0 0 0]
[1 0 0 1]
the 50 arry
[0 1 1 0]
[0 0 0 1]
[1 0 0 0]
the 51 arry
[0 1 1 0]
[1 0 0 0]
[0 0 0 1]
the 52 arry
[0 1 1 0]
[1 0 0 1]
[0 0 0 0]
the 53 arry
[0 1 1 1]
[0 0 0 0]
[1 0 0 0]
the 54 arry
[0 1 1 1]
[1 0 0 0]
[0 0 0 0]
the 55 arry
[1 0 0 0]
[0 0 0 0]
[0 1 1 1]
the 56 arry
[1 0 0 0]
[0 0 0 1]
[0 1 1 0]
the 57 arry
[1 0 0 0]
[0 0 1 0]
[0 1 0 1]
the 58 arry
[1 0 0 0]
[0 0 1 1]
[0 1 0 0]
the 59 arry
[1 0 0 0]
[0 1 0 0]
[0 0 1 1]
the 60 arry
[1 0 0 0]
[0 1 0 1]
[0 0 1 0]
the 61 arry
[1 0 0 0]
[0 1 1 0]
[0 0 0 1]
the 62 arry
[1 0 0 0]
[0 1 1 1]
[0 0 0 0]
the 63 arry
[1 0 0 1]
[0 0 0 0]
[0 1 1 0]
the 64 arry
[1 0 0 1]
[0 0 1 0]
[0 1 0 0]
the 65 arry
[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
the 66 arry
[1 0 0 1]
[0 1 1 0]
[0 0 0 0]
the 67 arry
[1 0 1 0]
[0 0 0 0]
[0 1 0 1]
the 68 arry
[1 0 1 0]
[0 0 0 1]
[0 1 0 0]
the 69 arry
[1 0 1 0]
[0 1 0 0]
[0 0 0 1]
the 70 arry
[1 0 1 0]
[0 1 0 1]
[0 0 0 0]
the 71 arry
[1 0 1 1]
[0 0 0 0]
[0 1 0 0]
the 72 arry
[1 0 1 1]
[0 1 0 0]
[0 0 0 0]
the 73 arry
[1 1 0 0]
[0 0 0 0]
[0 0 1 1]
the 74 arry
[1 1 0 0]
[0 0 0 1]
[0 0 1 0]
the 75 arry
[1 1 0 0]
[0 0 1 0]
[0 0 0 1]
the 76 arry
[1 1 0 0]
[0 0 1 1]
[0 0 0 0]
the 77 arry
[1 1 0 1]
[0 0 0 0]
[0 0 1 0]
the 78 arry
[1 1 0 1]
[0 0 1 0]
[0 0 0 0]
the 79 arry
[1 1 1 0]
[0 0 0 0]
[0 0 0 1]
the 80 arry
[1 1 1 0]
[0 0 0 1]
[0 0 0 0]
the 81 arry
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]
#include <stdio.h>
#include <string.h>
//整数转换成二进制串
char* itobs(int n,char * ps)
{
int i;
static int size = 8 * sizeof(int);
for(i=size-1;i>=0;i--,n >>=1)
{
ps[i]=(01&n)+'0';
}
ps[size]='\0';
return ps;
}
main( )
{
int i=0,count=0;
char str[33]={0},real[13]={0};
for (i=0;i<=4095;i++)
{
itobs(i,str);
strncpy(real,&str[20],12);
if ( (real[0]-'0') + (real[4]-'0') + (real[8]-'0')==1 &&
(real[1]-'0') + (real[5]-'0') + (real[9]-'0')==1 &&
(real[2]-'0') + (real[6]-'0') + (real[10]-'0')==1 &&
(real[3]-'0') + (real[7]-'0') + (real[11]-'0')==1)
{
printf("the %d arry\n",(count++)+1);
printf("[%c %c %c %c]\n",real[0],real[1],real[2],real[3]);
printf("[%c %c %c %c]\n",real[4],real[5],real[6],real[7]);
printf("[%c %c %c %c]\n\n",real[8],real[9],real[10],real[11]);
}
}
}
the 1 arry
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
the 2 arry
[0 0 0 0]
[0 0 0 1]
[1 1 1 0]
the 3 arry
[0 0 0 0]
[0 0 1 0]
[1 1 0 1]
the 4 arry
[0 0 0 0]
[0 0 1 1]
[1 1 0 0]
the 5 arry
[0 0 0 0]
[0 1 0 0]
[1 0 1 1]
the 6 arry
[0 0 0 0]
[0 1 0 1]
[1 0 1 0]
the 7 arry
[0 0 0 0]
[0 1 1 0]
[1 0 0 1]
the 8 arry
[0 0 0 0]
[0 1 1 1]
[1 0 0 0]
the 9 arry
[0 0 0 0]
[1 0 0 0]
[0 1 1 1]
the 10 arry
[0 0 0 0]
[1 0 0 1]
[0 1 1 0]
the 11 arry
[0 0 0 0]
[1 0 1 0]
[0 1 0 1]
the 12 arry
[0 0 0 0]
[1 0 1 1]
[0 1 0 0]
the 13 arry
[0 0 0 0]
[1 1 0 0]
[0 0 1 1]
the 14 arry
[0 0 0 0]
[1 1 0 1]
[0 0 1 0]
the 15 arry
[0 0 0 0]
[1 1 1 0]
[0 0 0 1]
the 16 arry
[0 0 0 0]
[1 1 1 1]
[0 0 0 0]
the 17 arry
[0 0 0 1]
[0 0 0 0]
[1 1 1 0]
the 18 arry
[0 0 0 1]
[0 0 1 0]
[1 1 0 0]
the 19 arry
[0 0 0 1]
[0 1 0 0]
[1 0 1 0]
the 20 arry
[0 0 0 1]
[0 1 1 0]
[1 0 0 0]
the 21 arry
[0 0 0 1]
[1 0 0 0]
[0 1 1 0]
the 22 arry
[0 0 0 1]
[1 0 1 0]
[0 1 0 0]
the 23 arry
[0 0 0 1]
[1 1 0 0]
[0 0 1 0]
the 24 arry
[0 0 0 1]
[1 1 1 0]
[0 0 0 0]
the 25 arry
[0 0 1 0]
[0 0 0 0]
[1 1 0 1]
the 26 arry
[0 0 1 0]
[0 0 0 1]
[1 1 0 0]
the 27 arry
[0 0 1 0]
[0 1 0 0]
[1 0 0 1]
the 28 arry
[0 0 1 0]
[0 1 0 1]
[1 0 0 0]
the 29 arry
[0 0 1 0]
[1 0 0 0]
[0 1 0 1]
the 30 arry
[0 0 1 0]
[1 0 0 1]
[0 1 0 0]
the 31 arry
[0 0 1 0]
[1 1 0 0]
[0 0 0 1]
the 32 arry
[0 0 1 0]
[1 1 0 1]
[0 0 0 0]
the 33 arry
[0 0 1 1]
[0 0 0 0]
[1 1 0 0]
the 34 arry
[0 0 1 1]
[0 1 0 0]
[1 0 0 0]
the 35 arry
[0 0 1 1]
[1 0 0 0]
[0 1 0 0]
the 36 arry
[0 0 1 1]
[1 1 0 0]
[0 0 0 0]
the 37 arry
[0 1 0 0]
[0 0 0 0]
[1 0 1 1]
the 38 arry
[0 1 0 0]
[0 0 0 1]
[1 0 1 0]
the 39 arry
[0 1 0 0]
[0 0 1 0]
[1 0 0 1]
the 40 arry
[0 1 0 0]
[0 0 1 1]
[1 0 0 0]
the 41 arry
[0 1 0 0]
[1 0 0 0]
[0 0 1 1]
the 42 arry
[0 1 0 0]
[1 0 0 1]
[0 0 1 0]
the 43 arry
[0 1 0 0]
[1 0 1 0]
[0 0 0 1]
the 44 arry
[0 1 0 0]
[1 0 1 1]
[0 0 0 0]
the 45 arry
[0 1 0 1]
[0 0 0 0]
[1 0 1 0]
the 46 arry
[0 1 0 1]
[0 0 1 0]
[1 0 0 0]
the 47 arry
[0 1 0 1]
[1 0 0 0]
[0 0 1 0]
the 48 arry
[0 1 0 1]
[1 0 1 0]
[0 0 0 0]
the 49 arry
[0 1 1 0]
[0 0 0 0]
[1 0 0 1]
the 50 arry
[0 1 1 0]
[0 0 0 1]
[1 0 0 0]
the 51 arry
[0 1 1 0]
[1 0 0 0]
[0 0 0 1]
the 52 arry
[0 1 1 0]
[1 0 0 1]
[0 0 0 0]
the 53 arry
[0 1 1 1]
[0 0 0 0]
[1 0 0 0]
the 54 arry
[0 1 1 1]
[1 0 0 0]
[0 0 0 0]
the 55 arry
[1 0 0 0]
[0 0 0 0]
[0 1 1 1]
the 56 arry
[1 0 0 0]
[0 0 0 1]
[0 1 1 0]
the 57 arry
[1 0 0 0]
[0 0 1 0]
[0 1 0 1]
the 58 arry
[1 0 0 0]
[0 0 1 1]
[0 1 0 0]
the 59 arry
[1 0 0 0]
[0 1 0 0]
[0 0 1 1]
the 60 arry
[1 0 0 0]
[0 1 0 1]
[0 0 1 0]
the 61 arry
[1 0 0 0]
[0 1 1 0]
[0 0 0 1]
the 62 arry
[1 0 0 0]
[0 1 1 1]
[0 0 0 0]
the 63 arry
[1 0 0 1]
[0 0 0 0]
[0 1 1 0]
the 64 arry
[1 0 0 1]
[0 0 1 0]
[0 1 0 0]
the 65 arry
[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
the 66 arry
[1 0 0 1]
[0 1 1 0]
[0 0 0 0]
the 67 arry
[1 0 1 0]
[0 0 0 0]
[0 1 0 1]
the 68 arry
[1 0 1 0]
[0 0 0 1]
[0 1 0 0]
the 69 arry
[1 0 1 0]
[0 1 0 0]
[0 0 0 1]
the 70 arry
[1 0 1 0]
[0 1 0 1]
[0 0 0 0]
the 71 arry
[1 0 1 1]
[0 0 0 0]
[0 1 0 0]
the 72 arry
[1 0 1 1]
[0 1 0 0]
[0 0 0 0]
the 73 arry
[1 1 0 0]
[0 0 0 0]
[0 0 1 1]
the 74 arry
[1 1 0 0]
[0 0 0 1]
[0 0 1 0]
the 75 arry
[1 1 0 0]
[0 0 1 0]
[0 0 0 1]
the 76 arry
[1 1 0 0]
[0 0 1 1]
[0 0 0 0]
the 77 arry
[1 1 0 1]
[0 0 0 0]
[0 0 1 0]
the 78 arry
[1 1 0 1]
[0 0 1 0]
[0 0 0 0]
the 79 arry
[1 1 1 0]
[0 0 0 0]
[0 0 0 1]
the 80 arry
[1 1 1 0]
[0 0 0 1]
[0 0 0 0]
the 81 arry
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]
#7
首先,你数据量又不大,简单粗暴是王道,其次,就算数据量很大,你要输出打印81种结果,是不是的print81次,是不是也一样要循环81次,我们在每次循环完就打印输出,或者全部计算完存起来,再打印输出,有多大区别,后者还要占点点内存
、不会写·
就是这样写
#include <stdio.h>
int main( )
{
int cnt = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++){
int pos[] = { i, j, k, l };
printf("\n\cnt:%d\n", ++cnt);
for (int m = 0; m < 3; m++){
for (int n = 0; n < 4; n++){
printf("%d\t", pos[n] == m ? 1 : 0);
}
printf("\n");
}
}
return 0;
}
#8
突然想起楼主喜欢短的程序....
#include <stdio.h>
int main( )
{
for (int i = 0; i < 81; i++){
int pos[] = { i%3, (i/3)%3, (i/9)%3, i/27 };
printf("\narray:%d\n", 1+i);
for (int j = 0; j < 12; ++j){
printf("%d%c", (pos[j % 4] == (j / 4) ? 1 : 0), ((j % 4)==3)?'\n':' ');
}
}
return 0;
}
#9
#10
仅供参考:
//qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
if (w<1 || m<w) w=m;
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//递归式回溯法生成全排列
if (depth==w) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
if (v && depth>0) {
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
}
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);
return;
}
if (argc>=3) w=atoi(argv[2]);
if (-1==w) v=1;
Make_Map(argv[1]);
Find(0);
}
//C:\test>qplw
//qplw 要产生全排列的字符串 [限定长度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//
#11
仅供参考://qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
if (w<1 || m<w) w=m;
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//递归式回溯法生成全排列
if (depth==w) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
if (v && depth>0) {
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
}
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);
return;
}
if (argc>=3) w=atoi(argv[2]);
if (-1==w) v=1;
Make_Map(argv[1]);
Find(0);
}
//C:\test>qplw
//qplw 要产生全排列的字符串 [限定长度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//
#12
我这水是氚!
C:\>qplw 001
001
010
100
// 每列有且仅有一个1的三种可能性,对应二进制的124
C:\>qplw 111122224444 4
1111
1112
1114
1121
1122
1124
1141
1142
1144
1211
1212
1214
1221
1222
1224
1241
1242
1244
1411
1412
1414
1421
1422
1424
1441
1442
1444
2111
2112
2114
2121
2122
2124
2141
2142
2144
2211
2212
2214
2221
2222
2224
2241
2242
2244
2411
2412
2414
2421
2422
2424
2441
2442
2444
4111
4112
4114
4121
4122
4124
4141
4142
4144
4211
4212
4214
4221
4222
4224
4241
4242
4244
4411
4412
4414
4421
4422
4424
4441
4442
4444
//
// 以上为对应4列的所有排列共81种
// 比如
//
// 0000
// 1111对应0000
// 1111
//
// 0000
// 1112对应0001
// 1110
//
// …… ……
//
// 1111
// 4444对应0000
// 0000
#13
???不要随意答复~影响阅读~
#14
大神们的我的确看不懂