zoukankan      html  css  js  c++  java
  • 荷兰国旗问题

    荷兰国旗问题

    描述 

    荷兰国旗有三横条块构成,自上到下的三条块颜色依次为红、白、蓝。现有若干由红、白、蓝三种颜色的条块序列,要将它们重新排列使所有相同颜色的条块在一起。本问题要求将所有红色的条块放最左边、所有白色的条块放中间、所有蓝色的条块放最右边。
     
    输入
    第1行是一个正整数n(n<100),表示有n组测试数据。接下来有n行,每行有若干个由R,W,B三种字符构成的字符串序列,其中R,W和B分别表示红、白、蓝三种颜色的条块,每行最多有1000个字符。
    输出
    对输入中每行上由R,W,B三种字符构成的字符串序列,将它们重新排列使所有相同颜色的条块在一起,满足前述要求。
    样例输入
    3
    BBRRWBWRRR
    RRRWWRWRB
    RBRW 
    
    样例输出
    RRRRRWWBBB
    RRRRRWWWB
    RRWB 

     
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    int n;
    string str;
    int c1,c2,c3;
    cin>>n;
    while(n--){
        cin>>str;
        c1=0,c2=0,c3=0;
        int m = str.size();
        for(int i=0;i<m;i++)
        {
            if(str[i]=='B')
                c1 ++;
            if(str[i]=='W')
                c2 ++;
            if(str[i]=='R')
                c3 ++;
        }
        for(int i=0;i<c3;i++)
            cout<<'R';
        for(int i=0;i<c2;i++)
            cout<<'W';
        for(int i=0;i<c1;i++)
            cout<<'B';
        cout<<endl;
    }
    
    
    return 0;
    }        
  • 相关阅读:
    python模块--time模块
    python模块--如何相互调用自己写的模块
    Animating Views Using Scenes and Transitions
    fragment 切换
    android textview 设置text 字体
    android intent 5.1
    android EditView ime
    animation of android (4)
    animation of android (3)
    animation of android (2)
  • 原文地址:https://www.cnblogs.com/imwtr/p/4069555.html
Copyright © 2011-2022 走看看