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

mrmrumman
New poster
Posts: 10
Joined: Sat Jul 05, 2008 11:26 pm
Location: Dhaka
Contact:

Re: 119 Why WA??

Post by mrmrumman » Wed Sep 03, 2008 4:12 am

I am getting WA can you please help?

Code: Select all

# include <stdio.h>
# include <string.h>
int main()
{
	int number;
	int personnum1=10;
	int nameChar=12;
	char name[personnum1][nameChar];
        int counter=1;
	while(scanf("%d",&number)==1)
	{
            if(counter>1)
                printf("\n");
                int serial=number;
		int credit=0,gift=0;
		char receiver[personnum1][nameChar];
		char giver[nameChar];
		int account[serial];
		for(int i=0;i<serial;i++)
			account[i]=0;
		for(int i=0;i<number;i++)
			scanf("%s",name[i]);
		for(int receiverSn=0,serial=0;serial<number;serial++)
		{
			scanf("%s %d %d",giver,&credit,&gift);
                        for(receiverSn=0;receiverSn<gift;receiverSn++)
                            scanf("%s",receiver[receiverSn]);
			for(int i=0;i<number;i++)
			{
					if(strcmp(name[i],giver)==0)
						if(gift!=0)
							account[i]=account[i]-credit+credit%gift;
						else
							account[i]=account[i]+credit;
					for(int j=0;j<receiverSn;j++)
					{
                                            int loop=strcmp(name[i],receiver[j]);
                                                if(loop==0)
						{
							account[i]=account[i]+credit/gift;
                                break;
						}
					}	
			}
		}
                for(int i=0;i<number;i++)
                    printf("%s %d\n",name[i],account[i]);
                //printf("\n");
                counter++;
                
	}
	return 0;
}
R|_|\/|\/|/-\|\|

Megov
New poster
Posts: 3
Joined: Sun Apr 06, 2008 8:20 pm

Re: Compile Error

Post by Megov » Sun Sep 21, 2008 1:11 pm

BTW, what does function index() do ?

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Compile Error

Post by mf » Sun Sep 21, 2008 1:42 pm

Just type 'man index' in your console! That kind of questions is exactly the reason why we have manpages.

If you're (still) on bare windows, here's an online copy of that page: http://www.kernel.org/doc/man-pages/onl ... dex.3.html

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Compile Error

Post by mf » Sun Sep 21, 2008 1:48 pm

mrmrumman wrote:Please see my code for UVA 119 problem it's show me floating point error why?
Usually that error means that you have tried to divide by zero in your program, so double-check every % and /.

blackSajia
New poster
Posts: 4
Joined: Tue Nov 11, 2008 12:14 pm

119- WA Why Plz Help

Post by blackSajia » Wed Nov 26, 2008 1:22 am

I am getting WA. My Code as follows:

Code: Select all

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


class GreedyGiftGivers
{
	private:	
		
		int i,j,mtos,gtos,numberOfMember;
	
		int initMoney[15],finalMoney[15];
	
		char memberName[15][15],giftReceiverName[15][15];
		
		int findPosition(char name[]);

	public:

		GreedyGiftGivers(int numberOfMember);
	
		void pushMemberName(char name[]){strcpy(memberName[++mtos],name);};
	
		void pushGiftReceiverName(char name[]){strcpy(giftReceiverName[++gtos],name);};
	
		void clearGiftReceiverName();
	
		void exchangeGiftMoney(char giftGiverName[],int initialMoney,int numberOfReceiver);
	
		char* getMemberName(int pos){return memberName[pos];};
	
		int getMemberMoney(int pos){return finalMoney[pos]-initMoney[pos];};
	
		~GreedyGiftGivers();		
};

GreedyGiftGivers::GreedyGiftGivers(int numberMember)
{
	mtos=gtos=-1;
		
	numberOfMember=numberMember;
		
	for(i=0;i<numberOfMember;initMoney[i]=finalMoney[i]=0,strcpy(memberName[i],""),strcpy(giftReceiverName[i],""),i++);
}

void GreedyGiftGivers::clearGiftReceiverName(){gtos=-1;for(i=0;i<15;strcpy(giftReceiverName[i],""),i++);}

void GreedyGiftGivers::exchangeGiftMoney(char giftGiverName[],int initialMoney,int numberOfReceiver)
{
		int pos,giftMoney;
		
		pos=findPosition(giftGiverName);initMoney[pos]+=initialMoney;
		
		if (numberOfReceiver!=0){finalMoney[pos]+=initialMoney%numberOfReceiver;giftMoney=initialMoney/numberOfReceiver;}
		else{finalMoney[pos]+=0;giftMoney=0;} 	
		
		for(i=0;i<numberOfReceiver;finalMoney[findPosition(giftReceiverName[i])]+=giftMoney,i++);
}

int GreedyGiftGivers::findPosition(char name[])
{
	for(j=0;j<numberOfMember;j++){if (!strcmp(name,memberName[j])){return j;}}		
		
	return 0;		
}

