题目

我们认为,假如一句话里含有 2 ,那么这句话很二次元。
同时,QQ 群里的二次元浓度由二次元话的数量的占比决定。
现在 Sabit 给你看了一眼群里的聊天记录,希望可以知道群里的二次元浓度是多少。

解题思路

使用 string 库中的 getline() 函数读取一行字符串。
使用变量 cnt 记录二次元话的数量。

C++代码

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

int main(){
    int T, n;
    cin >> T;
    string words;
    while(T--){
        int cnt = 0;
        cin >> n;
        getchar();    // 换行符
        for(int i=0; i<n; ++i){
            getline(cin, words);
            for(auto c : words){
                if(c=='2'){
                    ++cnt;
                    break;
                }
            }
        }
        double ans = 1.0*cnt/n;
        cout << fixed << setprecision(10) << ans << endl;
    }
    return 0;
}