簡體   English   中英

如何在google code jam中用c++代碼輸入測試用例

[英]How to input test case in c++ code in google code jam

我嘗試在 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;
}

您可以使用ifstreamofstream如下:

#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;
}

您可以將finfout視為cin ,因此不是從控制台讀取輸入, in.txt從文件in.txt讀取輸入。 不是使用cout寫入控制台, output.out使用fout寫入output.out

測試用例的一般格式是:

int t;   

cin >> t; 

while (t--)        (
{
    //code here
}

所以我們為我們的測試用例創建一個變量並要求用戶為它輸入一個值......然后我們創建一個 while 循環來檢查 t 的值是否 > 0 並每次將值遞減 1)希望我有所幫助! :D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM