Page 4 of 11

Re: 11340 - Newspaper

Posted: Wed Jan 07, 2009 8:15 am
by Obaida
Please help me.. So many WA!!!!

Code: Select all

removed

Re: 11340 - Newspaper

Posted: Thu Jan 29, 2009 12:24 pm
by Obaida
Some one plz reply..... :oops:

Re: 11340 - Newspaper

Posted: Fri Jan 30, 2009 1:00 am
by kbr_iut
u r having problem coz of overfloaw.
according to the problem ur sum variable may contain...
150000*10000*1000 = 1500000000000 which cant be supported within long or int range.........

i used a trick....when sum exceeds 100 make it dollar and keep it to another variable var...
then i just printed var and sum...
secondly ur program is printing some extra newline and there is also a problem handling extra space and newline.

i modified ur code a bit. open ur inbox.u coud see...( hey man.....it takes a unsigned long long time to find the actualy bug)

anyway.....keep posting.

Re: 11340 - Newspaper

Posted: Fri Aug 07, 2009 10:24 am
by calicratis19
to clear up the confusion

1)long is enough.
2)unsigned char is needed.

i got wa for the way i printed output.

in my ac code i used
printf("%ld.%02ld$\n",dollar,cents);
and then i got ac.

hope it helps.

Re: 11340 - Newspaper

Posted: Tue Apr 20, 2010 4:18 am
by amishera
From the above posts, many comments conflict with each other. Some people are saying char is enough, some is saying unsigned char is required. Some are saying to use long long, some are saying int is enough. Which one is correct actually? See I used this according to some suggestions made before:

Code: Select all

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

[b]#define to_unsigned_char(x) if (x < 0) x += 256[/b]
int table[1000000];

int main()
{		
	char article[1000000];
	int n;
	int k;
[b]	char c;[/b]
	int p;
	int m;
	int len;
	long long sum_dollar;
	int sum_cents;
	int i;
	int j;
	int t;
	scanf(" %d",&n);
	int first = 1;

	while (n-- > 0)
	{
		scanf(" %d",&k);
		for (j = 0;j < 512;j++)
		{
			table[j] = 0;
		}

		while (k-- > 0)
		{			
		[b]	scanf(" %c %d",&c, &p);
			t = c;	
			to_unsigned_char(t);
			table[(int)t] = p;[/b]
		}

		scanf("%d",&m);

		sum_dollar = 0;
		sum_cents = 0;
		while (m-- > 0)
		{
			scanf(" %[^\n]",article);
			for (k = 0;article[k];k++)
			{
				[b]t = article[k];
				to_unsigned_char(t);
				sum_cents += table[t];
				if (sum_cents >= 100) {
					sum_cents -= 100;
					sum_dollar++;
				}[/b]
			}
		}
		[b]printf("%lld", sum_dollar);
		if (sum_cents > 0)
		{
			printf(".%2d$\n", sum_cents);
		}
		else
		{
			printf(".0$\n");
		}[/b]
	}
	return 0;
}
The part in bold demonstrate the suggestions (which are consistent with each other) incorporated in the code (unsigned char, long long, separate variables for dollars and cents). But still that mf idiot judge is giving wrong answer. Sometimes I feel like to spank this mean judge.

Now what to do?

Re: 11340 - Newspaper

Posted: Tue Apr 20, 2010 9:43 am
by sohel
I have solved this problem a long time ago.
But from what i remember, you have to use 'unsigned char' for reading the 'paid characters'.

and for this part - printf(".%2d$\n", sum_cents);
shouldn't it be printf(".%02d$\n", sum_cents);

Re: 11340 - Newspaper

Posted: Wed Apr 21, 2010 5:19 am
by amishera
I made the format string to %02d and also made the unsigned character as mentioned here:

http://online-judge.uva.es/board/viewto ... 4&start=15

Code: Select all

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

using namespace std;

#define to_unsigned_char(x) if (x < 0) x += 256
int table[1000000];

