Run ID:109094
提交时间:2025-01-26 16:27:02
#include <iostream> #include <string> #include <cctype> // 用于字符操作 #include <iomanip> // 用于控制输出格式 using namespace std; int main() { int T; cin >> T; // 读取测试数据组数 cin.ignore(); // 忽略换行符,确保下一次读取时从第一组数据开始 while (T--) { char letter; string word; cin >> letter >> word; // 读取字母和单词 // 将字母和单词都转换为小写 letter = tolower(letter); for (char &c : word) { c = tolower(c); } // 计算字母在单词中出现的次数 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; }