题目1032:ZOJ

题目描述:
读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。
输入:
题目包含多组用例,每组用例占一行,包含ZOJ三个字符,当输入“E”时表示输入结束。
1<=length<=100。
输出:
对于每组输入,请输出一行,表示按照要求处理后的字符串。
具体可见样例。
样例输入:
ZZOOOJJJ
ZZZZOOOOOJJJ
ZOOOJJ
E
样例输出:
ZOJZOJOJ
ZOJZOJZOJZOO

ZOJOJO

#include<iostream>
  #include<string>
  using namespace std;

  int main() {
    string str;
    while (cin >> str && str != "E") {
      int count[3] = {0};
      int k = str.length();
      for (int i = 0; i < k; i++) {
        if (str[i] == 'Z')
          count[0]++;
        else if (str[i] == 'O')
          count[1]++;
        else if (str[i] == 'J')
          count[2]++;
      }
    }
    int max = count[0];
    for (int i = 1; i < 3; i++) {
      if (max > count[i]) {
        max = count[i];
      }
    }
    for (int i = 0; i < max; i++) {
      if (count[0]-- > 0)
        cout << 'Z';
      if (count[1]-- > 0)
        cout << 'O';
      if (count[2]-- > 0)
        cout << 'J';
    }
    cout << endl;
    return 0;
  }








评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值