c++ - spoj 上的防污系统
问题描述
我遇到了一个问题Anti Blot
我在所有测试用例上执行了我的程序,我得到了正确的答案。
但我仍然在 spoj 上得到“错误答案”
我试过的:
将流分解成字符串,将它们存储到向量中,所以我们可以说我们的向量将有 5 个字符串(numstring,+,numstring,=,numstring),然后我们将检查每个字符串,只要我们m
在字符串中找到,我们就会知道此位置的字符串是变量,需要借助其他数字字符串来计算
而且我们也确定带有数字的字符串只能位于0,2,4
其他两个索引将具有的向量中的索引处+ and =
测试用例:
输入:
3
23 + 47 = 马丘拉
3247 + 5machula2 = 3749
马丘拉13 + 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();
}
}
解决方案
我知道这很烦人,您的解决方案没有逻辑错误,但您应该更仔细地阅读问题描述。它说:
每个测试用例前面都有一个空行。
所以输入是:
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
这应该可以解决问题。
推荐阅读
- algorithm - 测试一个列表是否是另一个列表的子列表而不测试元素之间的相等性
- vba - PowerPoint VBA - 如何识别思考单元对象?
- asp.net-mvc - Azure 移动应用服务中的异常处理
- vb.net - 使用 RegisterHotKey 时如何检测按下了哪个热键?
- r - 匹配嵌套列表中的所有元素(无论位置如何)并返回匹配索引
- android - 有没有办法以编程方式隐藏/删除android权限对话框?
- html - 如何设置子 div 元素从其位置到页面底部的高度?
- javascript - 使用高级自定义字段将 Woocommerce 插件价格添加到帖子
- python - 在 Python 3 中将输入()转换为字节?
- java - startActivityForResult:finish() 返回错误的活动