hi minhaz sorry for late reply. here is your accepted code .....you are printing a extra space after each line and at last of the input a space and a new line charactor . hope it helps ..all the best for programming #include<stdio.h> #include<string> using namespace std; int res[20],taken[20],c_len; bool p[35]; void prime(void); void init(void); void dp(int);
int main(){ int cases=0; /* freopen("c:pr.txt","w",stdout);*/ prime(); /*/ for(c_len=1;c_len<17;c_len++){*/ while(scanf("%d",&c_len)==1){ if(cases>0)puts(""); cases++; init(); res[0]=1;taken[1]=1; printf("Case %d:\n",cases); dp(1); //puts(""); } } void dp(int n){ int i; if(n==c_len){ if(p[res[c_len-1]+1]==1){ for(i=0;i<c_len-1;i++){ printf("%d ",res[i]); } printf("%d\n",res[i]); //puts(""); } } for(i=2;i<=c_len;i++){ if(taken[i]==0 && p[res[n-1]+i]==1){ res[n]=i; taken[i]=1; dp(n+1); taken[i]=0; } } } void init(void){ memset(&res[0],0,18); memset(&taken[0],0,18); } void prime(void){ for(int i=1;i<32,p[i]=false ;i++); p[2]=p[3]=p[5]=p[7]=p[11]=true; p[13]=p[17]=p[19]=p[23]=p[29]=p[31]=true; } On Jun 6, 9:35 pm, "Minhaz Ul-Alam" <[EMAIL PROTECTED]> wrote: > hey mukesh, > this code got 3 P.E. I am not sure why. Is it helpful? > > #include<stdio.h> > #include<string> > using namespace std; > int res[20],taken[20],c_len; bool p[35]; > void prime(void); > void init(void); > void dp(int); > > void main(void){ > int cases=0; > /* freopen("c:pr.txt","w",stdout);*/ > prime(); > /*/ for(c_len=1;c_len<17;c_len++){*/ > while(scanf("%d",&c_len)==1){ > // if(cases>0)puts(""); > cases++; init(); > res[0]=1;taken[1]=1; > printf("Case %d:\n",cases); > dp(1); > puts(""); > }} > > void dp(int n){ > int i; > if(n==c_len){ > if(p[res[c_len-1]+1]==1){ > for(i=0;i<c_len;i++){ > printf("%d ",res[i]); > } > puts(""); > } > } > for(i=2;i<=c_len;i++){ > if(taken[i]==0 && p[res[n-1]+i]==1){ > res[n]=i; > taken[i]=1; > dp(n+1); > taken[i]=0; > } > }} > > void init(void){ > memset(&res[0],0,18); > memset(&taken[0],0,18);} > > void prime(void){ > for(int i=1;i<32,p[i]=false ;i++); > p[2]=p[3]=p[5]=p[7]=p[11]=true; > p[13]=p[17]=p[19]=p[23]=p[29]=p[31]=true; > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~----------~----~----~----~------~----~------~--~---