C++矢量和记忆运行时错误问题
我在 Codechef 此处遇到了问题。我正在尝试使用向量进行记忆。由于我对编程还是个新手,而且对 STL 容器也不太熟悉,所以我使用了 vector
作为查找表。 (尽管,有人建议我使用 map
有助于解决问题)。
所以,我的问题是下面给出的解决方案如何遇到运行时错误。为了得到错误,我使用问题的边界值 (100000000
) 作为输入。我的 Netbeans IDE 显示的错误消息是 RUN FAILED (exit value 1, Total time: 4s)
,输入为 1000000000
。这是代码:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#define LCM 12
#define MAXSIZE 100000000
using namespace std;
/*
*
*/
vector<unsigned long> lookup(MAXSIZE,0);
int solve(int n)
{
if ( n < 12) {
return n;
}
else {
if (n < MAXSIZE) {
if (lookup[n] != 0) {
return lookup[n];
}
}
int temp = solve(n/2)+solve(n/3)+solve(n/4);
if (temp >= lookup[n] ) {
lookup[n] = temp;
}
return lookup[n];
}
}
int main(int argc, char** argv) {
int t;
cin>>t;
int n;
n = solve(t);
if ( t >= n) {
cout<<t<<endl;
}
else {
cout<<n<<endl;
}
return 0;
}
I encountered a problem here at Codechef. I am trying to use a vector for memoization. As I am still new at programming and quite unfamiliar with STL containers, I have used vector
, for the lookup table. (although, I was suggested that using map
helps to solve the problem).
So, my question is how is the solution given below running into a run time error. In order to get the error, I used the boundary value for the problem (100000000
) as the input. The error message displayed by my Netbeans IDE is RUN FAILED (exit value 1, total time: 4s)
with input as 1000000000
. Here is the code:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#define LCM 12
#define MAXSIZE 100000000
using namespace std;
/*
*
*/
vector<unsigned long> lookup(MAXSIZE,0);
int solve(int n)
{
if ( n < 12) {
return n;
}
else {
if (n < MAXSIZE) {
if (lookup[n] != 0) {
return lookup[n];
}
}
int temp = solve(n/2)+solve(n/3)+solve(n/4);
if (temp >= lookup[n] ) {
lookup[n] = temp;
}
return lookup[n];
}
}
int main(int argc, char** argv) {
int t;
cin>>t;
int n;
n = solve(t);
if ( t >= n) {
cout<<t<<endl;
}
else {
cout<<n<<endl;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑这是否是一个内存问题,因为他已经说过该程序实际上运行并且他输入了 100000000。
我注意到的一件事是,在 if 条件下,即使 n == MAXSIZE (在此确切的条件)。由于 C++ 使用 0 索引向量,因此超出向量末尾的值将是 1。
我无法猜测该算法正在做什么,但我认为第一个“if”的右大括号 } 应该较低,并且您可能会在此边界条件上返回错误。
I doubt if this is a memory issue because he already said that the program actually runs and he inputs 100000000.
One things that I noticed, in the if condition you're doing a lookup[n] even if n == MAXSIZE (in this exact condition). Since C++ is uses 0-indexed vectors, then this would be 1 beyond the end of the vector.
I can't guess what the algorithm is doing but I think the closing brace } of the first "if" should be lower down and you could return an error on this boundary condition.
您要么没有足够的内存,要么没有足够的连续地址空间来存储 100,000,000 个
unsigned long
。You either don't have enough memory or don't have enough contiguous address space to store 100,000,000
unsigned long
s.这主要是内存问题。对于向量,您需要连续的内存分配[以便它能够跟上其恒定时间查找的承诺]。在你的例子中,使用 8 字节双精度,你基本上是在请求你的机器在一个块中为你提供大约 762 mb 的内存。
我不知道你正在解决哪个问题,但看起来你正在解决 Bytelandian 硬币。为此,最好使用映射,因为:
因此,在您的情况下最好使用的是 std::map。在您的情况下,实际上只需将
std::vector
替换为std::map
就可以作为map
还具有[]
操作员访问权限 [在大多数情况下,它应该]。This mostly is a memory issue. For a vector, you need contiguous memory allocation [so that it can keep up with its promise of constant time lookup]. In your case, with an 8 byte double, you are basically requesting your machine to give you around 762 mb of memory, in a single block.
I don't know which problem you're solving, but it looks like you're solving Bytelandian coins. For this, it is much better to use a map, because:
So, the best thing to use in your situation is an std::map. In your case, actually just replacing
std::vector<unsigned long>
bystd::map<int, unsigned long>
would work asmap
also has[]
operator access [for the most part, it should].