1220 Problem M- letters probability - Getting title - string handling -C ++ to achieve

Question M: letters probability

Time limit: 1 Sec Memory Limit: 32 MB
submitted: 161 solved: 71

Title Description

Xiao Ming recently very interested in probability problems. One day, Xiao Ming to play a game of chance and red together, first of all Xiaoming given a letter and a word, the probability of this letter appeared in this word is then calculated from red. The letters are not case sensitive.
For example, given the letters a, word is apple, then the probability is 0.20000.

Entry

Test input comprising a plurality of sets of data. Each set of data comprising a letter and a word. Not more than 200 words in length.

Export

For each input, output probability corresponding to the result of five decimal places.

Sample input  Copy

a apple
c Candy
a banana

Sample output  Copy

0.20000
0.20000
0.50000

Code

 

#include <iostream>
#include <string>
#include <algorithm>
//输入过程当中,注意输入格式,用getchar()吸收空格和回车键
using namespace std;

int main(){
  
        char c;
        string s;
        int num=0;
        while(scanf("%c",&c)!=EOF){
                getchar();
                cin>>s;
                getchar();
                int len=s.size();
                for(int i = 0;i < len;i++){
                    if(c>='A'&&c<='Z'){
                        c=c+32;
                    }
                    if(s[i]>='A'&&s[i]<='Z'){
                        s[i]+=32;
                    }
                    if(c==s[i]){
                        num++;
                    }
                }
                printf("%.5f\n",1.0*num/len);
                num=0;
        }
}

 

Published 20 original articles · won praise 0 · Views 122

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/104710763
Recommended