119 - Greedy Gift Givers

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Tue Dec 18, 2001 7:59 pm

In 119, there is sentence "Any money not given is kept and is part of a person's ``net worth'' printed in the output."

So, for the case,
1
mary
mary 20 0

I think the output is
mary 20

but the judge require us to output
mary 0

is the question requirement wrong??

bigredteam2000
New poster
Posts: 11
Joined: Sat Nov 17, 2001 2:00 am

Post by bigredteam2000 » Tue Jan 08, 2002 10:02 am

No. The judges are right. Marry does not get anything because she does not receive presents.
The wording of this question is very misleading, though.

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

ACM 119

Post by htl » Wed Jul 03, 2002 6:10 am

Why doesn't this code get accepted?
It always gets WA
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int n,money[10],x,givemoney,n2,receive,y;
char name[10][13],giver[13],receiver[10][13];
while(scanf("%d",&n)!=EOF)
{
for(x=0;x<n;x++)
scanf("%s",name[x]);
for(x=0;x<n;x++)
money[x]=0;
while(scanf("%s",giver)!=EOF)
{
scanf("%d",&givemoney);
scanf("%d",&n2);
if(!n2)
continue;
for(x=0;x<n2;x++)
scanf("%s",receiver[x]);
for(x=0;x<n;x++)
if(strcmp(giver,name[x])==0)
{
money[x]-=(givemoney-givemoney%n2);
break;
}
receive=(givemoney-givemoney%n2)/n2;
for(x=0;x<n2;x++)
for(y=0;y<n;y++)
if(strcmp(name[y],receiver[x])==0)
{
money[y]+=receive;
break;
}
}
for(x=0;x<n;x++)
printf("%s %d\n",name[x],money[x]);
}
}
[/c]

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Wed Jul 03, 2002 10:24 am

you can't use "while(scanf("%s",giver)!=EOF)"
there is exactly n lines of 'giver' data after the names.
btw you should put a blank line between each group in the output.

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

Post by htl » Wed Jul 03, 2002 11:48 am

The thing you said I don't understand:
btw you should put a blank line between each group in the output.
Could you explain it clearly?

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Wed Jul 03, 2002 11:59 am

"The output for each group should be separated from other groups by a blank line."

look at the sample output of the problem description, there is a blank line after "amr -150"

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

Post by htl » Wed Jul 03, 2002 12:04 pm

I put a '\n' after a group of output. And I got PE. I think it's because of the new line after each group. Maybe I don't have to put it. Thanks anyway.

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Wed Jul 03, 2002 12:10 pm

you shouldn't put a blank line after the last group (this is why i said only between groups)

H-kun
New poster
Posts: 1
Joined: Thu Jul 04, 2002 9:49 pm
Contact:

Post by H-kun » Tue Jul 09, 2002 3:16 pm

I still cannot get my program to work. I think the problem is related to the input, because it takes really little time to fail. Could you send me sample input and output? Thank you.
--
Pu. ("Ja ne.")
Helio "Great Mokona" Perroni Filho

lu shukai
New poster
Posts: 7
Joined: Tue Aug 06, 2002 9:26 am

119wa help

Post by lu shukai » Thu Aug 29, 2002 8:04 am

program P119;

{$APPTYPE CONSOLE}

uses
SysUtils;

type
mytype = record
name:array[1..15] of char;
len:integer;
money:integer;
end;
namet= record
name:array[1..15] of char;
len:integer;
end;
var
str:string;
i,n:integer;
people:array[1..15] of mytype;

procedure cal(giv,get:namet;m,nn:integer);
var
i,j:integer;
f:boolean;
begin
for i:=1 to n do
if people.len=get.len then begin
f:=true;
for j:= 1 to get.len do
if people.name[j]<>get.name[j] then begin
f:=false;
break;
end;
if f then break;
end;
people.money:=people.money+m div nn;
end;


procedure setmoney(str:string);
var
i,t,c,m,nn,j:integer;
giv,get:namet;
f:boolean;
begin
t:=1;
while not((str[t]>='a')and(str[t]<='z')) do
t:=t+1;
i:=0;
while (str[t]>='a')and(str[t]<='z') do begin
i:=i+1;
giv.name:=str[t];
t:=t+1;
end;
giv.len:=i;
while not((str[t]>='0')and(str[t]<='9')) do
t:=t+1;
m:=0;
while (str[t]>='0')and(str[t]<='9') do begin
m:=m*10+ord(str[t])-48;
t:=t+1;
end;
while not((str[t]>='0')and(str[t]<='9')) do
t:=t+1;
nn:=0;
while (str[t]>='0')and(str[t]<='9') do begin
nn:=nn*10+ord(str[t])-48;
t:=t+1;
end;
if (m<>0)and(nn<>0) then begin
for j:= 1 to nn do begin
while not((str[t]>='a')and(str[t]<='z')) do
t:=t+1;
i:=0;
while (str[t]>='a')and(str[t]<='z') do begin
i:=i+1;
get.name:=str[t];
t:=t+1;
end;
get.len:=i;

cal(giv,get,m,nn);
end;
for i:=1 to n do
if people.len=giv.len then begin
f:=true;
for j:= 1 to giv.len do
if people.name[j]<>giv.name[j] then begin
f:=false;
break;
end;
if f then break;
end;
people.money:=people.money-(m div nn)*nn;
end;



