在 c++ 中实现嵌套循环的更短方法吗?
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;
}
一个由许多子索引组成的超整数类,以及一个使用超整数类类型的开始和结束的索引类,可能用于编写具有唯一 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_;
};
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 初始化具有非默认构造函数的std::数组项的更好方法