2017-05-20
题目大意:
对于一群(NP个)(2<=NP<=10)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少。 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。 然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。 给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表, 请确定每个人收到的比送出的钱多的数目。
题解:
第一次做这题的时候还不懂数据结构,因为数据量小,2<=NP<=10, 数据存放在结构体里,线性时间查找也是没问题的。第二次做的时候用的map, 是平衡二叉树的实现。现在是第三次做,用的C++11里的unordered_map, 用的是hash的方法。
代码:
/*
ID: zachery1
PROG: gift1
LANG: C++11
*/
#include <iostream>
#include <fstream>
#include <unordered_map>
#define MAXN 20
#define cin fin
#define cout fout
using namespace std;
ifstream fin("gift1.in");
ofstream fout("gift1.out");
int n;
string names[MAXN];
unordered_map<string, int> umap;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> names[i];
umap[names[i]] = 0;
}
int total, num;
string giver;
while (cin >> giver >> total >> num) {
if (num == 0) continue; //防止0作为除数
int m = total / num;
umap[giver] -= (total - (total%num));
for (int i = 0; i < num; i++) {
string receiver;
cin >> receiver;
umap[receiver] += m;
}
}
for (int i = 0; i < n; i++)
cout << names[i] << " " << umap[names[i]] << endl;
return 0;
}