end;


procedure setname(str:string);
var
j,i,le,c,t:integer;
begin
t:=1;
c:=0;
while c<n do begin
c:=c+1;
while not((str[t]>='a')and(str[t]<='z')) do
t:=t+1;
le:=0;
while (str[t]>='a')and(str[t]<='z') do begin
le:=le+1;
people[c].name[le]:=str[t];
t:=t+1;
end;
people[c].len:=le;
end;
end;

procedure slv;
var
i,j:integer;
begin
for i:= 1 to n do begin
for j:= 1 to people[i].len do
write(people[i].name[j]);
write(' ');
writeln(people[i].money);
end;
end;

begin
while not eof do begin
readln(n);
readln(str);
setname(str);
for i:= 1 to n do
people[i].money:=0;
for i:=1 to n do begin
readln(str);
setmoney(str);
end;
slv;
writeln;
end;
end.

laboni
New poster
Posts: 12
Joined: Sat Sep 14, 2002 9:22 pm
Location: India

119 : GG Giver - Problem with multiple input

Post by laboni » Sat Sep 14, 2002 9:34 pm

Hey!
My program solves the problem correctly but it does not take more than one input.It takes only the first set of input, gives correct result, but then exist. Can u plz find out why?
here is the code in C:
[c]

//greedy gift giver:119

#include<stdio.h>
#include<string.h>

char name[100][200];
long balance[1000];
char t[150];

void main()
{
long x,i,j,k,m,people=0,per,h;
long n; char dummy;

while(scanf("%ld",&n)==1){
for(i=0;i<n;i++)
{
scanf("\n%s",name);
balance=0;
};
for(i=0;i<n;i++)
{
//m=total gifted money by t
scanf("\n%s%ld%ld",t,&m,&people);
//people=peoples to be gifted
if(!m || !people) continue;
per = m/people;
//per= perhead money to be gifted
x = m - per*people;

for(k=0;k<n;k++)
{
if (strcmp(t,name[k])==0)
{
balance[k] += x;
balance[k] -= m;
break;
}

} ; //end of for k
scanf("%c",&dummy);
for(j=0;j<people;j++)
{
scanf("%s",t);
for(k=0;k<n;k++)
{
if (strcmp(t,name[k])==0)
balance[k] += per;
} //end of for k
} //end of for j
} //end of for big i
scanf("%c",&dummy);
//printing
for(i=0;i<n;i++)
printf("%s %ld\n",name,balance);
printf("\n");
} //end of while
} //end of main

[/c]
Hope some one would help me :-?

Yarin
Problemsetter
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume
Contact:

Post by Yarin » Sat Sep 14, 2002 11:21 pm

Your input reading routine seems dangerous. Try this one instead:
while (!feof(stdin)) {
scanf("%d\n",&n);
for(i=0;i<n;i++)
scanf("%s",names);
for(i=0;i<n;i++) {
scanf("%s %d %d",s,&amount,&k);
....
for(j=0;j<k;j++) {
scanf("%s",s);
...
}
....
scanf("\n");
}
}

laboni
New poster
Posts: 12
Joined: Sat Sep 14, 2002 9:22 pm
Location: India

Post by laboni » Sun Sep 15, 2002 5:44 pm

I read everything with gets & got AC now.
still thanks for your advice.

supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

Post by supermin » Sat Nov 16, 2002 10:57 am

Picard wrote:you shouldn't put a blank line after the last group (this is why i said only between groups)

Code: Select all

#include<stdio.h>
#include<string.h>

int main(void)
{
 int n_people,i,j,count;
 char name[10][13],cmp[13],host[13];
 int money[10],cash;
 int give_n;
 
 while(scanf("%d",&n_people)!=EOF)
 {
  
  for(i=0;i<10;i++)
	  money[i]=0;

  i=0;
  while(i<n_people) 
  {
	  scanf("%s",name[i]);
      i++;
  }

  count=0;
  while(count<n_people)
  {
	  scanf("%s %d %d",host,&cash,&give_n);
      if(give_n==0) 
	  {
		  count++;	
          continue;
	  }
	  i=0;
	  while(i<give_n) 
	  {
	   scanf("%s",cmp);
	   for(j=0;j<n_people;j++)
	   if(strcmp(cmp,name[j])==0) money[j]+=cash/give_n;
	   i++;
	  }
	  for(j=0;j<n_people;j++)
		  if(strcmp(host,name[j])==0) money[j]+=cash%give_n-cash;
	  count++;
  }

  
  for(i=0;i<n_people;i++)
  {
   printf(name[i]);
   printf(" %d\n",money[i]);
  }
  
 }
 
 return 0;
}
I think for a while.....but I still can't figure out how to do the "blank line" except the last group..
Could you help me motify my source code....thanx
(I also got the P.E. accepted)[/c]

Olorin
New poster
Posts: 8
Joined: Sun Nov 03, 2002 3:39 pm

Post by Olorin » Wed Nov 20, 2002 4:57 am

Supermin: my dumb $0.02:
I think for a while.....but I still can't figure out how to do the "blank line" except the last group..
Put a blank line before every group except the first?

Just a hint, HTH
-- Elen Sila Lumenn' Omentielvo --

Post Reply

Return to “Volume 1 (100-199)”