hdu3783&&九度1032 ZOJ(字符串或栈)

来源:互联网 发布:人工智能前沿技术 编辑:程序博客网 时间:2024/06/05 20:26

ZOJ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2086    Accepted Submission(s): 1440


Problem Description
读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。
 

Input
题目包含多组用例,每组用例占一行,包含ZOJ三个字符,当输入“E”时表示输入结束。
1<=length<=100。
 

Output
对于每组输入,请输出一行,表示按照要求处理后的字符串。
具体可见样例。
 

Sample Input
ZZOOOJJJZZZZOOOOOJJJZOOOJJE
 

Sample Output
ZOJZOJOJZOJZOJZOJZOOZOJOJO
 

Source

浙大计算机研究生复试上机考试-2009年  



//自己的思路是使用三个栈,分别放字符Z,O,J,然后再按Z,O,J的顺序出栈即可。

#include<stdio.h>#include<string.h>#include<stack>#include<algorithm>using namespace std;char str[105];stack<char>s1;stack<char>s2;stack<char>s3;int main(){while(scanf("%s",str)!=EOF){int len=strlen(str);while(!s1.empty()){s1.pop();}while(!s2.empty()){s2.pop();}while(!s3.empty()){s3.pop();}if(str[0]=='E'){break;}else{for(int i=0;i<len;i++){if(str[i]=='Z'){s1.push(str[i]);}else if(str[i]=='O'){s2.push(str[i]);}else if(str[i]=='J'){s3.push(str[i]);}}}while(!s1.empty()||!s2.empty()||!s3.empty()){/**char ch1=s1.top();printf("%c",ch1);s1.pop();char ch2=s2.top();printf("%c",ch2);s2.pop();char ch3=s3.top();printf("%c",ch3);s3.pop();*/if(!s1.empty()){char ch1=s1.top();printf("%c",ch1);s1.pop();}if(!s2.empty()){char ch2=s2.top();printf("%c",ch2);s2.pop();}if(!s3.empty()){char ch3=s3.top();printf("%c",ch3);s3.pop();}}printf("\n");}return 0;}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">//下面是别人的更好一点的思路,不用栈
</pre><pre name="code" class="cpp">
<pre name="code" class="cpp">#include<stdio.h>#include<string.h>char str[105];int main(){int z,o,j;int len;while(scanf("%s",str)!=EOF){z=o=j=0;len=strlen(str);if(str[0]=='E'){break;}else{for(int i=0;i<len;i++){if(str[i]=='Z')z++;else if(str[i]=='O')o++;else if(str[i]=='J')j++;}while(z||o||j){if(z){printf("Z");z--;}if(o){printf("O");o--;}if(j){printf("J");j--;}}}printf("\n");}return 0;}




0 0
原创粉丝点击