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。
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
- hdu3783&&九度1032 ZOJ(字符串或栈)
- HDU3783:ZOJ
- hdu3783 ZOJ (水)
- HDU3783
- HDU3783
- zoj问题 九度1006&&hdu3788(字符串)
- 九度OJ 题目1032:ZOJ
- 九度OnlineJudge之1032:ZOJ
- 九度论坛--上机题1032:ZOJ
- 九度OJ—题目1032:ZOJ
- 题目 1032 ZOJ 九度OnlineJudge
- 九度OJ 题目1032:ZOJ
- C ZOJ(九度OJ 1032)
- 九度OJ-题目1032 ZOJ
- 九度1006:ZOJ问题
- 九度OJ 1032:ZOJ (基础题)
- 九度1510(字符串)
- 1006 ZOJ问题 九度Online Judge
- https://leetcode.com/problems/set-matrix-zeroes/
- hdu-1241 Oil Deposits (and) nyoj-27 水池数目
- JVM的GC机制<一>新生代GC
- 谈谈我对攻读计算机研究生的看法
- CUDA存储器模型
- hdu3783&&九度1032 ZOJ(字符串或栈)
- java全排列算法实现 --- 小例子实现
- "BestCoder"杯中国大学生程序设计冠军赛 HDU 5221 Occupation
- BZOJ 4029: [HEOI2015]定价
- c#调用DLL
- nginx+php+mysql服务器搭建
- 循环单链表的基本操作
- 微商如何购买微信小号推广?
- 两个链表的第一个公共结点