簡體   English   中英

在 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;
}

由許多子索引組成的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM