在 c++ 中实现嵌套循环的更短方法吗?

Any shorter way of implementing nested loops in c++?

本文关键字:方法 c++ 实现 嵌套循环      更新时间:2023-10-16

例如,如果我们希望程序执行以下操作:

int sum=0;
for(int a=0; a<10; ++a)
for(int b=0; b<10; ++b)
for(int c=0; c<10; ++c)
for(int d=0; d<1-; ++d)
sum+=max(max(a,b),max(c,d));

在这里,现在很好,因为只有 3 个嵌套循环。我的问题是,如果我们有例如 20 个嵌套循环来处理,有没有办法缩短工作量?

您可以使用递归和数组来循环变量,如下所示。我只是建议不要在循环中使用10,否则您将永远等待结果:-(

#include <iostream>
using namespace std;
int loopVar[20] = {0};
int summarize(int i){
if (i == 20)
return 0;
int sum = 0;
for(loopVar[i] = 0; loopVar[i] < 2; loopVar[i]++)
sum += summarize(i+1);

return sum + max(loopVar[3], loopVar[5]);
}
int main() {
cout << summarize(0) << endl;
return 0;
}

一个由许多子索引组成的超整数类,以及一个使用超整数类类型的开始和结束的索引类,可能用于编写具有唯一 for 循环的主(现在应该使用具有 1 个子索引的超整数来确保最后一个元素存在(

这个数学结果也被使用(琐碎的演示(:

max (max (a, b), (max (c,d)) == max (a, b, c, d)

使用此索引和超级整数,计算的总和为 74667,就像嵌套循环一样,(必须在循环后执行最终max_subindex ((:

int main (int argc, char * argv []) {
//m = max (max (i, j), max (k, l) <=> m = max (i, j, k, l)
SuperInteger beg (5, 0, 0, 0, 0), end (5, 10, 10, 10, 10, 1);
Index index (beg, end);
int sum (0);
for (; !index.end (); ++index) {
sum += index.max_subindex ();
}
sum += index.max_subindex ();
std::cout << "sum == " << sum << std::endl;
//sum == 74667
}

和超整数类:

#include <stdarg.h>
#include <iostream>
#define MAXSUBINDEX 100
class SuperInteger {
friend class Index;
public :
SuperInteger (int siz, ...) : size_ (siz) {
va_list ap;
va_start(ap, siz);
int* pval ((int*) val_);
for(int i = 1; i <= siz; i++, ++pval) {
*pval = va_arg (ap, int);
}
va_end(ap);    
}
~SuperInteger () {}
SuperInteger (const SuperInteger& pn) : size_ (pn.size_) {
size_ = pn.size_;
int* p ((int*)val_);
const int* q ((const int*) pn.val_);
for (int i (0); i != size_; ++i, ++p, ++q) *p = *q;
}
SuperInteger& operator = (const SuperInteger& pn) {
size_ = pn.size_;
int* p ((int*)val_);
const int* q ((const int*) pn.val_);
for (int i (0); i != size_; ++i, ++p, ++q) *p = *q;
return *this;
}
bool operator == (const SuperInteger& pn) const {
if (size_ != pn.size_) return false;
const int* p ((const int*)val_);
const int* q ((const int*)pn.val_);
for (int i (0); i != size_; ++i, ++p, ++q) if (*p != *q) return false;
return true;
}
bool operator != (const SuperInteger& pn) const {
return !operator == (pn);
}
#ifdef _DEBUG 
void write (std::ostream& os) const {
const int* p ((const int*)val_);
for (int i (0); i != size_; ++i, ++p) {os << *p << " ";}
}
#endif
int operator [] (const int& i) const {
return val_ [i];
}
int& operator [] (const int& i) {
return val_ [i];
}
int max_subindex () const {
int m (0);
const int* p ((const int*) val_);
for (int i (0); i != size_; ++i, ++p)if (*p>m) m = *p;
return m;
}
//private :
int size_;
int val_ [MAXSUBINDEX];
};

最后是 Index 类,带有预递增方法

class Index {
public :
Index (SuperInteger beg, SuperInteger end) : beg_ (beg), val_ (beg), end_ (end) {}
Index (const Index& i) : beg_ (i.beg_), val_ (i.val_), end_ (i.end_) {}
Index& operator = (const Index& i) {
beg_ = i.beg_;
val_ = i.val_;
end_ = i.end_;
return *this;
}
bool operator == (const Index& i) const {
return (val_ == i.val_?true:false);
}
bool operator != (const Index& i) const {
return !operator == (i);
}
void operator ++ () {
bool carry (true);
int i (0); //, j (i);
while (carry) {
if (val_ [i] +1 < end_ [i]) {
++val_ [i];
carry = false;
}
else {
val_ [i] = beg_ [i];
++i;
}
}
}
bool end () const {
for ( int i (0); i != val_.size_; ++i) {
if (val_ [i] +1 != end_ [i]) return false;
}
return true;
}
int max_subindex () const {
return val_.max_subindex ();
}
#ifdef _DEBUG
void write (std::ostream& os) {
val_.write (os);
}
#endif
SuperInteger beg_;
SuperInteger val_;
SuperInteger end_;
};