【发布时间】:2021-02-22 15:07:38
【问题描述】:
考虑一个 cpp 函数solve()。数字字符串a(示例测试字符串)作为参数传递给函数solve()。
我想让stdin 读取字符串a 中的数字。
这实际上是一个压力测试。所以在这个压力测试中,这个函数solve()被提供了一个字符串到return,这个结果将针对另一个函数solveFast()获得的另一个结果进行测试。
注意:- 函数
solve()中的算法已经提供给我们。我希望针对我自己的算法(solveFast())对该算法进行压力测试。保证solve()函数中的算法针对其测试输入提供正确的输出
#include <bits/stdc++.h>
using namespace std;
int solve(string s) {
// below given: an algorithm that
//uses stdin to take input sample test case (i.e.,not string s)
int n; //number of integers in string s
//ignore the purpose of the code below.
//Just observe that it is taking the inputs as cin (not from string s.
cin >> n;
int first, second, large;
for (int i = 0; i < n - 1; i++) {
if (i == 0) {
cin >> first >> second;
large = (second > first) ? second : first;
if (second < first) first = second;
} else {
cin >> second; // new num
if (second > large) {
first = large;
large = second;
}
else if(second > first){
first = second;
}
}
}
int result = large * first;
return result;
}
int solveFast(string s) {
/*
* my solution here
*/
return result;
}
int32_t main() {
//stress-testing code starts here
while (true) {
string a;
/*
* generating a sample test case and storing it in a string 'a'
*/
int res1 = solve(a);
int res2 = solveFast(a);
if(res1!=res2){
cout << "Wrong answer: " << res1 << " " << res2 << endl;
break;
}
else{
cout << "OK\n";
}
}
//stress-testig code ends
/*
for (int i = 1; i <= t; ++i) {
int n;
cin >> n;
vector<int> numbers(n);
for (int i = 0; i < n; i++) {
cin >> numbers[i];
}
auto result = solve(numbers);
cout << result << endl;
}
*/
return 0;
}
【问题讨论】:
-
std::stringstream有>>运算符,就像std::cin有
标签: c++ stdin stress-testing