Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 内存分配问题_C++_Runtime Error - Fatal编程技术网

C++ 内存分配问题

C++ 内存分配问题,c++,runtime-error,C++,Runtime Error,是我的密码。代码在我的code::blocks上运行良好,但在spoj站点和ideone.com上运行不正常。我遇到运行时错误。我猜spoj服务器无法分配所需的内存量。请给出一些建议 (我的代码)您的代码正在声明一个空字符串s,然后分配给它的元素 ... string s,res;int c=0; int sum,carry=0; for(int i=m-1;i>=0;i--) { sum=(a[i]-'0')*2+carry; s[c]=sum%10+'0';

是我的密码。代码在我的code::blocks上运行良好,但在spoj站点和ideone.com上运行不正常。我遇到运行时错误。我猜spoj服务器无法分配所需的内存量。请给出一些建议


(我的代码)

您的代码正在声明一个空字符串
s
,然后分配给它的元素

...
string s,res;int c=0;
int sum,carry=0;
for(int i=m-1;i>=0;i--)
{
    sum=(a[i]-'0')*2+carry;
    s[c]=sum%10+'0';         // This is undefined behavior, s is empty
    carry=sum/10;
    c++;
}
...

这只是@6502答案的扩展

它看起来很适合你想要的东西

ostringstream oss;
string s,res;
int c=0;
int sum,carry=0;

for(int i=m-1;i>=0;i--)
{
    sum=(a[i]-'0')*2+carry;
    oss << (sum%10) << '0'; //Were you trying to concatenate a '0' as well?
    carry=sum/10;
}

s = oss.str();
ostringstream操作系统;
字符串s,res;
int c=0;
整数和,进位=0;
对于(int i=m-1;i>=0;i--)
{
和=(a[i]-'0')*2+进位;

oss这是对你的问题的可能答案,而不是你的问题

我猜这个问题有算法的味道,它的目的是找到 时间复杂度最低的解决方案(可能是线性时间解决方案)。 对与最佳时间复杂度相关的问题进行一些预评价是很有帮助的

因此,我计算了几个时间步产生的模式(如下所示):

产生上述模式的代码如下所示:

#include<iostream>
using namespace std;
main()
{
string s,t=""; 
s="paste pattern produced in a time-step here";
int i,l,n=0;
l=s.length();
cout <<"s.length - "<<l<<endl;
    for(i=0;i<l;i++)
    {
        if(s[i]=='0')
          {t+="10";}
        else
          {t+="01";}
    }
l*=2;
        for(i=0;i<l-1;i++)
        {
            if(t[i]=='0' && t[i+1]=='0')
            {
            n+=1;
            }
        }
cout <<"t - "<<t<<endl;
cout <<"no. of consecutive zero pairs - "<<n<<endl;
}
#包括
使用名称空间std;
main()
{
字符串s,t=“”;
s=“在此处粘贴在时间步长中生成的图案”;
int i,l,n=0;
l=s.长度();

请在问题中发布你的代码。如果网站
ideone
消失了,那么提供的答案将毫无意义,因此你浪费了人们试图帮助你的时间,也不会为网站的未来用户带来任何好处。否决票是怎么回事?我只是提供了一个可能的解决方案,来解释OP在文章中的意图这段代码。感谢您不辞辛劳地将解释写得如此优美:)。我将采用这种方法,而不是我以前的方法。谢谢:)
#include<iostream>
using namespace std;
main()
{
string s,t=""; 
s="paste pattern produced in a time-step here";
int i,l,n=0;
l=s.length();
cout <<"s.length - "<<l<<endl;
    for(i=0;i<l;i++)
    {
        if(s[i]=='0')
          {t+="10";}
        else
          {t+="01";}
    }
l*=2;
        for(i=0;i<l-1;i++)
        {
            if(t[i]=='0' && t[i+1]=='0')
            {
            n+=1;
            }
        }
cout <<"t - "<<t<<endl;
cout <<"no. of consecutive zero pairs - "<<n<<endl;
}