题意:给定一个字母和字符串,求字母在字符串中出现的概率。
解题思路:将字符串中的每个字符映射到map中,然后可求出概率,注意字母的大小写。
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<char,int> vis;
int main()
{
//freopen("input.txt","r",stdin);
char e,words[210];
while(~scanf("%c %s",&e,words))
{
getchar();
vis.clear();
int len = strlen(words);
if(e >='A' && e <= 'Z') e = tolower(e);
for(int i = 0; i<len; i++)
{
if(words[i] >='A' && words[i] <= 'Z') words[i] = tolower(words[i]);
vis[words[i]]++;
}
double pro = vis[e]*1.0/len;
printf("%.5lf\n",pro);
}
return 0;
}