119 - Greedy Gift Givers
Moderator: Board moderators
-
- New poster
- Posts: 11
- Joined: Sat Nov 17, 2001 2:00 am
ACM 119
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]
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]
119wa help
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.
{$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.
119 : GG Giver - Problem with multiple input
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
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

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;
}
Could you help me motify my source code....thanx
(I also got the P.E. accepted)[/c]