C++ 彰雲嘉區複賽106-2 跑長編碼與資料壓縮

字串基本應用

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

string num[10]={"000","001","010","011","100","101","110","111"};

string print(int sum,char bit){
    string re(1,bit);
    re+=num[sum];
    re+=" ";
    return re;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);

    int iptNum=0;
    cin>>iptNum;

    string bit,out;
    getline(cin,bit);

    while(iptNum--){
        out="";
        getline(cin,bit);
        int j,sum=1;
        long long rate=0;
        for(j=1;j<bit.size();j++){
            if(bit[j]!='0' && bit[j]!='1'){
                cout<<"-1\n";
                j++;
                break;
            }

            if(bit[j-1]==bit[j] && sum<7){
                sum++;
            }else{
                out+=print(sum,bit[j-1]);
                rate+=4;
                sum=1;
            }
        }
        out+=print(sum,bit[j-1]);
        rate+=4;
        rate*=1000;
        rate/=bit.size();
        rate=(rate+5)/10;

        if(bit[j-1]=='0' || bit[j-1]=='1'){
            cout<<out<<rate<<"\n";
        }
    }
    return 0;
}

出處:GreenJudge

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *