首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >贪心赠与者USACO训练计划中的执行错误

贪心赠与者USACO训练计划中的执行错误
EN

Stack Overflow用户
提问于 2014-12-24 19:05:30
回答 1查看 701关注 0票数 0

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

执行错误:您的程序没有生成被判定为正确的答案。程序以0.005秒的速度停止,它使用了3496 KB的内存。您的回答长度为119 KB;正确的长度为121。在字符21处,您的答案是'1‘,而正确的答案是’5‘。

我快完成了,它给出了四个正确的答案。我不知道怎么修好它。我的一个朋友告诉我检查我的变量,我做了,但据我所知,所有这些都是正确的。

新的守则如下:

代码语言:javascript
复制
/*
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-24 20:56:47

您的代码不遵循输入格式的规范。

代码语言:javascript
复制
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?
            }
        }
    }

可能还有更多的,停在这里。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27641244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档