#include
#include
struct zm{
char c;
struct zm *pre;
struct zm *next;
};
struct zm *head,*p,*t;
void reset(int n,char co) //插入珠子
{
int i;
p=head;
for(i=1;i<=n;i++)p=p->next;
t=(struct zm *)malloc(sizeof(struct zm));
t->c=co;
t->next=p->next;
t->pre=p;
p->next->pre=t;
p->next=t;
}
void printzm() //输出珠串
{
p=head->next;
if(p->c=='+'){printf("-\n");return;}
while(p->c!='+'){printf("%c",p->c);p=p->next;}
printf("\n");
}
void check(int k) //消除珠子
{
int i,lc,rc;
struct zm *l,*r;
p=head->next;
for(i=1;i<=k;i++)p=p->next;
l=p->pre;r=p->next;
lc=0;rc=0;
while(l->c==p->c)
{
lc++;
l=l->pre;
}
while(r->c==p->c)
{
rc++;
r=r->next;
}
if(lc+rc+1>=3){r->pre=l;l->next=r;}
if(l->c==r->c){k-=lc;check(k);}
}
int main()
{
p=(struct zm *)malloc(sizeof(struct zm)); //链表'+'.....'+'
p->c='+';
head=p;
t=(struct zm *)malloc(sizeof(struct zm));
t->c='+';
p->next=t;
t->pre=p;
t->next=NULL;
char tmp;
char s[5];
int cnt,n,i,k;
cnt=0;
scanf("%c",&tmp);
while(tmp!='\n') //放置
{
reset(cnt,tmp);
cnt++;
scanf("%c",&tmp);
}
scanf("%d",&n);
for(i=1;i<=n;i++) //输出
{
scanf("%d",&k);scanf("%s",s);
reset(k,s[0]);
check(k);
printzm();
}
system("pause");
return 0;
}
c++的祖玛问题,用的是链表,测试时有时会停止运行,不能连消,源码如下,求大神指错;
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2016-01-31 00:32关注
看了你的程序半天没明白你怎么做。注释也没有,连怎么运行都没说。
不过网上有个类似的代码:
http://download.csdn.net/detail/yeargold/9211641(如果没有积分,采纳了本回答,我可以帮你下载)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报