int main()
{		
	char article[1000000];
	...
	unsigned char c;
        ...
	long long sum_dollar;
	int sum_cents;
        ...
	while (k-- > 0)
	{	
			c = cin.get(); //to skip whitespaces
			c = cin.get(); // get the char					
			t = c;            // convert it to int
			to_unsigned_char(t);		
			cin >> p;
			table[(int)t] = p;
	}
...
	while (m-- > 0)
	{
			scanf(" %[^\n]",article);
			for (k = 0;article[k];k++)
			{
				t = article[k];
				to_unsigned_char(t);
				sum_cents += table[t];
				if (sum_cents >= 100) {
					sum_cents -= 100;
					sum_dollar++;
				}
			}		
        }
...
	printf("%lld.%02d$\n", sum_dollar, sum_cents);[/u]
	

Still no right answer. I am not sure whether the reading the paragraph is the right procedure. Because it is reading a string using scanf(" %[^\n]"). Now you can't blame me that I didn't read the posts. I read the posts and made the necessary suggestions as well. But this anal retentive ACM judge is badgering me.

Re: 11340 - Newspaper

Posted: Wed Apr 21, 2010 5:40 am
by amishera
I made the final code which seems to be fool-proof because now both the table and the paragraph are read as unsigned char:

Code: Select all

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

using namespace std;

#define to_unsigned_char(x) if (x < 0) x += 256
int table[1000000];

int main()
{		
	unsigned char article[1000000];
	int n;
	int k;
	unsigned char c;
	int p;
	int m;
	int len;
	long long sum_dollar;
	int sum_cents;
	int i;
	int j;
	int t;
	scanf(" %d",&n);
	int first = 1;

	while (n-- > 0)
	{
		scanf(" %d",&k);
		for (j = 0;j < 512;j++)
		{
			table[j] = 0;
		}

		[b]while (k-- > 0)
		{	
			c = cin.get();
			c = cin.get();					
			t = c;
//			to_unsigned_char(t);			
			cin >> p;
			table[(int)t] = p;
		}[/b]

		scanf("%d",&m);

		sum_dollar = 0;
		sum_cents = 0;
		c = cin.get();
		while (m-- > 0)
		{
			[b]while ((c = cin.get()) != '\n')
			{				
				t = c;
	//			printf("c: %c\n", c);
//				to_unsigned_char(t);
				sum_cents += table[t];
				if (sum_cents >= 100) {
					sum_cents -= 100;
					sum_dollar++;
				}
			}[/b]
		}
		printf("%lld.%02d$\n", sum_dollar, sum_cents);
//		if (sum_cents > 0)
//		{
//			printf(".%02d$\n", sum_cents);
//		}
//		else
//		{
//			printf(".0$\n");
//		}
	}
	return 0;
}
Still the same old thing. I am totally pissed off with this ahole judge.

Re: 11340 - Newspaper

Posted: Wed Apr 21, 2010 1:11 pm
by sohel
Try avoiding scanf() altogether for this problem.
Use gets() and sscanf().

And btw, this online-judge aint retarded. It just compiles your program and runs it with the judge data - then compares your output with that of the judge answer file.

Re: 11340 - Newspaper

Posted: Thu Apr 22, 2010 1:08 am
by amishera
I am not sure why scanf wouldn't work but sscanf would work. Because the format strings are the same for both. The problem seems to boil down to how to input an unsigned char. So I resorted to the "cin" as suggested in previous posts for the problemetic parts (character reading). But still no change of my plight.

Re: 11340 - Newspaper

Posted: Fri Nov 12, 2010 2:53 pm
by faiem
:D
In this problem must use "unsigned char"...Or u will get WA...again and again just like me.
:oops:

Re: 11340 - Newspaper

Posted: Mon Nov 22, 2010 12:46 am
by JeanBez
I used string, map and unsiged char only in the first part while reading letter and the coast and got AC.
:D

Re: 11340 - Newspaper

Posted: Wed Jun 15, 2011 7:34 pm
by live_lie
thank you all...i have an AC now thanks a lot.

Re: 11340 - Newspaper

Posted: Thu Nov 03, 2011 1:57 pm
by Garfield
Always wanted to have AC. Thank you, your help is very much spy phone appreciated!

Re: 11340 - Newspaper

Posted: Mon Nov 07, 2011 11:47 am
by shakil ahmed
why WA???? plz help me............... :(

#include <stdio.h>
#include <string.h>
char str[10100];
int main()
{
int t,k;
scanf("%d",&t);
getchar();
for(k=1;k<=t;k++)
{
int a[260]={0},j,d,m,i,n;
char ch;
scanf("%d",&m);
getchar();
for(i=1;i<=m;i++)
{
scanf("%c %d",&ch,&d);
getchar();
a[ch]=d;
}
scanf("%d",&n);
getchar();
long long int sum=0,var=0;
for(i=1;i<=n;i++)
{
gets(str);
int len=strlen(str);
for(j=0;j<len;j++)
if(a[str[j]]>0)
sum+=a[str[j]];
if(sum>=100)
{
sum=0;
var++;
}

}
printf("%lld\.%lld$\n",var,sum);
}
return 0;
}