406 - Prime Cuts

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

mukeshtiwari
Learning poster
Posts: 63
Joined: Tue Mar 07, 2006 6:51 pm
Location: india
thnkx again ...

hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

406

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");
}
}``````

Samiul
New poster
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm
You should not print any "\n" on the first line and there will be two "\n" at the end.

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am

WA 406!Help..

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;
}``````

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
Use are asked to use existing thread.

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am
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;
}``````

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am
help...

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: 406 - Prime Cuts

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.

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: 406 - Prime Cuts

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.

maruf
New poster
Posts: 17
Joined: Sat May 24, 2008 6:00 pm

Re: 406 - Prime Cuts

cut.....acc
Last edited by maruf on Sun Aug 03, 2008 1:31 pm, edited 1 time in total.
lives for eternity......

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

Re: 406 - Prime Cuts

Maybe this is the why..
Each line of output should be followed by a blank line

maruf
New poster
Posts: 17
Joined: Sat May 24, 2008 6:00 pm

Re: 406 - Prime Cuts

these are some output...where should i print "\n"??
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
lives for eternity......

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

Re: 406 - Prime Cuts

You should print '\n' after the last case..

rhsumon
New poster
Posts: 48
Joined: Wed Aug 23, 2006 12:29 pm
Location: Dhaka

Re: 406 - Prime Cuts

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 ...........
``````
Last edited by rhsumon on Sat Aug 16, 2008 9:02 pm, edited 1 time in total.

rajib_sust
New poster
Posts: 16
Joined: Sun Mar 02, 2008 10:34 am
Location: SUST , Sylhet, Bangladesh

reply on :: 406 - Prime Cuts

try to read the problem carefully it is simple explain
it is easy also

Rajib, sust
life is beautiful like coding