488 - Triangle Wave

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

Moderator: Board moderators

User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

488 - accepted, but PE

Post by tnaires »

Hi everybody,

Here is my code for problem 488:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n_cases, *amp, *freq, i, j, k, l;
	
	scanf("%d", &n_cases);
	printf("\n");
	
	amp = (int *) calloc(n_cases, sizeof(int));
	freq = (int *) calloc(n_cases, sizeof(int));
	
	for (i = 0; i < n_cases; i++)
	{
		scanf("%d%d", &amp[i], &freq[i]);
		printf("\n");
	}
	
	for (i = 0; i < n_cases; i++)
	{
		for (j = 0; j < freq[i]; j++)
		{
			for (k = 1; k <= amp[i]; k++)
			{
				for (l = 0; l < k; l++)
					printf("%d", k);
				
				printf("\n");
			}
			
			for (k = amp[i] - 1; k >= 1; k--)
			{
				for (l = 0; l < k; l++)
					printf("%d", k);
				
				printf("\n");
			}
			
			if ((j < freq[i] - 1) || (i < n_cases - 1))
				printf("\n");
		}
	}
	
	free(amp);
	free(freq);
	return 0;
}
It works, but with Presentation Error... Somebody has some suggestion? Thanks a lot, and sorry about my english 8)
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

You are first taking all the inputs and then give all the outputs. This is unnecessary and in some problems you will not be able to do so because of the memory contraints..
for (i = 0; i < n_cases; i++)
{
scanf("%d%d", &amp, &freq);
printf("\n");
}


The rason for PE is, as you are taking input, you are printing a newline, which clearly cuases extra newlines to appear in your outpur that are not present in judges output.
I hope this helps.
User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

Post by tnaires »

Hi, shamim. Thanks for attention.
Your tip didn't work... I'm still with a PE. You told me things that left me confused:
shamim wrote:You are first taking all the inputs and then give all the outputs. This is unnecessary and in some problems you will not be able to do so because of the memory contraints..
But in a multiple input problem, I get all the inputs first, then I show the results. Or am I wrong?
shamin wrote:The rason for PE is, as you are taking input, you are printing a newline, which clearly cuases extra newlines to appear in your outpur that are not present in judges output.
But the problem says that "there is also a blank line between two consecutive inputs".
I hope that you can help me. Thanks again, and success for you :D
User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

Post by tnaires »

Hello again.
shamin, I finally understood what you said. I saw the output of the program:

Code: Select all

(blank line)
(blank line)
(blank line)
(blank line)
(blank line)
results
I refreshed my code, using your tips:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int num_casos, amp, freq, i, j, k, l;
	
	scanf("%d", &num_casos);
	
	for (i = 0; i < num_casos; i++)
	{
		scanf("%d%d", &amp, &freq);
		
		for (j = 0; j < freq; j++)
		{
			for (k = 1; k <= amp; k++)
			{
				for (l = 0; l < k; l++)
				{
					printf("%d", k);
				}
				
				printf("\n");
			}
			
			for (k = amp - 1; k >= 1; k--)
			{
				for (l = 0; l < k; l++)
				{
					printf("%d", k);
				}
				
				printf("\n");
			}
			
			if ((j < freq - 1) || (i < num_casos - 1))
			{
				printf("\n");
			}
		}
	}
	
	return 0;
}
But I'm still having PE!! I'm almost giving up... Does anyone got Accepted in this problem?! :-?
Thanks a lot. 8)
Tarso Nunes Aires
Natal/RN - Brazil
WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

p488

Post by WR »

Print a blank line even after the last output.

input

Code: Select all

2

2
3

3
2
output:

Code: Select all

1
22
1
_
1
22
1
_
1
22
1
_
1
22
333
22
1
_
1
22
333
22
1
_
_ stands for a blank line
User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

Re: p488

Post by tnaires »

WR wrote:Print a blank line even after the last output.
Thanks man! I got AC!!
But I really don't understand it...
The problem says that "There is a blank line after each separate waveform, excluding the last one."
I'd never know of this...
Tarso Nunes Aires
Natal/RN - Brazil
Rony
New poster
Posts: 16
Joined: Wed Jun 30, 2004 6:46 am
Location: Dhaka
Contact:

