注册 登录
编程论坛 C++教室

如何编写0-1变量的排列问题

robbiezl 发布于 2007-06-15 14:10, 827 次点击

假设有8个0-1变量,分别是x1至x8,每个变量只能取0或者取1
那么对这8个变量共有2的8次幂种组合即256种组合
请问用c++如何能自动生成这256种组合
最好能自动把这种组合的结果存在一个txt文档里面,感激不尽

[此贴子已经被作者于2007-6-15 14:24:35编辑过]

7 回复
#2
aipb20072007-06-15 14:27
从0 --- 255(没有256)每个数逐次转换为2进制。输出。

转换可以用bitset或者其他的可行10转2的方法!
#3
robbiezl2007-06-15 15:25

我自己用了一个8重的循环
void init_xy(){
int x[9];
ofstream ouf("XY.txt");
for(x[1]=0;x[1]<=1;x[1]++){
for(x[2]=0;x[2]<=1;x[2]++){
for(x[3]=0;x[3]<=1;x[3]++){
for(x[4]=0;x[4]<=1;x[4]++){
for(x[5]=0;x[5]<=1;x[5]++){
for(x[6]=0;x[6]<=1;x[6]++){
for(x[7]=0;x[7]<=1;x[7]++){
for(x[8]=0;x[8]<=1;x[8]++){
ouf<<x[1]<<" "<<x[2]<<" "<<x[3]<<" "<<x[4]<<" "<<x[5]<<" "<<x[6]<<" "<<x[7]<<" "<<x[8]<<endl;
}
}
}
}
}
}
}
}
}

#4
robbiezl2007-06-15 15:27
但是问题是,我把这些排列组合输入到XY.txt之后,txt文档中每一行都是一种组合
但是假如我想直接用一个数组取出第二行或者第三行那8个数怎么取呢??
#5
HJin2007-06-15 16:17

/*---------------------------------------------------------------------------
File name: main.cpp
Author: HJin

6/15/2007 01:17:45


This is the problem of repeatable permutation, which needs basic
knowledge about recursive function calls.

The required 256 permutations are written in a.txt.

Sample output from Repeatable_Permutation( int )

1 0000
2 0001
3 0010
4 0011
5 0100
6 0101
7 0110
8 0111
9 1000
10 1001
11 1010
12 1011
13 1100
14 1101
15 1110
16 1111
Press any key to continue . . .

*/


#include <iostream>
#include <string>
#include <fstream>


using namespace std;


string s; // a global buffer

/**
Output to a ASCII file.
*/
void Repeatable_Permutation(int n, ofstream& ofs);

/**
Output to stdout.
*/
void Repeatable_Permutation(int n);


int main()
{
int n=4;
Repeatable_Permutation(n);

ofstream ofs("a.txt");
if(!ofs)
{
cout<<"cannot create a file\n";
exit(0);
}

n=8;
s=""; // clear s
Repeatable_Permutation(n, ofs);

ofs.close();

return 0;
}

void Repeatable_Permutation(int n, ofstream& ofs)
{
string temp;
if(n==0)
{
ofs<<s<<endl;
return;
}
else
{
temp = s; // remember the previous state of s

// write 0 and repeat n-1 times
s+="0";
Repeatable_Permutation(n-1, ofs);

// write 1 and repeat n-1 times
s = temp; // restore the previous state
s+="1"; // write 1
Repeatable_Permutation(n-1, ofs);
}
}


void Repeatable_Permutation(int n)
{
static int count=0; // counter
string temp;

if(n==0)
{
++count;
cout<<count<<"\t"<<s<<endl;
return;
}
else
{
temp = s;
s+="0";
Repeatable_Permutation(n-1);

s = temp;
s+="1";
Repeatable_Permutation(n-1);
}

}

#6
wfpb2007-06-15 16:46

#include <ctime>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cassert>
#include <iostream>
using namespace std;


enum RESULT_01 {R_INVALIDSTREAM,R_OUTOFMEMORY,R_SUCCESS};


RESULT_01 Create_01(ostream* os,int num)
{
    if(os->fail())
        return R_INVALIDSTREAM;
    srand(time(0));
    for (int i=0;i<num;i++)
    {
        char buf[10]={0};
        itoa(rand()%256,buf,2);
        os->fill('0');
        (*os)<<right<<setw(8)<<buf<<endl;
    }
    return R_SUCCESS;
}
RESULT_01 Read_01(istream* ins,int index,int& result)
{
    if(ins->fail())
        return R_INVALIDSTREAM;
    ins->seekg(0,ios::end);
    int len=ins->tellg();
    ins->seekg(0,ios::beg);
    if(index*10>=len)
        return R_OUTOFMEMORY;
    ins->seekg(index*10,ios::beg);
    char buf[10]={0};
    (*ins)>>buf;
    result=0;
    for(int i=0;i<strlen(buf);i++)
    {
        result+=(buf[8-i-1]-'0')*pow(2,i);
    }
    return R_SUCCESS;
}
void main()
{
    if(R_SUCCESS!=Create_01(new ofstream(\"C:\\1.txt\"),3))
    {
        cout<<\"failed create!\"<<endl;
        return;
    }
    int result=0;
    if(R_SUCCESS==Read_01(new ifstream(\"C:\\1.txt\"),2,result))
        cout<<result<<endl;
}

[此贴子已经被作者于2007-6-15 16:49:05编辑过]

#7
wfpb2007-06-15 16:53

对了,new以后忘记delete了,偷懒了。

#8
robbiezl2007-06-15 19:29
谢谢楼上的几位
1