如何在谷歌代码堵塞中输入c++代码中的测试用例
How to input test case in c++ code in google code jam
我试图解决谷歌代码堵塞练习页最小标量产品中的一个问题,我有用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
相关文章:
- 为什么控制台要求输入,即使代码中没有输入
- 在C++中使用 ASCII 代码将输入从小写转换为大写
- 输入较高值时的分段代码
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- C++逗号分隔的输入数组代码过早退出
- 创建一个数组,以允许用户输入存储并稍后在代码中使用
- 使用用户输入打印 6X10 矩阵的代码 - C++
- C++基于输入参数的动态代码生成
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 代码在执行之前终止,并且没有从用户那里获取输入
- 我觉得我放入结构中的输入代码可以压缩,关于如何在保持代码简短的同时保持数据个性化的任何建议?
- 有人可以解释我这个代码吗?它给出与我们输入的数字相反
- 代码占用了我输入的一半
- 为什么给定的代码不适用于所有测试用例?为什么当输入为 7786 时它不起作用?
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 有没有把用户输入当作代码来阅读的想法
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 使用boost::propertytree为我的代码输入文件格式
- 为什么在 stdin 上输入时"n"而不是从代码输入时显示不同?