第一层第二题:贪婪的礼物赠送者

该问题描述了一群朋友互相赠送金钱作为礼物的场景。每个人根据手中的金额平均分配给指定的接收者,剩余金额保留。程序需要计算每个人在送礼后比原先多或少的钱数。输入包含朋友数量、每个人的名字、每个人的初始金额及接收者列表,输出是每个人净收入的变化情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Greedy Gift Givers

A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchangegifts of money. Each of these friends might or might not give some money to anyor all of the other friends. Likewise, each friend might or might not receivemoney from any or all of the other friends. Your goal in this problem is todeduce 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 moneyevenly among all those to whom he or she is giving a gift. No fractional moneyis available, so dividing 3 among 2 friends would be 1 each for the friendswith 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 atleast 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 14characters, the money each person in the group spends on gifts, and a (sub)listof 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 fromWindows, which ends lines with two charcters, '\n' and '\r'. Do not let yourprogram 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 asingle 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 online 2 of the input.

All gifts are integers. Each person gives the same integer amount of moneyto each friend to whom any money is given, and gives as much as possible thatmeets 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

 

    题目大意是:有一群人要互相送礼物,嗯,没错,送的是钱。每个人要给几个人送礼物,每份礼物的分量(钱数)就是他们手上的钱数除以要分的份数向下取整,剩余的仍归他自己所有(注意:这里的所谓“手上有的钱”不包括他前面的人送给他的钱)。程序的任务是在第一轮送完钱后,算出每个人比原来多多少钱(比原来少的话用负数表示)。

    输入格式:

    第一行一个数字np,表示这堆人有np个。

    接下来有np行,每行一个人名,表示这些人的名字························1

    接下来有np组数据:

        每组数据的第一行是一个人名,代表要送礼物的人。························2        第二行是两个数字n和m,表示这人手上有n块钱,要分给m个人。

        接下来m行是2里面这个人要送礼物的人。

    输出格式:

输出np行,每行由一个字符串和一个数字构成,字符串表示人名,数字表示他比原来多了多少钱。(注意:人名的顺序是按1里面的顺序输出的)。

    数据范围:2≤np≤10,人名长度不大于14

    第一层的题目无需多提,模拟就好,反正楼主看到这题第一反应是不用stl都对不起C++了。

 

 

/*
ID:su1q2d21
LANG:C++
TASK:gift1
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<map>
#define maxn 20
using namespace std;
int main()
{
	freopen("gift1.in","r",stdin);
	freopen("gift1.out","w",stdout);
	int np;
	int i,j;
	int mon;
	int diver;
	string name[maxn];
	map<string,int> get;
	string giver,receiver;
	map<string,int> account;
	cin>>np;
	for(i=1;i<=np;i++){
		cin>>name[i];
		get[name[i]]=0;
	}
	for(i=1;i<=np;i++){
		cin>>giver;
		cin>>mon;
		cin>>diver;
		account[giver]=mon;
		if(diver!=0) mon/=diver;
		get[giver]+=(account[giver]-mon*diver);
		for(j=1;j<=diver;j++){
			cin>>receiver;
			get[receiver]+=mon;
		}
	}
	for(i=1;i<=np;i++) cout<<name[i]<<' '<<(get[name[i]]-account[name[i]])<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值