GreedyGiftGivers::~GreedyGiftGivers(){for(i=0;i<numberOfMember;initMoney[i]=finalMoney[i]=0,strcpy(memberName[i],""),strcpy(giftReceiverName[i],""),i++);}




int main()
{
//	freopen("119.in","r",stdin);

//	freopen("119.out","w",stdout);

	bool flag=false;

	int i,j,numberOfMember,initialMoney,numberOfGiftReceiver;

	char name[15],giftGiverName[15];
	
	while(scanf("%d ",&numberOfMember)==1) 
	{
		
		GreedyGiftGivers ggg(numberOfMember);
			
		for(i=0;i<numberOfMember;scanf("%s",name),ggg.pushMemberName(name),i++);	

		for(i=0;i<numberOfMember;i++)
		{
			scanf("%s%d%d",giftGiverName,&initialMoney,&numberOfGiftReceiver);
		
			for(j=0;j<numberOfGiftReceiver;scanf("%s",name),ggg.pushGiftReceiverName(name),j++);
				
			ggg.exchangeGiftMoney(giftGiverName,initialMoney,numberOfGiftReceiver);
				
			ggg.clearGiftReceiverName();
		}

		flag?putchar('\n'):flag=true;

//		printf("%s %d",ggg.getMemberName(0),ggg.getMemberMoney(0));
			
		for(i=0;i<numberOfMember;printf("%s %d\n",ggg.getMemberName(i),ggg.getMemberMoney(i)),i++);
		
//		printf("\n\n");
	}

	return 0;
}



