gpt4 book ai didi

c++ - spoj 上的抗印迹系统

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:12 25 4
gpt4 key购买 nike

我遇到了一个问题 Anti Blot

我在所有测试用例上执行了我的程序,我得到了正确的答案。

但我仍然在 spoj 上得到“错误答案”

我尝试过的:

将流分解成字符串并将它们存储到 vector 中,所以我们可以这样说我们的 vector 将有 5 个字符串 (numstring,+,numstring,=,numstring) 然后当我们在字符串中找到 m 时我们将检查每个字符串,我们知道该位置的字符串是变量,需要借助其他数字字符串进行计算

而且我们还确定带有数字的字符串只能位于 vector 中的 0,2,4 索引处,其他两个索引将具有 + 和 =

测试用例:

输入:

3

23 + 47 = machula

3247 + 5machula2 = 3749

machula13 + 75425 = 77038

输出:

23 + 47 = 70

3247 + 502 = 3749

1613 + 75425 = 77038

#include<iostream>
#include<sstream>
#include<vector>
#include<string>
using namespace std;
int main() {
int t;
(cin >> t).get();
while (t--) {
string str;

getline(cin, str);
stringstream split(str);
string each;
vector <string> tokens;
while (getline(split, each, ' ')) {
///scan each part of stream;
tokens.push_back(each);

}
int ind = -1;
for (int i = 0; i < tokens.size(); i++) {
string temp;
temp = tokens[i];
for (int j = 0; j < temp.length(); j++) {
if (temp[j] == 'm') {
ind = i;
break;
}

}
if (ind != -1)
break;
}
int i1, i2;
string str1;
if (ind == 0) {
i1 = stoi(tokens[2]);
i2 = stoi(tokens[4]);
int result = i2 - i1;
cout << result << " + " << i1 << " = " << i2 << "\n";
//break;
}
else if (ind == 2) {
i1 = stoi(tokens[0]);
i2 = stoi(tokens[4]);
int result;
result = i2 - i1;
cout << i1 << " + " << result << " = " << i2 << "\n";
//break;
}
else if (ind == 4) {
i1 = stoi(tokens[0]);
i2 = stoi(tokens[2]);
int result = i1 + i2;
cout << i1 << " + " << i2 << " = " << result << "\n";
//break;
}
tokens.clear();
str.erase();
}


}

最佳答案

我知道这很烦人,你的解决方案没有逻辑错误,但你应该更仔细地阅读问题描述。它说:

Each test case is preceded by a blank line.

所以输入是:

3

23 + 47 = machula

3247 + 5machula2 = 3749

machula13 + 75425 = 77038

注意行与行之间的空行。只需将您的代码修复为:

// Blablabla before getline
getline(cin, str);
getline(cin, str);
stringstream split(str);
// The rest of your code

这应该可以解决问题。

关于c++ - spoj 上的抗印迹系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54265421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com