[usaco]Greedy Gift Givers题解

题目:

输入n以及n个人的名字,然后进行n轮送钱……每轮输入付钱者姓名,收钱人数,总金额,然后输入收钱者姓名。最后输出每个人的资产变化结果(初始为0)。

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 a single 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 starting on line 2 of the input.

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

OUTPUT EXPLANATION

Five names: dave, laura, owen, vick, amr Let's keep a table of the gives (money) each person 'has':
davelauraowenvickamr
00000
First, 'dave' splits 200 among 'laura', 'owen', and 'vick'. That comes to 66 each, with 2 left over
-200+26666660
Second, 'owen' gives 500 to 'dave':
-198+5006666-500660
Third, 'amr' splits 150 between 'vick' and 'owen':
30266-434+7566+75-150
Fourth, 'laura' splits 0 between 'amr' and 'vick'; no changes:
30266-359141-150
Finally, 'vick' gives 0 to no one:
davelauraowenvickamr
30266-359141-150

题解:

判断两个字符串是否相等使用strcmp函数,参数为两个指针,返回0则相等。若收钱者个数为0则不能直接除以0否则程序错误。输入繁杂,除此以外没什么难点。

代码:

/*
ID: xcwhkh1
LANG: C
TASK: gift1
*/
#include <stdio.h>
#include<string.h>
int main () {
    FILE *fin  = fopen ("gift1.in", "r");
    FILE *fout = fopen ("gift1.out", "w");
    int n,i,pay,r;
    char name[10][100];
    char na[100],payer[100];
    int money[10][2];
    for(int i=0;i<10;i++)
    money[i][0]=i;
    for(int i=0;i<10;i++)
    money[i][1]=0;
    fscanf (fin,"%d",&n);
for(i=0;i<n;i++)
{
fscanf(fin,"%s",name[i]);
}

    for(int ii=0;ii<n;ii++)//给钱次数 
    {
    fscanf(fin,"%s",payer);
    for(i=0;i<n;i++)
    if(strcmp(name[i],payer)==0){
    fscanf(fin,"%d %d",&pay,&r);
    if(r==0)
    continue;
    else
    for(int j=0;j<r;j++)
    {
    fscanf(fin,"%s",na);
    for(int k=0;k<n;k++)
    if(strcmp(name[k],na)==0)
    money[k][1]+=pay/r;
}
money[i][1]-=pay/r*r;
}
}
for(int i=0;i<n;i++)
fprintf (fout,"%s %d\n",name[i],money[i][1]);
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值