首页 > ACM题库 > HDU-杭电 > hdu 2131 Probability[解题报告]C++
2013
12-29

hdu 2131 Probability[解题报告]C++

Probability

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

a apple
c Candy
a banana

样例输出:

0.20000
0.20000
0.50000

本题考查字符的格式输入问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2131


三种格式的 输入比较:

(1):

while(scanf("%c",&a)!=EOF)
   {
      getchar();
      scanf("%s",str);
      getchar();
   }

(2):

while(scanf(" %c",&a)!=EOF)
   {
      scanf("%s",str);
   }

(3):

while(scanf("%c ",&a)!=EOF)
   {
      scanf("%s\n",str);
   }

具体代码:(水)

/*************
  HDU 2131.cpp
Author:souly

http://blog.csdn.net/aiyi21

Time:2013.8.5
*************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
   freopen("in.txt","r",stdin);
   int i,j,len,sign;
   char a,str[205];
   while(scanf("%c ",&a)!=EOF)
   {
      // getchar();
      scanf("%s\n",str); 
      //getchar();
       sign=0;
       len=strlen(str);
       char b=(a>='A'&&a<='Z')?a+32:a-32;
       for(i=0;i<len;i++)
       {
           if(str[i]==a||str[i]==b)
            sign++;
       }
       double sum=double(sign)/len;
       printf("%0.5lf\n",sum);
   }
return 0;
}

解题转自:http://blog.csdn.net/aiyi21/article/details/9774335


  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  2. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish