试图打印出等于和值的特殊组合类型

时间:2017-12-18 18:12:32

标签: c++

您好我需要一些帮助我从互联网上找到以下代码我想在此代码中添加一些更改。

此代码打印出所有可能的时隙组合总值必须=和

如何以最大容量值控制每个插槽 例如,如果slot = 3,sum = 8则我想添加一个容量(数组或向量)= [4] [2] [3]所有序列必须在此范围内。所以我也可以跳过这样的等于sum [0] [4] [4]的序列。我需要帮助抱歉我正在学习英语..

#include <iostream> 
#include <vector> 
#include <algorithm> 

template <typename T, typename ForwardIterator> 
bool increment(ForwardIterator first, ForwardIterator last, T maximum) { 
  for (auto it = first; it != last; ++it) { 
      if (*it != maximum) { 
       std::fill(first, it, ++*it); 
      return true; 
    } 
   } 
   return false; 
 } 

int main() 
{ 
 int minimum = 1; // included 
 int slots = 3; 
 int sum = 8; 
 int internal_max = sum - slots * minimum; 
 std::vector<int> vect(slots - 1, 0); 
 do { 
   auto previous_pos = internal_max; 
    for (auto it = vect.begin(); it != vect.end(); ++it) { 
     auto val = previous_pos - *it + minimum; 
     previous_pos = *it; 
     std::cout << val << " "; 
   } 
   std::cout << previous_pos + minimum << std::endl; 
 } while (increment(vect.begin(), vect.end(), internal_max)); 
} 

1 个答案:

答案 0 :(得分:-1)

// next_permutation example
 #include <iostream>     // std::cout
 #include <algorithm>    // std::next_permutation, std::sort
 #include <vector>

  using namespace std;


  template <typename T, typename ForwardIterator>

 bool increment(ForwardIterator first, ForwardIterator Last, T  Maximum) {

for (auto it = first; it != Last; ++it) {

    if (*it != Maximum) {

        fill(first, it, ++*it);

        return true;
    }

}

return false;

}

 int main() {

int minimum = 0;
int slots = 3;
int sum = 124;
int internal_Max = sum - slots * minimum;
vector<int> vect(slots - 1, 0);
int count = 0, CT = 0, L1 = 127, L2 = 1, L3 = 127, R1 = 0, R2 = 0, R3 = 0;
bool F1 = false, F2 = false;

do {

    auto previous_po = internal_Max;
    for (auto it = vect.begin(); it != vect.end(); ++it) {

        auto val = previous_po - *it + minimum;
        previous_po = *it;
        if (CT == 0) {

            if (val <= L1) {
                F1 = true;
                R1 = val;
            }

        }
        else if (CT == 1) {

            if (val <= L2) {
                F2 = true;

                R2 = val;
            }

        }
        CT++;
    }

    CT = 0;
    if (F1 && F2 && (previous_po + minimum) <= L3) {

        cout << R1 << " " << R2 << " " << previous_po + minimum << " " << "Count = " << count++ << endl;
    }

    F1 = false;
    F2 = false;

} while (increment(vect.begin(), vect.end(), internal_Max));







return 0;
}

这是我到目前为止所知道的,这不是一种正确的方法,但这将做我想要的。我所做的是从打印中跳过一些组合我甚至不想要那个组合计算plz给我一个正确的方法