Re: problem 406
Posted: Tue Jul 27, 2010 12:32 pm
why can't you edit your post?shantanu18 wrote:i can't edit my post so can not delete my code
why can't you edit your post?shantanu18 wrote:i can't edit my post so can not delete my code
Code: Select all
#include<stdio.h>
#include<math.h>
int main()
{
int sr[169];int count=2;int flag=1;int i,b;int n,m;int count1=0;int even,odd;int even1,odd1;int even2,odd2;int even3,odd3;
sr[0]=1;sr[1]=2;sr[2]=3;sr[3]=5;
for( i=5;i<=1000;i+=2)
{
int s=(int)sqrt(i);
for(int j=2;j<=s;j++)
{
if(i%j!=0)flag=0;
else
{
flag=1;
break;
}
}
if(flag==0)
{
count=count+1;
sr[count]=i;
}
}
while((scanf("%d %d",&n,&m))==2)
{
count1=0;
int k=0;
while(sr[k]<=n)
{
count1=count1+1;
k++;
}
printf(" %d %d:",n,m);
if(count1%2==0)
{
even=m*2;
even1=(count1-even);
even2=even1/2;
even3=even2+even;
if(even>=count1)
{
int k1=0;
while(sr[k1]<=n)
{
printf(" %d",sr[k1]);
k1++;
}
}
else if(even<count1)
{
for(int i=even2;i<even3;i++)
printf(" %d",sr[i]);
}
}
else if(count1%2!=0)
{
odd=(m*2)-1;
odd1=(count1-odd);
odd2=odd1/2;
odd3=odd2+odd;
if(odd>=count1)
{
int k1=0;
while(sr[k1]<=n)
{
printf(" %d",sr[k1]);
k1++;
}
}
else if(odd<count1)
{
for(int i=odd2;i<odd3;i++)
printf(" %d",sr[i]);
}
}
printf("\n\n");
}
return 0;
}
Code: Select all
Accepte...d
Code: Select all
Accepted
But i don't know how to remove the space before any digit when the output is too long and will be printed in newline.Can anyone please give any idea?lnr wrote:Try this case:
Input:Output:Code: Select all
100 51 1000 1000 954 1
Hope it helps.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
Code: Select all
Accepted :D
Code: Select all
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cctype>
#include<string>
#include<cmath>
using namespace std;
bool isprime(int t){
if(t==2||t==3)
return true;
else if(t%2==0||t%3==0)
return false;
int s=sqrt(t);
for(int i=2;i<=s;i++)
if(t%i==0)
return false;
return true;
}
int main(){
int a,b;
int prime[1000],c=1;
prime[0]=1;
for(int i=2;i<1001;i++){
if(isprime(i)){
prime[c]=i;
c++;
}
}
while(cin>>a>>b){
for(c=0;c<1000;c++)
if(prime[c]>a)
break;
cout<<a<<" "<<b<<":";
if(c%2){
if(2*b-1>c)
for(int i=0;prime[i]<=a;i++)
cout<<" "<<prime[i];
else
for(int i=c/2-b+1;i<=c/2+b-1;i++)
cout<<" "<<prime[i];
}else{
if(2*b>c)
for(int i=0;prime[i]<=a;i++)
cout<<" "<<prime[i];
else
for(int i=c/2-b;i<=c/2+b-1;i++)
cout<<" "<<prime[i];
}
cout<<"\n\n";
}
return 0;
}
Code: Select all
1000 1000
Code: Select all
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