下面的代码是用 1 和 0 填充一个矩阵来解决运筹学中的一个问题——我不会详细说明。我遇到的问题是有 6 个嵌套循环,即使代码编译完美,最后的打印语句也不会执行(尽管我没有收到任何错误消息)。谁能告诉我为什么会这样,如果 6 个嵌套循环太多而无法以这种方式处理,是否还有其他方法可以实现。提前致谢。
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<vector<int> > A(480, std::vector<int>(100000));
int index = 1;
int N;
int N2;
int N3;
int N4;
int N5;
vector<int> indices1;
vector<int> indices2;
vector<int> indices3;
vector<int> indices4;
vector<int> indices5;
for (int s1 = 0; s1 <= 480 - 24 + 1; s1++)
{
if (s1 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s2 = s1 + 24 + 24 - 1; s2 <= 480 - 24 + 1; s2++)
{
if (s2 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s3 = s2 + 24 + 24 - 1; s3 <= 480 - 24 + 1; s3++)
{
if (s3 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s4 = s3 + 24 + 24 - 1; s4 <= 480 - 24 + 1; s4++)
{
if (s4 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s5 = s4 + 24 + 24 - 1; s5 <= 480 - 24 + 1; s5++)
{
// generate print5
for (int pos = 0; pos <= 10; pos++)
{
indices1.push_back(s1 + pos);
indices1.push_back(s1 + 13 + pos);
indices1.push_back(s2 + pos);
indices1.push_back(s2 + 13 + pos);
indices1.push_back(s3 + pos);
indices1.push_back(s3 + 13 + pos);
indices1.push_back(s4 + pos);
indices1.push_back(s4 + 13 + pos);
indices1.push_back(s5 + pos);
indices1.push_back(s5 + 13 + pos);
}
std::sort(indices1.begin(), indices1.end());
// now loop over print5, N is number of elements in indices1
N = 11 * 10;
for (int ind1 = 0; ind1 < N; ind1++)
{
A [ indices1[ind1] ][index] = 1;
}
index = index + 1;
}
}
//else statement
//generate print4
for (int pos = 0; pos <= 10; pos++)
{
indices2.push_back(s1 + pos);
indices2.push_back(s1 + 13 + pos);
indices2.push_back(s2 + pos);
indices2.push_back(s2 + 13 + pos);
indices2.push_back(s3 + pos);
indices2.push_back(s3 + 13 + pos);
indices2.push_back(s4 + pos);
indices2.push_back(s4 + 13 + pos);
}
std::sort(indices2.begin(), indices2.end());
//now loop over print4
N2 = 11 * 8;
for (int ind2 = 0; ind2 < N2; ind2++)
{
A [ indices2[ind2] ][index] = 1;
}
index = index + 1;
}
}
//else statement
// generate print3
for (int pos = 0; pos <= 10; pos++)
{
indices3.push_back(s1 + pos);
indices3.push_back(s1 + 13 + pos);
indices3.push_back(s2 + pos);
indices3.push_back(s2 + 13 + pos);
indices3.push_back(s3 + pos);
indices3.push_back(s3 + 13 + pos);
}
std::sort(indices3.begin(), indices3.end());
//now loop over print3
N3 = 11 * 6;
for (int ind3 = 0; ind3 < N3; ind3++)
{
A [ indices3[ind3] ][index] = 1;
}
index = index + 1;
}
}
//else statement
//generate print2
for (int pos = 0; pos <= 10; pos++)
{
indices4.push_back(s1 + pos);
indices4.push_back(s1 + 13 + pos);
indices4.push_back(s2 + pos);
indices4.push_back(s2 + 13 + pos);
}
std::sort(indices4.begin(), indices4.end());
//now loop over print2
N4 = 11 * 4;
for (int ind4 = 0; ind4 < N4; ind4++)
{
A [ indices4[ind4] ][index] = 1;
}
index = index + 1;
}
}
//last else statement
//generate print1
for (int pos = 0; pos <= 10; pos++)
{
indices5.push_back(s1 + pos);
indices5.push_back(s1 + 13 + pos);
}
std::sort(indices5.begin(), indices5.end());
//now loop over print1
N5 = 11 * 2;
for (int ind5 = 0; ind5 < N5; ind5++)
{
A [ indices5[ind5] ][index] = 1;
}
index = index + 1;
}
// now print elements - I only print till element 50 here, since I dont actually know how many
// columns I need, therefore I initialized that to a big number (since I will need a lot)
for (int i = 0; i < 480; i++)
{
for (int j = 0; j < 50; j++)
{
cout << A[i][j];
}
}
return 0;
}
最佳答案
如果你放一个:
if (s5 % 100 == 0)
cout << s1 << ' ' << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5
<< '\r' << std::flush;
就在下面
for (int s5 = s4 + 24 + 24 - 1; s5 <= 480 - 24 + 1; s5++)
{
您将了解算法的速度(并且您会立即看到它“永不”结束)。
我没有深入研究你的问题,所以我不会尝试其他方法。保持你的算法,有一些事情你必须解决:
您应该检查(并在某处重置)
index
变量:它变得越来越大,您将遇到段错误 (A[indices1[ind1]][索引]
).indices1
, ...indices5
vector 将迅速变得非常大,std::sort
函数将采用很多时间。在我看来,您并没有使用 vector 中的所有元素,例如:
std::sort(indices1.begin(), indices1.end()); N = 11 * 10; for (int ind1 = 0; ind1 < N; ++ind1) A [indices1[ind1]][index] = 1;
如果您只需要前
N
个元素,您可以使用std::partial_sort
:N = 11 * 10; std::partial_sort(indices1.begin(), indices1.begin() + N, indices1.end());
这会更快(但可能还不够快)。
关于c++ - 用 1 和 0 填充一个大矩阵 - 六个嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23502234/