当我在系统上运行该代码时,该代码成功执行,但是当我在codechef上提交代码时,它会遇到运行时错误(Runtime Error(SIGABRT))。
这是问题的链接http://www.codechef.com/problems/LELUCKYN
这是什么错误,我该如何解决?
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main() {
int t;
cin >> t;
for ( auto i = 1; i <= t; i++ ) {
int n;
cin >> n;
vector<int> vec(n);
for ( auto in = 0; in < n; in++ ) {
int input;
cin >> input;
vec[in] = input;
}
int count = 0;
for (int x = 0; x < vec.size(); x++ ) {
for ( int y = x; y < vec.size(); y++ ) {
string num="";
for (int z = x; z <= y; z++ ) {
num.append(to_string(vec[z]));
}
int number = stoi(num);
int c4 = 0, c7 = 0;
while ( number != 0 ) {
int d = number % 10;
if ( d == 4) c4++;
else if (d ==7 ) c7++;
number/= 10;
}
int po = (int)pow( c4, c7 );
if (c4 != 2 && c7 != 2 && po <= ( ( y - x ) + 1 )) count++;
}
}
cout << count << endl;
}
}
如果我正确地阅读了您的代码,则是将所有数字连接到一个字符串中,然后尝试将其转换为一个int
数字以便对数字进行计数。
该字符串将不会始终转换为该字符串,int
因为它太大了,stoi
在这种情况下会引发异常。
你可以只算得到的字符串中的数字(数'4'
S和'7'
s,而不是4
S和7
S),或者你可以在每个数算数字,并把它们加起来,不通过字符串转换去。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句