Coin test

来源:互联网 发布:上海网站关键词优化 编辑:程序博客网 时间:2024/06/01 10:13

描述

As is known to all,if you throw a coin up and let it droped on the desk there are usually three results. Yes,just believe what I say ~it can be the right side or the other side or standing on the desk, If you don't believe this,just try In the past there were some famous mathematicians working on this .They repeat the throwing job once again. But jacmy is a lazy boy.He is busy with dating or playing games.He have no time to throw a single coin for 100000 times. Here comes his idea,He just go bank and exchange thousands of dollars into coins and then throw then on the desk only once. The only job left for him is to count the number of coins with three conditions.

He will show you the coins on the desk to you one by one. Please tell him the possiblility of the coin on the right side as a fractional number if the possiblity between the result and 0.5 is no larger than 0.003. BE CAREFUL that even 1/2,50/100,33/66 are equal only 1/2 is accepted ! if the difference between the result and 0.5 is larger than 0.003,Please tell him "Fail".Or if you see one coin standing on the desk,just say "Bingo" any way.

输入
Three will be two line as input.
The first line is a number N(1<N<65536)
telling you the number of coins on the desk.
The second line is the result with N litters.The letter are "U","D",or "S","U" means the coin is on the right side. "D" means the coin is on the other side ."S" means standing on the desk.
输出
If test successeded,just output the possibility of the coin on the right side.If the test failed please output "Fail",If there is one or more"S",please output "Bingo"
样例输入
6UUUDDD
样例输出
           1/2
解题思路:
    一看到那么长的英文描述,就给人一种特别难的感觉,实际不然,其主要信息就是:
用N个硬币做实验,每个硬币可能会出现三种情况:正面,反面和竖起来。当出现一个或多个硬币竖起来就输出“Bingo”;出现正面的概率与0.5的差值小于0.003就输出正确,否则输出错误。
本题需要注意的是,1.读取n之后,应该用getchar();将回车读取出来,否则会影响以下程序的进行。2.整形/整形其结果还会是整形,可以通过整形/整形*1.0的到double型数据。3.输出分数之前,应该将分子分母进行约分再输出。4.分数的输出形式为(”%d/%d“,a,b)。
程序代码:
#include<stdio.h>#include<math.h>void fun(int *a,int *b,double *s);int main(){int n,count=0,j;char i;double s;scanf("%d",&n);getchar();for(j=1;j<=n;j++){scanf("%c",&i);//putchar(i);if(i=='S'){printf("Bingo\n");break;}if(i=='U')count++;}fun(&count,&n,&s);//printf("%d %d %lf",count,n,s);if(fabs(s)<=0.003)printf("%d/%d\n",count,n);else if(fabs(s)>0.003)printf("Fail\n");return 0;}void fun(int *a,int *b,double *s){int i;for(i=2;i<=*a;i++){while(*a%i==0&&*b%i==0){*a/=i;*b/=i;}(*s) = (*a) / (*b * 1.0)-0.5;}}



0 0
原创粉丝点击