| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 827 人关注过本帖
标题:如何编写0-1变量的排列问题
只看楼主 加入收藏
robbiezl
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-6-15
收藏
 问题点数:0 回复次数:7 
如何编写0-1变量的排列问题

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

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

搜索更多相关主题的帖子: 0-1变量 排列 编写 文档 txt 
2007-06-15 14:10
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
从0 --- 255(没有256)每个数逐次转换为2进制。输出。

转换可以用bitset或者其他的可行10转2的方法!

Fight  to win  or  die...
2007-06-15 14:27
robbiezl
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-6-15
收藏
得分:0 

我自己用了一个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;
}
}
}
}
}
}
}
}
}

2007-06-15 15:25
robbiezl
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-6-15
收藏
得分:0 
但是问题是,我把这些排列组合输入到XY.txt之后,txt文档中每一行都是一种组合
但是假如我想直接用一个数组取出第二行或者第三行那8个数怎么取呢??
2007-06-15 15:27
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

/*---------------------------------------------------------------------------
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);
}

}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-15 16:17
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

#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编辑过]


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-15 16:46
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

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


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-15 16:53
robbiezl
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-6-15
收藏
得分:0 
谢谢楼上的几位
2007-06-15 19:29
快速回复:如何编写0-1变量的排列问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012621 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved