码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2131 Probability【水题】

时间:2015-01-28 22:38:24      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

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【水题】

标签:

原文地址:http://blog.csdn.net/lianai911/article/details/43240701

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!