我试图解决谷歌代码堵塞练习页最小标量产品中的一个问题,我有用c++编写的程序,我在常见问题解答页面上读到,我们必须用练习页上的.in测试文件来测试我们的程序,以便下载,但我不知道如何使用UBUNTU 12.04 LTS&请允许我第一次参加比赛。。因此,如有任何帮助,我们将不胜感激。。提前感谢
我试过
#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;
}
您可以使用ifstream和ofstream如下所示:
#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
。
测试用例的通用格式为:
int t;
cin >> t;
while (t--) (
{
//code here
}
因此,我们为测试用例创建一个变量,并要求用户为其输入一个值……然后我们创建一个while循环,检查t的值是否>0,每次将值递减1)希望我能帮上忙!:D