| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 694 人关注过本帖
标题:USACO上的一道题,提交后出现下面的错误信息,求解释+纠错,感激不尽!
只看楼主 加入收藏
jiaxingzheng
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-11-3
收藏
 问题点数:0 回复次数:1 
USACO上的一道题,提交后出现下面的错误信息,求解释+纠错,感激不尽!
程序题目:
Greedy Gift Givers

A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.

The rules for gift-giving are potentially different than you might expect. Each person sets aside a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account".

In any group of friends, some people are more giving than others (or at least may have more acquaintances) and some people have more money than others.

Given a group of friends, no one of whom has a name longer than 14 characters, the money each person in the group spends on gifts, and a (sub)list of friends to whom each person gives gifts, determine how much more (or less) each person in the group gives than they receive.

IMPORTANT NOTE

The grader machine is a Linux machine that uses standard Unix conventions: end of line is a single character often known as '\n'. This differs from Windows, which ends lines with two charcters, '\n' and '\r'. Do not let your program get trapped by this!

PROGRAM NAME: gift1

INPUT FORMAT

Line 1:     The single integer, NP
Lines 2..NP+1:     Each line contains the name of a group member
Lines NP+2..end:    NP groups of lines organized like this:
The first line in the group tells the person's name who will be giving gifts.
The second line in the group contains two numbers: The initial amount of money (in the range 0..2000) to be divided up into gifts by the giver and then the number of people to whom the giver will give gifts, NGi (0 ≤ NGi ≤ NP-1).
If NGi is nonzero, each of the next NGi lines lists the the name of a recipient of a gift.
SAMPLE INPUT (file gift1.in)

5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0
OUTPUT FORMAT

The output is NP lines, each with the name of a person followed by a single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed in the same order they appear on line 2 of the input.

All gifts are integers. Each person gives the same integer amount of money to each friend to whom any money is given, and gives as much as possible that meets this constraint. Any money not given is kept by the giver.

SAMPLE OUTPUT (file gift1.out)

dave 302
laura 66
owen -359
vick 141
amr -150

错误信息:
 > Run 1: Execution error: Your program (`gift1') exited with signal
        #8 (floating point exception [usually caused by accessing memory
        out of bounds]). The program ran for 0.000 CPU seconds before the
        signal. It used 3504 KB of memory.

所提交的自己写的代码:
程序代码:
/*
ID: 
PROG: gift1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std;
class Giver
{private:
    int give;
    int recvnum;
    int gain;
    int recv;
    int distribution;
    string name;

public:
    Giver()
    {

    }
    Giver(string name)
    {
        recv=0;
        this->name=name;
    }
    Giver(const Giver&giver)
    {
        this->give=giver.give;
        this->recvnum=giver.give;
        this->gain=giver.gain;
        this->recv=giver.recv;
        this->distribution=giver.distribution;
    }
    string getName() const
    {
        return name;
    }
    void setGive(int give)
    {
        this->give=give;
    }
    void setRecv(int recv)
    {
        this->recv+=recv;
    }
    int getRecv() const
    {
        return recv;
    }
    void setRecvnum(int recvnum)
    {
        this->recvnum=recvnum;
    }
    int getRecvnum() const
    {
        return recvnum;
    }
    void setGain()
    {
        gain=give%recvnum+recv;
    }
    int getDistribution()
    {
        distribution=give/recvnum;
        return distribution;
    }
    int getEarn()
    {
        setGain();
        return gain-give;
    }



};



int main() {
    ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");
    string str;
    int NP;
    fin>>NP;
    Giver* givers=new Giver[NP];
    for(int i=0;i<NP;i++)
    {
        fin>>str;
        Giver g(str);
        givers[i]=g;

    }
    for(int j=0;j<NP;j++)
    {
        fin>>str;
        int k=0;
        int a,b;
        fin>>a>>b;
        while(k<NP)
        {
            if(givers[k].getName()==str)
            {
                givers[k].setGive(a);
                givers[k].setRecvnum(b);

                for(int l=0;l<givers[k].getRecvnum();l++)
                {
                    int m=0;
                    fin>>str;
                    while(m<NP)
                    {
                        if(givers[m].getName()==str)
                        {
                            givers[m].setRecv(givers[k].getDistribution());

                            break;
                        }
                        m++;
                    }
                }
                break;

            }
            k++;
        }


    }
    for(int n=0;n<NP;n++)
    {


        fout<<givers[n].getName()<<" "<<givers[n].getEarn()<<endl;

    }
    return 0;
}




ps:本地运行没问题,也能给出正确答案

另附测试数据
10
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Spafford
Farmer
Venema
Linus
mitnik
300 3
Poulsen
Tanner
Baran
Poulsen
1000 1
Tanner
Spafford
2000 9
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Farmer
Venema
Linus
Tanner
1234 1
Poulsen
Stallman
536 3
Farmer
Venema
Linus
Ritchie
2000 1
mitnik
Baran
79 2
Tanner
Farmer
Farmer
0 0
Venema
12 9
mitnik
Poulsen
Tanner
Stallman
Ritchie
Baran
Spafford
Farmer
Linus
Linus
1000 1
mitnik
 
输出数据
mitnik 2923
Poulsen 557
Tanner 128
Stallman -311
Ritchie -1777
Baran 245
Spafford -1997
Farmer 440
Venema 391
Linus -599
搜索更多相关主题的帖子: different receive exchange friends decided 
2013-11-03 12:00
jiaxingzheng
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-11-3
收藏
得分:0 
没人来答,自个找到错误了。。。
1.无fin.close();
  无fout.close();
不过这好像是次要的。。以前也没加过,但都可以通过。。
2.没有考虑recvnum=0的情况导致除0
2013-11-03 15:28
快速回复:USACO上的一道题,提交后出现下面的错误信息,求解释+纠错,感激不尽! ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020874 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved