【英文原題】app
Greedy Gift Giverside
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 some or all of the other friends (although some might be cheap and give to no one). Likewise, each friend might or might not receive money from any or all of the other friends. Your goal is to deduce how much more money each person receives than they give.this
The rules for gift-giving are potentially different than you might expect. Each person goes to the bank (or any other source of money) to get 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 7 among 2 friends would be 3 each for the friends with 1 left over – that 1 left over goes into the giver's "account". All the participants' gift accounts start at 0 and are decreased by money given and increased by money received.spa
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:code
determine how much money each person ends up with.blog
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 characters, '\n' and '\r'. Do not let your program get trapped by this!ip
Line # | Contents | |||
---|---|---|---|---|
1 | A single integer, NP | |||
2..NP+1 | Line i+1 contains the name of group member i | |||
NP+2..end | NP groups of lines organized like this:
|
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
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.ci
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.字符串
dave 302 laura 66 owen -359 vick 141 amr -150
Five names: dave, laura, owen, vick, amr. Let's keep a table of the gives (money) each person 'has':
【中文翻譯】
對於一羣(NP個)要互送禮物的朋友,GY要肯定每一個人送出的錢比收到的多多少。在這一個問題中,每一個人都準備了一些錢來送禮物,而這些錢將會被平均分給那些將收到他的禮物的人。然而,在任何一羣朋友中,有些人將送出較多的禮物(多是由於有較多的朋友),有些人有準備了較多的錢。給出一羣朋友,沒有人的名字會長於 14 字符,給出每一個人將花在送禮上的錢,和將收到他的禮物的人的列表,請肯定每一個人收到的比送出的錢多的數目。
輸入格式:
第 1 行: 人數NP,2<= NP<=10
第 2 行 到 第NP+1 行:這NP個在組裏人的名字一個名字一行
第NP+2到最後:
這裏的I段內容是這樣組織的:
第一行是將會送出禮物人的名字。
第二行包含二個數字:第一個是原有的錢的數目(在0到2000的範圍裏),第二個 NGi 是將收到這我的禮物的人的個數 若是 NGi 是非零的, 在下面 NGi 行列出禮物的接受者的名字,一個名字一行。
輸出格式:
輸出NP行
每行是人的名字和每一個人收到的比送出的錢多的數目
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
思路:這道題目是純粹的模擬題,按照題目的要求來走。
這題能夠用結構體來實現(網上有許多用map來解決的,但我學的是C因此沒用,不過用map確實方便很多~)。
首先定義結構體變量people,將每一個的名字進行存儲,而後開始循環:
定位到當前要送禮的這我的(字符串查找),而後將這我的的錢平均分配給那些他要送給的人(即受贈人所擁有的錢增長)
一直這樣循環完每個人……
最後掃描一遍整個結構體,輸出每一個人的名字和他收到的比送出的錢多的數目(負數表示他「虧了」,正數表示他「賺了」)
代碼以下:
1 #include<stdio.h> 2 #include<string.h> 3 struct people//定義結構體people 4 { 5 char name[14];//姓名 6 int money;//某人所擁有的錢數 7 int list;//要送禮物的列表人數 8 int give;//送給每一個人的錢數 9 }; 10 struct people p[20]; 11 int n,money,list,b; //人數n、某人所擁有的錢數(暫存)、要送禮物的列表人數(暫存)、送給每一個人的錢數(暫存) 12 char man[14];//姓名暫存變量 13 int moneys(int mo,int friends)//計算送給每一個人的錢數 14 { 15 if(friends==0) 16 { 17 return 0;/*注意判斷要送的人數(list)是否爲0,不然會出現除數爲0而運行錯誤的狀況*/ 18 } 19 return mo/friends;//若非0則返回結果(向下取整) 20 } 21 int main() 22 { 23 scanf("%d",&n); 24 int i,j,k; 25 for(i=0;i<n;i++) 26 { 27 scanf("%s",p[i].name);//輸入每一個人的明名字 28 } 29 for(i=0;i<n;i++) 30 { 31 scanf("%s%d %d",man,&money,&list);//先將輸入內容暫時存入變量中 32 for(j=0;j<n;j++) 33 { 34 if(strcmp(man,p[j].name)==0)//搜索姓名相符的變量 35 { 36 p[j].list=list; 37 p[j].give=moneys(money,p[j].list);//將信息存入此結構體變量內 38 b=j;//保存此變量的下標,留着送禮時用(由於j的值會變更) 39 } 40 } 41 for(j=0;j<p[b].list;j++)//往下循環他將送禮的人 42 { 43 scanf("%s",man);//輸入收禮人姓名 44 for(k=0;k<n;k++) 45 { 46 if(strcmp(p[k].name,man)==0)//搜索與姓名相符的變量 47 { 48 p[k].money+=p[b].give;//收禮人擁有錢數增長 49 p[b].money-=p[b].give;//送禮人錢數對應減小 50 } 51 } 52 } 53 } 54 for(i=0;i<n;i++) 55 { 56 printf("%s %d\n",p[i].name,p[i].money);//按要求依次輸出信息 。 57 } 58 return 0; 59 }