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

int main() {
    string letter, word;
    while (cin >> letter >> word) {
        // 将字母和单词都转换为小写字母
        for (int i = 0; i < letter.length(); i++) {
            letter[i] = tolower(letter[i]);
        }
        for (int i = 0; i < word.length(); i++) {
            word[i] = tolower(word[i]);
        }
        
        int count = 0;
        for (int i = 0; i < word.length(); i++) {
            if (word[i] == letter[0]) {
                count++;
            }
        }
        
        double probability = (double)count / word.length();
        cout << fixed << setprecision(5) << probability << endl;
    }
    
    return 0;
}
描述小明最近对概率问题很感兴趣。一天小明和小红一起玩一个概率游戏首先小明给出一个字母和一个单词然后由小红计算这个字母在这个单词中出现的概率。字母不区分大小写。例如给定的字母是a单词是apple那么概率是020000。输入描述输入包含多组测试数据。每组数据包含一个字母和一个单词。单词的长度不超过200。输出描述对于每一个输入输出对应的概率结果保留5位小数。希望能用c++编写代码不要讲解。输入:a a

原文地址: https://www.cveoy.top/t/topic/jagK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录