WA 488

Post by Rony »

Hi,
Can any one check my code? I am getting WA continuousely. I think there is something silly mistake.Following is my code.

Code is removed after getting AC. If any one need any help mail me.

Regards
Rony.
Last edited by Rony on Tue Mar 29, 2005 9:17 am, edited 1 time in total.
Raj Ariyan
Learning poster
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

Post by Raj Ariyan »

Hi,
This is a multiple input problem right ? And u always print new line after each output. Then what happend with the last output ??? The will be print one extra new line after last output, that should not be happened. So replace by this -->

Must be at the last line,

if(tcase) // last output tcase=0,so false & no newline
puts("");

another is -->

if(f!=1) // When f is 1 in the loop then dont print extra line
puts("");

or u can use it like-->

if(f!=freq) // at the end
puts("");

Second mistake--> U always take input by gets(), but why u need it. There is no character input then sscanf is not necessary. Just replace it by scanf and simply take input. If u use gets then u might get wrong input something like blank line.

Hope it helps.
Some Love Stories Live Forever ....
Rony
New poster
Posts: 16
Joined: Wed Jun 30, 2004 6:46 am
Location: Dhaka
Contact:

Re: WA 488

Post by Rony »

Hi Raj Ariyan,
I got Ac just replacing ges() and ssanf() function by scanf() function ,nothing else .Not checking also last line \n.
Thanks.

Regards.
Rony
[Depressed]
murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

WA for 488 (Triangle wave)!! can u believe ??pls help me ...

Post by murkho »

<code>

Can u believe I am getting WA for 488(triangle wave)..Yea, it is possible for me. Help me please to find out the reason..Pls..
/*BEGIN_OF_THE_SOURCE_CODE*/

#include<stdio.h>

int main()
{
long int i,j,fre,amp;
long int rep = 0,count=0;
while( scanf("%ld %ld",&amp,&fre)==2)
{
rep=0;
while(rep <fre)
{
if(count !=0)
printf("\n");
for ( i =1;i<=amp;i++)
{
for(j=0;j<i;j++)
printf("%ld",i);
printf("\n");
}

for(i = i-2;i>0;i--)
{
for(j = 0;j<i;j++)
printf("%ld",i);
printf("\n");
}
//printf("\n");
rep++;
count++;
}
}
return 0;
}
/*END_OF_THE_SOURCE_CODE*/
[quote]
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

Your code doesn't even work for the sample input given at http://acm.uva.es/p/v4/488.html

I think you missed that it's a multiple-input problem. The first integer gives the number of test cases.
murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

Thanks Mf for your reply

Post by murkho »

Thanks for reply . Actually my problem set with me, nothing said about test case. NOw i got the point. So, thank you.




mf wrote:Your code doesn't even work for the sample input given at http://acm.uva.es/p/v4/488.html

I think you missed that it's a multiple-input problem. The first integer gives the number of test cases.
asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

Plz Help Me On 488.I've got WR

Post by asif_rahman0 »

Here is my code.I can't understand why got WR.Plz help me and also give me some inputs and outputs for this problem

#include<stdio.h>

void main()
{
int amp,freq,j,k,l;
while(scanf("%d %d",&amp,&freq)==2)
{

for (j = 0; j < freq; j++)
{
for (k = 1; k <= amp; k++)
{
for (l = 1; l <= k; l++)
{
printf("%d", k);
}

printf("\n");
}

for (k = amp - 1; k >= 1; k--)
{
for (l = 1; l <= k; l++)
{
printf("%d", k);
}

printf("\n");
}


printf("\n");
}
}
}
Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman »

What is WR? :o
You should never take more than you give in the circle of life.
asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

Sorry

Post by asif_rahman0 »

Sorry I've got WA(Wrong Answer).That's my typing mistakes.Rally sorry for that.Plz help me. :(
Post Reply

Return to “Volume 4 (400-499)”