USACO_1_1_Greedy Gift Givers

本文介绍了一个使用C++实现的礼品交换程序。该程序通过定义一个PEOPLE类来跟踪参与者赠送和收到的金额,并最终计算出每位参与者之间的收支差额。
ExpandedBlockStart.gifContractedBlock.gif/**//*
ID: sdjllyh1
PROG: gift1
LANG: C++
TIME: 2008/9/7
*/


//#include "stdafx.h"
#include <iostream>
#include 
<fstream>
#include 
<string>
#include 
<vector>
using namespace std;

class PEOPLE
ExpandedBlockStart.gifContractedBlock.gif
{
private:
    
string m_name;
    
int m_giveMoney;
    
int m_receiveMoney;

public:
    PEOPLE(
string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        m_name 
= name;
        m_giveMoney 
= 0;
        m_receiveMoney 
= 0;
    }

    
void SetGiveMoney(int giveMoney)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        m_giveMoney 
= giveMoney;
    }

    
void AddReceiveMoney(int receiveMoney)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        m_receiveMoney 
+= receiveMoney;
    }

    
string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return m_name;
    }

    
int GetDifference()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return m_receiveMoney - m_giveMoney;
    }


}
;


vector
<PEOPLE> people;

int n;

int FindNumber(string name)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int retNum = 0;
    
for(int i=0; i<n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(people[i].GetName() == name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            retNum 
= i;
            
break;
        }

    }

    
return retNum;
}


ExpandedBlockStart.gifContractedBlock.gif
int main() {
    ofstream fout (
"gift1.out");
    ifstream fin (
"gift1.in");
    
    fin
>>n;
    
string name;
    
for(int i=0; i<n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        fin
>>name;
        PEOPLE newPeople(name);
        people.push_back(newPeople);
    }


    
for(int i=0; i<n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
string giver;
        
int hasMoney;
        
int receiverCount;
        fin
>>giver>>hasMoney>>receiverCount;
        
if(receiverCount > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int num = FindNumber(giver);
            
int giveMoney = ((int)(hasMoney / receiverCount)) * receiverCount;
            people[num].SetGiveMoney(giveMoney);
            
            
for(int j=0; j<receiverCount; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string receiverName;
                fin
>>receiverName;
                
int receiverNum = FindNumber(receiverName);
                people[receiverNum].AddReceiveMoney(hasMoney 
/ receiverCount);
            }

        }

    }


    
//show
    for(int i=0; i<n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        fout
<<people[i].GetName()<<' '<<people[i].GetDifference()<<endl;
    }


    
return 0;
}



转载于:https://www.cnblogs.com/SDJL/archive/2008/09/07/1286269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值