以下代码用于为运筹学中的问题用一和零填充矩阵-我将为您省去细节。我的问题是有6个嵌套循环,即使代码可以完美地编译,最后的print语句也不会执行(尽管我没有收到任何错误消息)。谁能告诉我为什么会这样,并且,如果这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]][index]
)。indices1
,... indices5
向量将迅速变大,并且std::sort
函数将花费大量时间。在我看来,您没有使用向量中的所有元素,例如:
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());
这样会更快(但可能不够快)。