fndlu 2016-01-30 04:37 采纳率: 100%
浏览 1578
已采纳

c++的祖玛问题,用的是链表,测试时有时会停止运行,不能连消,源码如下,求大神指错;

#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;
}

  • 写回答

1条回答

  • threenewbee 2016-01-31 00:32
    关注

    看了你的程序半天没明白你怎么做。注释也没有,连怎么运行都没说。

    不过网上有个类似的代码:
    http://download.csdn.net/detail/yeargold/9211641

    (如果没有积分,采纳了本回答,我可以帮你下载)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态