#include <stdio.h>
#include <stdlib.h>
struct data
{
char go[10001];
int min;
};
typedef struct data way;
int count=0,x=0;
char path[10001];
void dfs(int c,int v,int flag,int times,way* ptr)
{
if(v==x)
{
for(int a=0;a<times-1;a++)
(ptr+count)->go[a]=path[a];
if(flag) (ptr+count)->go[times-1]=1;
else (ptr+count)->go[times-1]=0;
(ptr+count)->min=times;
count++;
times--;
}
if(v<x)
{
if(flag)//上一個為c
{
path[times-1]=1;
dfs(c,c+v,0,times+1,ptr);
}
else //上一個為v
{
path[times-1]=0;
dfs(v,v,1,times+1,ptr);//c
dfs(c,c+v,0,times+1,ptr);//v
}
}
}
int main(void)
{
int i,j,n,k=0,min_way;
way *ptr=(way*)malloc(sizeof(way)*10001);
int *store=(int*)malloc(sizeof(int)*6001);
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<count;i++) // memset((ptr+i)->go,0,10001);
for(j=0;j<10001;j++)
(ptr+i)->go[j]=0;
for(j=0;j<10001;j++)
path[j]=0;
for(i=0;i<k;i++)
*(store+i)=0;
x=n;
count=0;
dfs(1,1,1,1,ptr);//c=1 , v=0
min_way=10000;
for(i=0;i<count;i++)//find min
if( (ptr+i)->min < min_way )
min_way=(ptr+i)->min;
k=0;
for(i=0;i<count;i++)
if((ptr+i)->min==min_way)
{
*(store+k)=i;
k++;
}
printf("min:%d\n",min_way);
printf("way:%d\n",k);
for(i=0;i<k;i++)//print
{
printf("Ctrl ");
for(j=0;j<min_way-1;j++)
if( ((ptr+*(store+i))->go[j]) ==1) printf("C + ");
else printf("V + ");
j++;
if( ((ptr+*(store+i))->go[j]) ==1) printf("C");
else printf("V");
printf("\n");
}
}
free(ptr);
free(store);
return 0;
}
我在我的電腦上跑都沒有問題,可是一上傳就RE...
應該是malloc的宣告錯誤吧?我真的不太會用@@
請各位高手指點一下謝謝!
P.S.malloc能宣告在main外面嗎??(就是配置成全域的意思