Page 8 of 13

Posted: Fri Jul 13, 2007 4:01 pm
by mukeshtiwari
thnkx again ...

406

Posted: Wed Dec 12, 2007 8:40 pm
by hridoy
Can any one please tell why I m getting WA in acm-406......

Code: Select all

#include<stdio.h>
#include<math.h>

int prime(int n)
{
	int i;
	if(n==2)
		return 2;
	if(n%2==0)
		return 0;
	for(i=3;i<=sqrt(n);i+=2)
		if(n%i==0)
			return 0;
	return n;
}

main()
{
	int i,j,a[1000],n,c,x,k,l;
	while(scanf("%d %d",&n,&c)!=EOF)
	{
		j=0;
		for(i=1;i<=n;i++)
		{
			x=prime(i);
			if(x!=0)
			{
				a[j]=i;
				j++;
			}
		}
		if(j%2==0)
		{
			if(j>=2*c)
			{
				k=(j-2*c)/2;
				l=k+2*c;
			}
			else
			{
				k=0;
				l=j;
			}
		}
		else
		{
			if(j>=(2*c-1))
			{
				k=(j-2*c+1)/2;
				l=k+2*c-1;
			}
			else
			{
				k=0;
				l=j;
			}
		}

		printf("\n%d %d:",n,c);
		for(i=k;i<l;i++)
			printf(" %d",a[i]);

		printf("\n");
	}
}

Posted: Fri Dec 21, 2007 11:29 am
by Samiul
You should not print any "\n" on the first line and there will be two "\n" at the end.

WA 406!Help..

Posted: Sat Feb 09, 2008 9:29 am
by AcmNightivy
i have searched all of the thread and tested all of the cases given..and all of them give the right answers..but still get WA..Help..Thanks..

Code: Select all

#include <iostream.h>
#include "math.h"
#include "string.h"

bool IfPrime (int num)
{
	if (num == 2 || num == 1)
		return true;
	else
		if (num % 2 == 0)
			return false;
	
	int limit = sqrt (num) + 1;

	for (int i = 3; i < limit; i = i + 2)
	{
		if (num % i == 0)
			return false;
	}
	return true;
}

int main ()
{
	int n, c, num, start;
	int i;
	int prime[1000];
	int primeNum;
	bool flag = true;

	while (cin>>n>>c)
	{
		if (flag == true)
			flag = false;
		else
			cout<<endl;

		primeNum = 0;
		for (i = 1; i <= n; i++)
			if (IfPrime (i))
				prime[primeNum++] = i;

		if (primeNum % 2 == 0)
			num = c * 2;
		else
			num = c * 2 - 1;

		if (num >= primeNum)
		{
			start = 0; 
			num = primeNum;
		}
		else
		{
			start = (primeNum - num) / 2;
		}

		cout<<n<<" "<<c<<":";
		for (i = 0; i < num; i++)
			cout<<" "<<prime[start + i];
		cout<<endl;
	}
	return 0;
}

Posted: Sat Feb 09, 2008 8:27 pm
by Jan
Use are asked to use existing thread.

Posted: Sun Feb 10, 2008 7:33 am
by AcmNightivy
i have searched all of the thread and tested all of the cases given..and all of them give the right answers..but still get WA..Help..Thanks..

Code: Select all

#include <iostream.h> 
#include "math.h" 
#include "string.h" 

bool IfPrime (int num) 
{ 
   if (num == 2 || num == 1) 
      return true; 
   else 
      if (num % 2 == 0) 
         return false; 
    
   int limit = sqrt (num) + 1; 

   for (int i = 3; i < limit; i = i + 2) 
   { 
      if (num % i == 0) 
         return false; 
   } 
   return true; 
} 

int main () 
{ 
   int n, c, num, start; 
   int i; 
   int prime[1000]; 
   int primeNum; 
   bool flag = true; 

   while (cin>>n>>c) 
   { 
      if (flag == true) 
         flag = false; 
      else 
         cout<<endl; 

      primeNum = 0; 
      for (i = 1; i <= n; i++) 
         if (IfPrime (i)) 
            prime[primeNum++] = i; 

      if (primeNum % 2 == 0) 
         num = c * 2; 
      else 
         num = c * 2 - 1; 

      if (num >= primeNum) 
      { 
         start = 0; 
         num = primeNum; 
      } 
      else 
      { 
         start = (primeNum - num) / 2; 
      } 

      cout<<n<<" "<<c<<":"; 
      for (i = 0; i < num; i++) 
         cout<<" "<<prime[start + i]; 
      cout<<endl; 
   } 
   return 0; 
}

Posted: Thu Feb 28, 2008 4:57 pm
by AcmNightivy
help...

Re: 406 - Prime Cuts

Posted: Mon Jul 07, 2008 5:54 pm
by lnr
I also don't understand why your code is getting WA?
Chang your code here.

Code: Select all

int limit = sqrt (num) + 1; 
To
int limit = sqrt (num); 
Try using this.

Re: 406 - Prime Cuts

Posted: Mon Jul 07, 2008 8:05 pm
by lnr
Try this case:
Input:

Code: Select all

100 51
1000 1000
954 1
Output:

Code: Select all

100 51: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

1000 1000: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

954 1: 419

Hope it helps.

Re: 406 - Prime Cuts

Posted: Sun Aug 03, 2008 8:04 am
by maruf
cut.....acc

Re: 406 - Prime Cuts

Posted: Sun Aug 03, 2008 10:16 am
by helloneo
Maybe this is the why..
Each line of output should be followed by a blank line

Re: 406 - Prime Cuts

Posted: Sun Aug 03, 2008 11:42 am
by maruf
these are some output...where should i print "\n"?? :roll:
100 100: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

100 50: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

100 51: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

1000 1000: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

1 1: 1

Re: 406 - Prime Cuts

Posted: Sun Aug 03, 2008 11:55 am
by helloneo
You should print '\n' after the last case.. :-)

Re: 406 - Prime Cuts

Posted: Sat Aug 16, 2008 8:07 pm
by rhsumon
Hello programmars

I mean my code i correct but WA
Where is the problem
Plz Help me
Here is my code:

Code: Select all

AC ...........

reply on :: 406 - Prime Cuts

Posted: Sat Aug 16, 2008 8:22 pm
by rajib_sust
try to read the problem carefully it is simple explain
it is easy also

Rajib, sust