首页 > 代码库 > HDU2131 Probability【水题】

HDU2131 Probability【水题】

Probability

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5205    Accepted Submission(s): 2597

Problem Description
Mickey is interested in probability recently. One day , he played a game which is about probability with mini.First mickey gives a letter and a word to mini.Then mini calculate the probability that the letter appears in the word.For example,give you the letter "a" and the word "apple". the probability of this case is 0.20000.

Input
The input contains several test cases. Each test case consists of a letter and a word.The length of the word is no longer than 200.
 
Output
For each test case, print the probability rounded to five digits after the decimal point.
 
Sample Input
a apple
c Candy
a banana
 
Sample Output
0.20000
0.20000
0.50000
 
Author
KiKi
 
Source

HDU 2007-11 Programming Contest_WarmUp


题目大意:输入一个字符ch和一个字符串s(无视大小写),求这个字符在字符串中出现的概率

思路:用isupper函数判断字符ch和字符串s里的字符里是不是大写字母,如果是大写字母就

全部转换为小写字母,判断并计算字符ch在字符串s里出现的次数,除以总字符就是概率。


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;

char s[220],ch;

int main()
{
    while(cin >> ch >> s)
    {
        int len = strlen(s);
        int num = 0;
        if(isupper(ch))
            ch += 32;
        for(int i = 0; i < len; ++i)
        {
            if(isupper(s[i]))
                s[i] += 32;
            if(ch == s[i])
                num++;
        }

        printf("%0.5lf\n",num*1.0/len);
    }

    return 0;
}


HDU2131 Probability【水题】