c++ - Greedy Gift Givers USACO 培训计划中的执行错误

标签 c++ execution

<分区>

我今年在高中上计算机编码课,我正在尝试做 Greedy Gift Givers( http://cerberus.delosent.com:791/usacoprob2?a=2uhftHpQUHa&S=gift1 )。我将其上交并收到执行错误:

"Execution error: Your program did not produce an answer that was judged as correct. The program stopped at 0.005 seconds; it used 3496 KB of memory. Your answer length was 119; the correct length is 121. At character number 21, your answer says '1' while the correct answer says '5'."

我快完成了,它给出了十分之四的正确答案。我不知道如何解决它。我的一位 friend 告诉我检查我的变量,我照做了,但据我所知,所有这些都是正确的。

新代码如下:

/*
ID              :   aknorth1
PROB            :   gift1
LANG            :   C++
*/

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    const int ARRAY_SIZE=10;

    int groupSize, numReceivers, giveAway;
    int bankAcct[ARRAY_SIZE];
    string giver, receivers;
    string groupPeople[ARRAY_SIZE];

    ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");

    fin >> groupSize;

    for (int j=0; j<groupSize; j++)
    {
        fin >> groupPeople[j];      
        bankAcct[j]=0;
    }

    for(int x=0; x<groupSize; x++)
    {
        fin >> giver;
        fin >> giveAway;
        fin >> numReceivers;
        for (int j=0; j<numReceivers; j++)
        {
            if (giver == groupPeople[j])
            {
                bankAcct[j] -= giveAway;
                if (numReceivers != 0)
                {
                    bankAcct[j] += (giveAway % numReceivers);
                }
            }
        }   
        for(int j=0; j<numReceivers; j++)
        {
            fin >> receivers;

        for (int q=0; q<groupSize; q++)
            if (groupPeople[q] == receivers)
            {
                if (numReceivers != 0)
                {
                    bankAcct[q] += (giveAway / numReceivers);
                }
            }
        }
    }
    for (int j=0; j<groupSize; j++)
    {
        fout << groupPeople[j]<< " " << bankAcct[j] << endl;
    }                   
    return 0;
}

最佳答案

您的代码不符合输入格式规范。

fin >> giver; // dave
fin >> giveAway; // gives away 200
fin >> numReceivers; // to 3 receivers

for (int j=0; j<numReceivers; j++)
{
    if (giver == groupPeople[j]) // if dave was 4th in the list then what?
        {
            bankAcct[j] -= giveAway;
            if (numReceivers != 0)
            {
                bankAcct[j] += (giveAway % numReceivers); // giver gives to himself?
            }
        }
    }

可能还有更多,就此打住。

关于c++ - Greedy Gift Givers USACO 培训计划中的执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27641244/

相关文章:

java - 这些类和子类静态 block 何时执行(对于枚举)?

c++ - 具有 exact_constructions 内核的 3D 凸包 CGAL::halfspace_intersection_3

c++ - 为什么 std::abs(9484282305798401ull) = 9484282305798400?

c++ - 使用 getline 和 vector 的 C++ 问题

c++ - std::equal 给出 "Term doesnt evaluate to a function taking 2 arguments"

Python 的执行顺序

Java - 如何执行一个进程

Windows Azure : how to measure the execution time of a code

c++ - 弗洛伊德三角变体

javascript - jQuery .prop 停止执行而没有错误