用1和0填充一个大矩阵——6个嵌套循环
Fill a large matrix with ones and zeros - six nested loops
下面的代码是为运筹学中的一个问题用1和0填充一个矩阵-我将为您省去细节。我遇到的问题是,有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
函数将花费大量时间。在我看来,你没有使用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());
这将更快(但可能不够快)。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 我需要将多个函数组合为一个函数
- 获取向量C++中第一个值和最后一个值的和
- 在多个核心中处理一个HTTP请求
- 从多个源构造一个对象,包括一个对象向量
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 在C++行尾写一个分号或多个分号是否会改变任何内容
- 为我的 c++ 类介绍制作一个三角形分类器.我有几个问题
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 将第一个n个字符复制到std :: string
- 如何得到一个两个数字都相等或这个数字更大
- 如何使函数一次使用一个 3 个不同的变量