USACO SEC.1.1 NO.2 Greedy Gift Givers

题意:给定N个名字, 接下来对于每个名字, 输入给出总钱数, 和人数,以及给到的每个人

最后输出每个人最后的实际获得钱数

题解:映射关系, 例如使用Hash方法或者高速映射方法, C++中可以采用STL的map数据结构

USACO的习题提交格式略麻烦

每次需要修改: 题目名称(头部注释中) 读入输出文件名称(ifstream, ofstream中)

/*
ID: lsswxr1
PROG: gift1
LANG: C++
*/
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include <string>
#include 
using namespace std;

///宏定义
const int  INF = 1000000000;
const int MAXN = 1010;
const int maxn = MAXN;
///全局变量 和 函数

#define USACO

#ifdef USACO
#define cin fin
#define cout fout
#endif
//
int np;
map<string, int> receiveMoney;
map<string, int> sendoutMoney;
string names[maxn];
int main()
{

    
#ifdef USACO
    ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");
#endif
    //
    ///变量定义
    receiveMoney.clear();
    sendoutMoney.clear();
    cin >> np;
    for (int i = 0; i < np; i++)
    {
        string tmp;
        cin >> tmp;
        names[i] = tmp;
        receiveMoney[tmp] = 0;
        sendoutMoney[tmp] = 0;
    }
    string senderName;
    while (cin >> senderName)
    {
        int originMoney, persons;
        cin >> originMoney >> persons;
        if (persons == 0)
            continue;
        int ave = originMoney / persons;
        for (int i = 0; i < persons; i++)
        {
            string recName;
            cin >> recName;
            receiveMoney[recName] += ave;
            sendoutMoney[senderName] += ave;
        }
    }

    for (int i = 0; i < np; i++)
    {
        cout << names[i] << " " << receiveMoney[names[i]] - sendoutMoney[names[i]] << endl;
    }


    ///结束
    return 0;
}


 

转载于:https://www.cnblogs.com/rayforsure/p/3425849.html

你可能感兴趣的:(USACO SEC.1.1 NO.2 Greedy Gift Givers)