求解一个概率方面的问题,用C语言来写:

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

0

1个回答

#include
#include
void main()
{ char str[5] = "apple";//将所需测试的字符串数组放进字符数组里
char a='a';//目标字符
int num=0, i=0;
//循环检测整个字符数组
for(i=0;i<5;i++)
{
if(str[i]==a)
num++;
}
printf("%f",num/5.000);//注意不能用num/5 至于为什么我想你可以在书本教基础数据类型里有说到
}
这些都是简单的知识如果你要学习c的话遇到问题先看回顾一下课本

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的。是时候展现真正的技术了!