Greedy Gift Givers
A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.
The rules for gift-giving are potentially different than you might expect. Each person sets aside a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account".
In any group of friends, some people are more giving than others (or at least may have more acquaintances) and some people have more money than others.
Given a group of friends, no one of whom has a name longer than 14 characters, the money each person in the group spends on gifts, and a (sub)list of friends to whom each person gives gifts, determine how much more (or less) each person in the group gives than they receive.
IMPORTANT NOTE
The grader machine is a Linux machine that uses standard Unix conventions: end of line is a single character often known as '\n'. This differs from Windows, which ends lines with two charcters, '\n' and '\r'. Do not let your program get trapped by this!
PROGRAM NAME: gift1
INPUT FORMAT
Line 1: | The single integer, NP | |||
Lines 2..NP+1: | Each line contains the name of a group member | |||
Lines NP+2..end: | NP groups of lines organized like this:
|
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 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
题意:
共有NP个人,NP个人轮流分钱。轮流输入分钱人的名字S,输入拥有的钱数M与分的人数N,再依次输入要分给对象的人名(一个N个),分完后分钱人剩下钱数(S%N),被分钱对象分得的钱数为平均数(S/N),算出最后每人剩余的钱数。NP范围为2到10,钱数M是0到2000。
思路:
1.定义结构体(Name and money),然后用结构体数组来存数据,将NP个人的信息存到数组中。一开始先初始化每个人的钱数都是0再开始统计,统计完后在依次输出人名和钱数;
2.定义name[20][20]来保存人名,再定义money[20]来保存人对应的钱数,然后跟上面的情况同样处理,要注意的是这里的数字要对应好。
第一次提交时错误的代码:
/* TASK:gift1 LANG:C ID:sum-g1 */ #include<stdio.h> #include<string.h> struct person //定义结构体 { char name[50]; int money; }a[15]; int main() { FILE *fin =fopen("gift1.in","r"); FILE *fout=fopen("gift1.out","w"); int N,i,cash,number,k,s,j; //N为总共的人数 char n[50]; fscanf(fin,"%d",&N); for(i=1;i<=N;i++) //输入人名,同时对该人的钱数初始化 { fscanf(fin,"%s",n); strcpy(a[i].name,n); a[i].money=0; } for(i=1;i<=N;i++) { fscanf(fin,"%s",n); //输入分钱人 for(j=1;j<=N;j++) if(!strcmp(a[j].name,n)) break;//找到这个分钱人 fscanf(fin,"%d%d",&cash,&number); //输入分多少钱和分给多少人 if(number==0&&cash==0) break; //这里不应该是break的一开始还是没发现是除数作为0情况时的问题... a[j].money=a[j].money-cash+cash%number; //如果number=0的话作为除数是没有意义的 for(k=1;k<=number;k++) { fscanf(fin,"%s",n); //输入要分给谁 for(s=1;s<=N;s++) //找到这个人 if(!strcmp(a[s].name,n)) {a[s].money+=(cash/number);break;} //同理,这里也是 } } for(i=1;i<=N;i++) //最后输出这人些人分钱后的剩余钱数 fprintf(fout,"%s %d\n",a[i].name,a[i].money); exit(0); }
Debug:
一开始没发现是除数问题的时候,一直在死盯着看自己写的代码,所以我决定每进行一步都输出来看看它此刻运行的状态来找出到底是哪里的问题。并且把结构体改成用数组对应的方法来解决这道题。
#include<stdio.h> #include<string.h> int main() { int N,i,j,give,who,k,l; char name[10][10]; char temp[50]; int money[10]; memset(name,0,sizeof(name)); memset(money,0,sizeof(money)); scanf("%d",&N); for(i=0;i<N;i++) { scanf("%s",name[i]); money[i]=0; } //输入人名并初始化后,进行一次输出查看,发现这里没有问题。 // printf("\n"); // for(i=0;i<N;i++) // printf("%s %d\n",name[i],money[i]); for(i=0;i<N;i++) { scanf("%s",temp); j=0; while(strcmp(name[j],temp)) j++; //输入分钱人后,看看能不能正确的找到这个人 // printf("\n%d\n",j); scanf("%d%d",&give,&who); if(!give&&!who) money[j]=money[j]; if(give&&who) money[j]=money[j]+give%who-give; if(give&&!who) money[j]=money[j]-give; if(who) { //一开始没有红色字体部分 for(k=0;k<who;k++) { l=0; scanf("%s",temp); while(strcmp(temp,name[l])) l++; money[l]=money[l]+give/who; } } } for(i=0;i<N;i++) //最后一次输出查看总结果,结果却输不出来,故有问题的为下面那部分 printf("%s %d\n",name[i],money[i]); return 0; } //最后我还验证了一下是不是真的是除数问题,所以另外写了个简单的代码来测试一下,结果真的是这样 #include<stdio.h> int main() { int n=0,m=0; printf("%d %d\n",n/m,n%m); }
The second times and AC at the end:
/* TASK:gift1 LANG:C ID:sum-g1 */ #include<stdio.h> #include<string.h> int main() { FILE *fin =fopen("gift1.in","r"); FILE *fout=fopen("gift1.out","w"); int N,i,j,give,who,k,l; char name[10][10]; char temp[50]; int money[10]; memset(name,0,sizeof(name)); memset(money,0,sizeof(money)); fscanf(fin,"%d",&N); for(i=0;i<N;i++) { fscanf(fin,"%s",name[i]); money[i]=0; } for(i=0;i<N;i++) { fscanf(fin,"%s",temp); j=0; while(strcmp(name[j],temp)) j++; fscanf(fin,"%d%d",&give,&who); if(!give&&!who) money[j]=money[j]; if(give&&who) money[j]=money[j]+give%who-give; if(give&&!who) money[j]=money[j]-give; if(who) { for(k=0;k<who;k++) { l=0; fscanf(fin,"%s",temp); while(strcmp(temp,name[l])) l++; money[l]=money[l]+give/who; } } } for(i=0;i<N;i++) fprintf(fout,"%s %d\n",name[i],money[i]); exit(0); }
总结:
一开始想的是能不能用map,可能是刚学了的关系,想要马上能用到。但是数据存进去之后是自动排好序的,而且题目给出人数的范围最多只有10个,这样子的话时间复杂度也只有0(N^2),也就是100,这样的话,也不需要用到map这种数据结构,所以还是决定不用map。于是试着从简单的办法想起。其实题目不难,但是自己硬是把它给复杂化了,学过的东西乱又杂,还不会运用,用到的时候又是不正确的。但是当你把问题简单化了之后,你还是有可能会做不出来,因为练得少,所以一用起来到处都是Bug,Debug也要花很长的时间,现在犯的都是写很低级的错误,虽然低级却又不容易发现,说明不够细心。