首页 > 编程知识 正文

哈理工 张淑丽,哈理工程序设计竞赛

时间:2023-05-04 05:33:52 阅读:18302 作者:3253

关于水的问题,我做了三个小时。

标题的意思是,在只包含“x”和“y”字符的字符串中,将“xy”对全部(不论前后顺序)去除,“x”和“y”之间无论有没有字符,只要对齐就去除

从一开始题意就搞错了,做了很长时间,后来注意到,这是不是在堆栈上做完的问题。

想法:首先用一个数组保存字符串,然后将字符串从头到尾填充到堆栈中。 如果堆栈顶部元素的字符与字符串中的字符不同,则挤出堆栈顶部元素。 同时字符串中的那个字符也不会塞进堆栈中,最后把堆栈中的元素全部输出就可以了。

代码如下。

# include iostream # include cstring # includecstdlib # include stack # define maxn 100006 usingnamespacestd; char str[maxn]={}; int main () while(cinstr ) ) {堆叠char book; intlen=strlen(str; for(intI=0; i len; I () if ) book.empty ) ) book.push ) str[I]; }else{if(book.top(====str[i] ) book.push ) str[I] ); (else ) book.pop ); } } } while (! book.empty () ) { cout book.top ); book.pop (; } cout endl; 短信(str,0,sizeof ) str ); }错误代码:

# include cstdio # includecstdlib # include cstring # include stack # define maxn 100006 usingnamespacestd; char str[maxn]={},rec[maxn]={}; int main () { char ch; int cnt=0; wile((Scanf('%s ',str ) ) {堆栈字符簿; intlen=strlen(str; if(len==1) printf('%sn ',str ); 继续; }for(intI=0; i len; I ) if(str[I]=='x'str[I1]=='y ' ) I; }else{book.push(str[I]; //printf('%c ',str[i] ); }//printf () ) (n ); int flag=1; while(flag==1) { int cnt=1; rec[1]=book.top (; book.pop (; flag=0; printf('%c ',rec[1] ); while (! book.empty () ) if ) rec[CNT]!=book.top () ) flag=1; if(rec[CNT]=='x'book.top )=='y ' ) { rec[cnt]=0; book.pop (; (else ) CNT; rec[cnt]=book.top (; printf('%c ',rec[cnt] ); (if (! book.empty () ) book.pop ); }printf((n*******(n ) ); //printf('%dn ',book.empty ) ); if (! 标志) break; intlen=Strlen(rec1; printf(%d(n ),len ); for(intI=1; i=len; I ) (printf )、rec[i]; book.push(rec[I]; rec[i]=0; }printf((n#######(n ) ); }printf('%sn ',rec 1); 短信(str,0,sizeof ) str ); 短信(rec,0,sizeof ) rec ); }}/**xyyxxx*/

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。