406 - Prime Cuts
Moderator: Board moderators
-
- Learning poster
- Posts: 63
- Joined: Tue Mar 07, 2006 6:51 pm
- Location: india
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");
}
}
-
- 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;
}
-
- 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;
}
Re: 406 - Prime Cuts
I also don't understand why your code is getting WA?
Try using this.Chang your code here.Code: Select all
int limit = sqrt (num) + 1; To int limit = sqrt (num);
Re: 406 - Prime Cuts
Try this case:
Input:
Output:
Hope it helps.
Input:
Code: Select all
100 51
1000 1000
954 1
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
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......
Re: 406 - Prime Cuts
Maybe this is the why..
Each line of output should be followed by a blank line
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......
Re: 406 - Prime Cuts
You should print '\n' after the last case.. 

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:
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.
-
- 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
it is easy also
Rajib, sust
life is beautiful like coding