如何在C++代码中输入测试用例在谷歌代码堵塞
问题描述:
我试着解决谷歌代码堵塞实践页面Minimum Scalar Product中的问题,我用C++编写的程序,我阅读常见问题页面,我们必须测试我们的程序与.in测试文件放在练习页面上下载,但我不知道如何和我使用UBUNTU 12.04 LTS &请我参加第一次比赛..所以任何帮助将不胜感激..谢谢在提前如何在C++代码中输入测试用例在谷歌代码堵塞
我试图
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int numCase;
cin >> numCase;
int i, j, n;
long long c;
for (i = 0; i < numCase; i++)
{
cin >> n;
vector<long long> array1, array2;
for (j = 0; j < n; j++)
{
cin >> c;
array1.push_back(c);
}
for (j = 0; j < n; j++)
{
cin >> c;
array2.push_back(c);
}
sort(array1.begin(), array1.end());
sort(array2.begin(), array2.end(), greater<long long>());
long long ans = 0;
for (j = 0; j < n; j++)
ans += (array1[j] * array2[j]);
cout << "Case #" << (i+1) << ": " << ans << endl;
}
return 0;
}
答
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("input.in");
ofstream fout("output.out");
//-- check if the files were opened successfully
if (!fin.is_open()) cout << "input.in was not open successfully" << endl;
if (!fout.is_open()) cout << "output.out was not open successfully" << endl;
int numCase;
fin >> numCase;
int i, j, n;
long long c;
for (i = 0; i < numCase; i++)
{
fin >> n;
vector<long long> array1, array2;
for (j = 0; j < n; j++)
{
fin >> c;
array1.push_back(c);
}
for (j = 0; j < n; j++)
{
fin >> c;
array2.push_back(c);
}
sort(array1.begin(), array1.end());
sort(array2.begin(), array2.end(), greater<long long>());
long long ans = 0;
for (j = 0; j < n; j++)
ans += (array1[j] * array2[j]);
fout << "Case #" << (i + 1) << ": " << ans << endl;
}
fin.close();
fout.close();
return 0;
}
你可以把fin
和fout
为cin
,所以不是从控制台读取输入,你从文件中读取in.txt
输入。使用cout
来写信给控制台,而不是使用fout
写信给output.out
。
请您能更好地解释...谢谢 – king 2015-04-03 14:30:32
检查更新,并注意您将通过上传output.out文件提交您的答案。 – MrGreen 2015-04-03 14:35:33
因此,这会从in.txt 1中读取输入1 ...谢谢。你能帮我把它放在我给你的代码上面吗?只是为了更好的理解 – king 2015-04-03 14:36:37