当前位置: 代码迷 >> 综合 >> USACO天梯--Greedy Gift Givers
  详细解决方案

USACO天梯--Greedy Gift Givers

热度:1   发布时间:2023-12-17 02:24:56.0

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.


SAMPLE INPUT (file gift1.in)

5------/*朋友的数目*/
dave--------/*以下5行是朋友的名字*/
laura
owen
vick
amr
dave----------------------/*给礼物的人的姓名*/
200 3------------------/*礼物的总价值   要分礼物的人数*/
laura----------------------/*以下3行为得到礼物的人的姓名*/          /*下面格式就不再叙述,同上*/
owen
vick
owen----------------------/*给礼物的人的姓名*/
500 1-------------/*礼物的总价值   要分礼物的人数*/
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0

SAMPLE OUTPUT (file gift1.out)

dave 302
laura 66
owen -359
vick 141
amr -150
 
这是一道很常规的模拟题,难度也就可能在NOIP的Day1第一题难度,这里要注意的是文件的读入读取操作,同时注意对应每个人的支出与获得。
题目注意:dave最后为什么是302的原因:
200分三个人不能整分,199也不行,198可以整分。所以支出198,再从amr那里得到500,有500-198=302.
代码如下:
/*
ID:wang ming
PROG:gift1
LANG:C++
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{FILE *fin=fopen("gift1.in","r");FILE *fout=fopen("gift1.out","w");char friends[10][15],giver[10][15],list[10][15];int friends1[10]={0};int NP,i,j,k,l,m,n,z,ll,num,money;                                /*朋友的数量*/fscanf(fin,"%d",&NP);for(i=0;i<NP;i++)                  /*读入朋友名单*/{fscanf(fin,"%s",&friends[i]);/*把朋友的钱初始清0*/}/* for(i=0;i<NP;i++)cout<<friends[i]<<" "<<friends1[i]<<endl;*/for(i=0;i<NP;i++){fscanf(fin,"%s",&giver[i]);                                /*赠送朋友的数量*/fscanf(fin,"%d%d",&money,&num);if(num!=0){for(n=0;n<NP;n++){if(strcmp(friends[n],giver[i])==0)friends1[n]-=(money-money%num);}for(m=0;m<num;m++){fscanf(fin,"%s",&list[m]);for(n=0;n<NP;n++)if(strcmp(friends[n],list[m])==0)friends1[n]+=(money-money%num)/num;} }}for(ll=0;ll<NP;ll++)fprintf(fout,"%s %d\n",friends[ll],friends1[ll]);return 0;
}