题目:
输入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':dave | laura | owen | vick | amr |
0 | 0 | 0 | 0 | 0 |
First, 'dave' splits 200 among 'laura', 'owen', and 'vick'. That comes to 66 each, with 2 left over | ||||
-200+2 | 66 | 66 | 66 | 0 |
Second, 'owen' gives 500 to 'dave': | ||||
-198+500 | 66 | 66-500 | 66 | 0 |
Third, 'amr' splits 150 between 'vick' and 'owen': | ||||
302 | 66 | -434+75 | 66+75 | -150 |
Fourth, 'laura' splits 0 between 'amr' and 'vick'; no changes: | ||||
302 | 66 | -359 | 141 | -150 |
Finally, 'vick' gives 0 to no one: | ||||
dave | laura | owen | vick | amr |
302 | 66 | -359 | 141 | -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;
}
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;
}