假设有8个0-1变量,分别是x1至x8,每个变量只能取0或者取1
那么对这8个变量共有2的8次幂种组合即256种组合
请问用c++如何能自动生成这256种组合
最好能自动把这种组合的结果存在一个txt文档里面,感激不尽
[此贴子已经被作者于2007-6-15 14:24:35编辑过]
假设有8个0-1变量,分别是x1至x8,每个变量只能取0或者取1
那么对这8个变量共有2的8次幂种组合即256种组合
请问用c++如何能自动生成这256种组合
最好能自动把这种组合的结果存在一个txt文档里面,感激不尽
[此贴子已经被作者于2007-6-15 14:24:35编辑过]
我自己用了一个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;
}
}
}
}
}
}
}
}
}
/*---------------------------------------------------------------------------
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);
}
}
#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编辑过]