【问题标题】:Execution Error in Greedy Gift Givers USACO Training program [closed]贪婪的送礼者 USACO 培训计划中的执行错误 [关闭]
【发布时间】:2014-12-24 19:05:30
【问题描述】:

今年我正在上高中的计算机编码课程,我正在尝试做贪婪的送礼者(http://cerberus.delosent.com:791/usacoprob2?a=2uhftHpQUHa&S=gift1)。我把它上交并得到一个执行错误,上面写着:

"执行错误:您的程序没有产生答案 这被认为是正确的。程序在 0.005 秒处停止; 它使用了 3496 KB 的内存。你的答案长度是 119;正确的 长度为 121。在第 21 个字符处,您的答案为“1”,而 正确答案是“5”。”

我差不多完成了,它给出了十分之四的答案。我不知道如何解决它。我的一位朋友告诉我检查我的变量,我做到了,但据我所知,它们都是正确的。

新代码如下:

/*
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;
}

【问题讨论】:

  • 在所有循环中,您应该使用ARRAY_SIZE,而不是groupSize
  • 问题定义应该包含在问题中,至少是基础知识。如果问题的定义太长,则提供问题定义的链接(问题中至少包含基本描述。

标签: c++ execution


【解决方案1】:

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

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?
            }
        }
    }

可能还有更多,到此为止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多