Run ID:113269
提交时间:2025-03-15 16:14:47
#include <iostream> #include <string> #include <cctype> // 用于tolower函数 #include <iomanip> // 用于设置输出格式 using namespace std; int main() { string input; while (getline(cin, input)) { if (input.empty()) break; // 如果输入为空,结束程序 // 分割输入的字母和单词 char letter = tolower(input[0]); // 将字母转换为小写 string word = input.substr(2); // 提取单词部分 transform(word.begin(), word.end(), word.begin(), ::tolower); // 将单词转换为小写 // 统计字母出现的次数 int count = 0; for (char c : word) { if (c == letter) { count++; } } // 计算概率 double probability = static_cast<double>(count) / word.length(); // 输出结果,保留5位小数 cout << fixed << setprecision(5) << probability << endl; } return 0; }