[英]Any shorter way of implementing nested loops in c++?
例如,如果我们希望程序执行以下操作:
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;
}
由许多子索引组成的 SuperInteger class 和使用 SuperInteger Class 类型的开头和结尾的 Index classn,可能用于编写具有唯一 for 循环的 main(现在应该使用具有 1 个子索引的 SuperIntegers 来确保最后一个元素存在)
也使用了这个数学结果(简单的演示):
max (max (a, b), (max (c,d)) == max (a, b, c, d)
使用这个索引和 Super Integer,计算的总和是 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
}
和超整数 class:
#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];
};
最后,索引 class,采用预增量方法
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_;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.