Plz help me :( :( :(

r2ro
New poster
Posts: 38
Joined: Thu Sep 25, 2008 9:26 am

Re: 119- WA Why Plz Help

Post by r2ro » Mon Dec 01, 2008 4:53 am

Your program fails this test case

Code: Select all

2
Fat Dude
Fat 1000 0
Dude 100 1 Fat
Output should be:

Code: Select all

Fat 100
Dude -100
But you output

Code: Select all

Fat -900
Dude -100
Hope this helps!

gtcoder
New poster
Posts: 12
Joined: Tue Mar 23, 2010 5:45 am

Re: 119 Why WA??

Post by gtcoder » Thu Apr 08, 2010 5:08 am

One of the craziest hard-to-get-AC experience

To get AC you need three things.

1. Check Jan's Sample I/O in this thread and correct it.
2. Check these Sample I/0 and correct it.
Input

Code: Select all

2
Fat Dude
Fat 1000 0
Dude 100 1 Fat
Output

Code: Select all

Fat 100
Dude -100
3. Print a blank line after each case except the last one.

I'm a fool and one of the most stupid people in this world. Took almost 2 days and about 12 WAs to get AC.

sudipta
New poster
Posts: 11
Joined: Wed Sep 30, 2009 7:23 pm
Location: Sylhet

Re: 119 Why WA??

Post by sudipta » Sat May 29, 2010 7:08 pm

My code is giving all the output correctly. But I am getting WA. Please help anyone..

Code: Select all

Deleted After Accepted
Last edited by sudipta on Sun May 30, 2010 8:10 am, edited 1 time in total.
Don't Copy, Think Also

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Re: 119 Why WA??

Post by helloneo » Sat May 29, 2010 7:16 pm

Your code is printing an extra '\n' at the end of output..

sudipta
New poster
Posts: 11
Joined: Wed Sep 30, 2009 7:23 pm
Location: Sylhet

Re: 119 Why WA??

Post by sudipta » Sat May 29, 2010 9:56 pm

How can I stop this? I am weak in these case.
Don't Copy, Think Also

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Re: 119 Why WA??

Post by helloneo » Sun May 30, 2010 5:26 am

Output should be this form.. ([\n] represents a new line character)

Code: Select all

output1[\n]
[\n]
output2[\n]
[\n]
last output[\n]
[\n]  <---------------- this should not exist

sudipta
New poster
Posts: 11
Joined: Wed Sep 30, 2009 7:23 pm
Location: Sylhet

Re: 119 Why WA??

Post by sudipta » Sun May 30, 2010 8:11 am

Thank you very much. I got it accepted.
Don't Copy, Think Also

vickki
New poster
Posts: 3
Joined: Wed Nov 10, 2010 9:11 am

119 problem Run Time Error Please Help Urgent !!!

Post by vickki » Thu Jan 13, 2011 8:31 am

Hi i recnetly started solving the problem Greedy Gift Givers but i am getting Runtime Error i dont know why ? I have tried almost all the inputs provide in the form please. Sorry for Creating another thread related to 119 :-?

Here is the my program

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

class Data
{
	String name;
	int spent;
	int frnd_cnt;
	int recived;
}

public class Main
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList <Data> list = new ArrayList<Data>();
		HashMap hm = new HashMap();
		String str12 = null;
		int count = 1;
		while((str12 = br.readLine())!= null)
		{
			if (count != 1)
					System.out.println();
			count++;
			
			//Scan the Input Very Important
			String names[] = (br.readLine()).split("\\s");

			int position = 0;
			int temp = names.length;

			/*for(int i =0;i<names.length;i++)
			System.out.println(names[i]);*/
			for(int i = 0;i<temp;i++)
			{
				Data d = new Data();
				d.name = names[i];d.spent=0;d.frnd_cnt=0;d.recived=0;
				list.add(d);
				hm.put(names[i],position++);
			}
			/*for(int i =0 ;i< list.size();i++)
			{
				Data d4 = (Data)list.get(i);
				System.out.println(d4.name+" "+d4.spent+" "+d4.frnd_cnt+" "+d4.recived);
			}*/
						
			while(temp-- > 0)
			{
						
					String input[] = (br.readLine()).split("\\s");
					int index = (Integer)hm.get(input[0]);
					Data d1 = (Data)list.get(index);
					int temp_spent = Integer.parseInt(input[1]);
					int temp_frnd_cnt = Integer.parseInt(input[2]);
					if(temp_frnd_cnt == 0 || temp_spent == 0)
						continue;
					d1.spent = d1.spent + temp_spent;
					d1.recived = d1.recived + temp_spent%temp_frnd_cnt;//Done till here

					
					for(int i = 0,j=3;i < temp_frnd_cnt;i++,j++)
					{
						int index1 = (Integer)hm.get(input[j]);
						Data d2 = (Data)list.get(index1);
						d2.recived = d2.recived + (temp_spent/temp_frnd_cnt);
					}
			}
			for(int i =0 ;i< list.size();i++)
			{
				Data d3 = (Data)list.get(i);
				if(d3.spent==0)
					System.out.println(d3.name+" "+d3.recived);
				else
					System.out.println(d3.name+" "+(d3.recived-d3.spent));
			}
			hm.clear();
			list.clear();
		}
			

	}
}
Below are my input file and output file.
Input

Code: Select all

8
atxdknly yhfixjsw kkufnuxxzr bmnmgq oketl hnkoaugzq cddi teiojwa
kkufnuxxzr 886 1 yhfixjsw
hnkoaugzq 142 5 atxdknly yhfixjsw cddi kkufnuxxzr bmnmgq
hnkoaugzq 1699 3 oketl oketl bmnmgq
atxdknly 600 1 bmnmgq
hnkoaugzq 1861 4 yhfixjsw teiojwa teiojwa kkufnuxxzr
yhfixjsw 182 1 atxdknly
kkufnuxxzr 617 3 atxdknly oketl yhfixjsw
kkufnuxxzr 1721 5 atxdknly yhfixjsw atxdknly oketl yhfixjsw
10
qghume ylnlf xfir vscxggb kfnqdux fnf zvsrtkjpr pggxrpn vystmwcy yyc
zvsrtkjpr 1805 1 yyc
qghume 1350 6 ylnlf vscxggb vystmwcy yyc vscxggb kfnqdux
kfnqdux 756 4 zvsrtkjpr zvsrtkjpr ylnlf vystmwcy
kfnqdux 439 1 vscxggb
pggxrpn 1538 5 xfir yyc ylnlf vscxggb fnf
yyc 1658 4 qghume pggxrpn zvsrtkjpr vscxggb
zvsrtkjpr 1021 4 kfnqdux xfir qghume yyc
pggxrpn 1573 2 xfir zvsrtkjpr
qghume 1161 3 fnf pggxrpn fnf
kfnqdux 31 6 qghume qghume ylnlf zvsrtkjpr qghume pggxrpn
2
Fat Dude
Fat 1000 0
Dude 100 1 Fat
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
3
liz steve dave
liz 30 1 steve
steve 55 2 liz dave
dave 0 2 steve liz
2
dave liz
dave 200 0
liz 100 1 dave 
Output

Code: Select all

atxdknly 503
yhfixjsw 2090
kkufnuxxzr -2728
bmnmgq 1194
oketl 1681
hnkoaugzq -3698
cddi 28
teiojwa 930

qghume -1827
ylnlf 726
xfir 1348
vscxggb 1610
kfnqdux -745
fnf 1081
zvsrtkjpr -1242
pggxrpn -2301
vystmwcy 414
yyc 936

Fat 100
Dude -100

dave 302
laura 66
owen -359
vick 141
amr -150

liz -3
steve -24
dave 27

dave 100
liz -100

Please Help me i right now don't what to do next.

Scarecrow
Learning poster
Posts: 69
Joined: Wed Oct 19, 2011 9:06 pm

Re: 119 Why WA??

Post by Scarecrow » Sun Jan 22, 2012 1:30 pm

i can't figure it out why i'm getting WA.. :( plz can any1 help me..

Code: Select all

removed after AC
Last edited by Scarecrow on Thu Jan 26, 2012 10:44 pm, edited 1 time in total.
Do or do not. There is no try.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 119 Why WA??

Post by brianfry713 » Wed Jan 25, 2012 1:24 am

Scarecrow, try this I/O:
gtcoder wrote: Input

Code: Select all

2
Fat Dude
Fat 1000 0
Dude 100 1 Fat
Output

Code: Select all

Fat 100
Dude -100
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 1 (100-199)”