//我正试图从HackerEarth
解决这个问题:Binary Queries。
虽然最初,这个问题对我来说很容易,但实际提交我的代码以便在所有测试用例上运行时,我的代码就是抛出SIGABRT error
。
检查错误后,我发现错误类型为out of range
。我无法弄清楚如何解决这个问题:问题:https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/range-query-2/
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
int main()
{
long N,Q,L,R,X;
int ch=0,buf;
unsigned long long intrim;
string str,cstr;
scanf("%ld %ld",&N,&Q);
cin.ignore();
getline(cin,str);
str.erase((remove_if(str.begin(),str.end(),(int(*)(int))isspace)),str.end()); // here its a key point
//cout<<str;
for(int i=0;i<Q;i++)
{
scanf("%d",&ch);
if(ch==1) // alter the X bit
{
scanf("%ld",&X);
if(str[X-1]==0){
str[X-1]=1;
}
else {
str[X-1]=0;
}
}
else if(ch==0)
{
scanf("%ld %ld",&L,&R);
cstr.append((str.begin()+L-1),str.begin()+R);
intrim=std::stoull(cstr,nullptr,2);
if(intrim%2==0){
cout<<"EVEN"<<endl;
}
else{
cout<<"ODD"<<endl;
}
cstr.clear();
}
}
}
stoi返回一个整数
stol返回一个长整数
stoll返回一个很长的int数
因此,当您将字符串转换为数字时,请考虑可能的范围并相应地使用。