对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for those who view gift giving with cynicism). 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物 的人. 然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱. 给出一群朋友, 没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物 的人的列表, 请确定每个人收到的比送出的钱多的数目.
输入格式:
第 1 行:人数 NP,2<=NP<=10
第 2 到 NP+1 行:这 NP 个在组里人的名字 (一个名字一行 )
第 NP+2 到最后:这里的 NP 段内容是这样组织的:
第一行是将会送出礼物人的名字.
第二行包含两个数字: 第一个是原有的钱的数目(在 0 到 2000 的范围里),第二个 NGi 是将收到 这个送礼者礼物的人的个数,如果NGi是非零的, 在下面 NGi 行列出礼物的接受者的名字(一个名字一行)。
输出格式:
输出 NP 行
每行是一个的名字加上空格再加上收到的比送出的钱多的数目.
对于每一个人,他名字的打印顺序应和他在输入的2到NP+1行中输入的顺序相同.所有的送礼的钱都是整数.
每个人把相同数目的钱给每位要送礼的朋友,而且尽可能多给,不能给出的钱被送礼者自己保留.
输入样例1:
在这里给出一组输入。例如:
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
输出样例1:
在这里给出相应的输出。例如:
dave 302
laura 66
owen -359
vick 141
amr -150
输入样例2:
2
john
lennon
lennon
0 0
john
0 0
输出样例2:
john 0
lennon 0
输入样例3:
10
Alex
Bob
Catherine
Dave
Ebert
Francis
Godot
Harris
Iliya
Jimbo
Alex
2000 9
Bob
Catherine
Dave
Ebert
Francis
Godot
Harris
Iliya
Jimbo
Bob
2000 9
Alex
Catherine
Dave
Ebert
Francis
Godot
Harris
Iliya
Jimbo
Catherine
2000 9
Alex
Bob
Dave
Ebert
Francis
Godot
Harris
Iliya
Jimbo
Dave
2000 9
Alex
Bob
Catherine
Ebert
Francis
Godot
Harris
Iliya
Jimbo
Ebert
2000 9
Alex
Bob
Catherine
Dave
Francis
Godot
Harris
Iliya
Jimbo
Francis
2000 9
Alex
Bob
Catherine
Dave
Ebert
Godot
Harris
Iliya
Jimbo
Godot
2000 9
Alex
Bob
Catherine
Dave
Ebert
Francis
Harris
Iliya
Jimbo
Harris
2000 9
Alex
Bob
Catherine
Dave
Ebert
Francis
Godot
Iliya
Jimbo
Iliya
2000 9
Alex
Bob
Catherine
Dave
Ebert
Francis
Godot
Harris
Jimbo
Jimbo
2000 9
Alex
Bob
Catherine
Dave
Ebert
Francis
Godot
Harris
Iliya
输出样例3:
Alex 0
Bob 0
Catherine 0
Dave 0
Ebert 0
Francis 0
Godot 0
Harris 0
Iliya 0
Jimbo 0
【源代码】
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person
{
public:
Person(string);
Person(); // 默认构造函数
~Person();
string sName = "";
int iMoney = 0; // 原有的钱
int gift = 0; // 单次送出的礼物价值
int gifts = 0; // 总共送出的礼物总价值
void give_iMoney(int, int);
void get(int);
void result();
};
Person::Person()
{
}
Person::Person(string s)
{
this->sName = s;
}
void Person::give_iMoney(int iMoney, int iNum) // 送礼函数
{
this->iMoney = iMoney;
if (iNum != 0)
{
this->gift = this->iMoney / iNum;
this->gifts += ((this->iMoney / iNum) * (-iNum));
}
else if(iNum == 0)
{
this->gift = 0;
}
}
void Person::get(int gift) // 收礼函数
{
this->iMoney += gift; // 收到的礼物可用来送礼
this->gifts += gift;
}
void Person::result() // 打印函数
{
cout << this->sName << " " << this->gifts << endl;
}
Person::~Person()
{
}
Person& rePerson(vector<Person>& people, int N, string name) //匹配姓名函数
{
for (int i = 0; i < N; i++)
{
if (people[i].sName == name)
{
return people[i];
}
}
}
int main()
{
int iNum_per;
cin >> iNum_per;
string name, gift_name; // name: 送礼者姓名;gift_name:收礼者姓名
vector<Person> people(iNum_per);
for (unsigned int i = 0; i < people.size(); i++) // 储存N个Person对象;
{
cin >> name;
people[i] = Person(name);
}
int iMoney, iNum;
for (unsigned int i = 0; i < people.size(); i++) // 送礼与收礼
{
cin >> name;
cin >> iMoney >> iNum;
rePerson(people, people.size(), name).give_iMoney(iMoney, iNum); // 送出礼物
for (int j = 1; j <= iNum; j++)
{
cin >> gift_name;
rePerson(people, people.size(), gift_name).get(rePerson(people, people.size(), name).gift); // 收入礼物
}
}
for (unsigned int i = 0; i < people.size(); i++) // 打印姓名及收礼和送礼的总价值;
{
people[i].result();
}
return 0;
}
本文介绍了一种用于计算朋友间礼物交换的算法,确保每个人送出和收到的礼物价值平衡。通过输入参与者名单、各自预算和赠送对象,算法计算出每个人最终的收支情况。
205

被折叠的 条评论
为什么被折叠?



