C++ 用c+打印所有排列+;
我想列出所有安排,以下是我的示例代码:C++ 用c+打印所有排列+;,c++,permutation,C++,Permutation,我想列出所有安排,以下是我的示例代码: const unsigned char item1[] = {'b'}; const unsigned char item2[] = { 'A', 'C' ,'D'}; const unsigned char item3[] = {'1','2'}; int _tmain(int argc, _TCHAR* argv[]) { for (int i = 0; i < sizeof(item1) / sizeof(unsigned char)
const unsigned char item1[] = {'b'};
const unsigned char item2[] = { 'A', 'C' ,'D'};
const unsigned char item3[] = {'1','2'};
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < sizeof(item1) / sizeof(unsigned char); i++){
for (int j = 0; j < sizeof(item2) / sizeof(unsigned char); j++){
for (int k = 0; k < sizeof(item3) / sizeof(unsigned char); k++){
printf("%c%c%c\n",item1[i],item2[j],item3[k]);
}
}
}
return 0;
}
const unsigned char item1[]={'b'};
常量无符号字符项2[]={A',C',D'};
常量无符号字符项3[]={'1','2'};
int _tmain(int argc,_TCHAR*argv[]
{
对于(int i=0;i
这将打印所有排列,但我担心如果数组项从item1
到item99
,则代码很难维护。打印所有排列是否有更好的解决方案?谢谢 您可以将“迭代器”存储在vector中,然后您可以执行以下操作:
bool increase(const std::vector<std::string>& v, std::vector<std::size_t>& it)
{
for (std::size_t i = 0, size = it.size(); i != size; ++i) {
const std::size_t index = size - 1 - i;
++it[index];
if (it[index] == v[index].size()) {
it[index] = 0;
} else {
return true;
}
}
return false;
}
void do_job(const std::vector<std::string>& v, std::vector<std::size_t>& it)
{
for (std::size_t i = 0, size = v.size(); i != size; ++i) {
std::cout << v[i][it[i]];
}
std::cout << std::endl;
}
void iterate(const std::vector<std::string>& v)
{
std::vector<std::size_t> it(v.size(), 0);
do {
do_job(v, it);
} while (increase(v, it));
}
bool增加(const std::vector&v,std::vector&it)
{
对于(std::size_t i=0,size=it.size();i!=size;++i){
常数std::size\u t index=size-1-i;
++它[索引];
if(it[index]==v[index].size()){
它[指数]=0;
}否则{
返回true;
}
}
返回false;
}
void do_job(const std::vector&v,std::vector&it)
{
对于(std::size_t i=0,size=v.size();i!=size;++i){
std::cout实现这一点的好方法是将问题视为整数基转换问题。因此,组合的总数是所有数组大小的乘积。输出字符串n足以确定应在字符串中打印的数组索引。
既然你已经把它标记为C++问题,我就使用二维向量,因为这使得生活变得简单得多:
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize the vector
vector<vector<char>> v( 3 );
v[0].push_back( 'b' );
v[1].push_back( 'A' );
v[1].push_back( 'C' );
v[1].push_back( 'D' );
v[2].push_back( '1' );
v[2].push_back( '2' );
// This is a convenience vector of sizes of each 1-D vector
vector<size_t> sizes( v.size() );
// Get the total number of combinations and individual vector
// sizes
size_t total = 1;
for( size_t i = 0; i < v.size(); ++i )
{
sizes[i] = v[i].size();
total *= sizes[i];
}
size_t done = 0;
// Loop till all the combinations are printed
while( done != total )
{
// Remainder, which is the index of the element
// in the 1-D vector that is to be printed
size_t r = 0;
// Quotient to be used for the next remainder
size_t q = done;
// Combination to be printed
string s = "";
// Loop over the 1-D vectors, picking the correct
// character from each
for( size_t i = 0; i < v.size(); ++i )
{
r = q % sizes[v.size() - 1 - i];
q = static_cast<size_t>( floor( q/sizes[v.size() - 1 - i] ) );
s = v[v.size() - 1 - i][r] + s;
}
cout<<s<<"\n";
done++;
}
return 0;
}
int-tmain(int-argc,_-TCHAR*argv[]
{
//初始化向量
向量v(3);
v[0]。推回('b');
v[1]。推回('A');
v[1]。推回('C');
v[1]。推回('D');
v[2]。推回('1');
v[2]。推回('2');
//这是每个一维向量大小的方便向量
向量大小(v.size());
//获取组合和单个向量的总数
//尺寸
总尺寸=1;
对于(大小i=0;i 虽然说不上更好,但您正在寻找的可能是一个向量向量,通过递归函数进行处理。您可以使用#define array_SIZE(a)(sizeof(a)/sizeof(a[0]))减少数组长度计算。
@Hamed我不确定您所说的“一对循环”是什么意思。我理解“一对循环”=“很少的循环”.OPs'item1'到'item99'似乎是在讨论100个循环要处理的100个项目,这在更改为向量向量时仍然适用。我可能误解了你的意思。请随意详细说明。@Hamed,他需要显示所有组合,对于n=100,它是1000000个组合,对于三个lop语句的三个数组sif如果要对每个阵列和100个不同的阵列执行此操作,请准备等待。如果平均循环持续时间为1ns,则所花费的总时间约为400亿年。大约是宇宙存在时间的4倍…\u tmain()<代码>是OS特定的。它不是标准C++。不重要,它不影响逻辑。我只是使用了OP的设置。@ TuRoland:我在Linux上尝试过,它确实工作。在什么时候它崩溃了?@图罗兰:抱歉。代码有一个小的差别。我复制粘贴旧版本。现在编辑了答案。