i am trying the following problem.
http://acm.uva.es/p/v7/729.html
my code is as below : (it is giving a wrong answer on submission !)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct strin
{
char st[20];
struct strin * next;
};
typedef struct strin stri;
void func (int k , int m , int r , int n , char *str , int count ,stri
**first);
void add(char *str,stri **first);
void prin(stri **first);
int main()
{
stri *first=NULL;
int i,count=0;
int noi,ij;
int r,n;
char *str;
scanf("%d",&noi);
for(ij=0;ij<noi;ij++)
{
scanf("%d %d",&n,&r);
str=(char *)malloc(sizeof(char)*(n+1));
for(i=0;i<n;i++)
{
str[i]='0';
}
str[n]='\0';
func(0,n-r,r,n,str,count,&first);
prin(&first);
printf("\n");
first=NULL;
}
system("pause");
return 0;
}
void func (int k , int m , int r , int n , char *str,int count,stri
**first)
{
int i,j ;
if(count==(r-1))
{
for(j=k;j<n;j++)
{
str[j]='1';
if(j!=k)
str[j-1]='0';
add(str,first);
}
str[j-1]= '0';
return ;
}
else
{
for(i=k;i<=m;i++)
{
str[i]='1';
if(i!=k)str[i-1]='0';
func(i+1,m+1,r,n,str,count+1,first);
}
}
}
void add(char *str,stri **first)
{
static stri *p=NULL;
if((*first)==NULL)
{
*first=(stri *)malloc(sizeof(stri));
strcpy((*first)->st,str);
(*first)->next=NULL;
p=*first;
}
else
{
(p->next)=(stri *)malloc(sizeof(stri));
p=p->next;
strcpy(p->st,str);
p->next=NULL;
}
}
void prin(stri **first)
{
if((*first)==NULL)
return;
else
{
prin(&((*first)->next));
printf("%s\n",(*first)->st);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---