当前位置: 代码迷 >> 综合 >> Coin Test
  详细解决方案

Coin Test

热度:101   发布时间:2023-09-22 23:26:45.0

Coin Test

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 1
描述

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"
样例输入
6
UUUDDD
样例输出
1/2
来源
郑州大学校赛题目
上传者

张云聪

问题地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=204

题目大意:

输入包含U、S、D的字符串,会告诉个数,U表示抛硬币正面向上,S表示硬币立起来了,D表示硬币反面向上。用来模拟抛硬币的概率实验。

当有硬币立起来时,输出Bingo。

否则计算正面向上的概率,若概率与0.5的差距在0.003范围之内,认为是成功的实验,此时要求输出概率。

否则输出Fail。

代码:

#include <iostream>  
#include <stdio.h>   
#include <string.h>  
#include <math.h>  
#include <vector>  
#include <queue>  
#include <stack>  
#include <map>  
#include <string>  
#include <algorithm>  
#include <iomanip>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//int gcd(int top,int bottom){  
//    //求 top 和 bottom的最大公约数 根据程序的执行逻辑和题意可知 第一次进入时top和bottom不为0    
//   if(bottom == 0){  
//        return top;  
//    }else return gcd(bottom,top%bottom);  
//} 
int gcd(int a,int b)  
{  
int temp;  
while(a%b){  
temp=a%b;  
a=b;  
b=temp;  
} 
return b;
}
int main(int argc, char** argv) {
 /*ifstream a=freopen("/input.txt","r",stdin);
 ofstream a=freopen("/output.txt","w",stdout);*/
 int n,countU=0,countD=0;
 bool haveStand=false;
 scanf("%d",&n) ;
 getchar();
 int cc=n;
 while(cc--){
 char tmp;
 scanf("%c",&tmp);
 if(tmp == 'S'){
 haveStand=true;
 break; 
 }
 if(tmp == 'U'){
 countU++;
 }
 if(tmp == 'D'){
 countD++;
 }
 }
 //cout<<countU<<countD<<endl;
 double x=(double)countU/n;
 if(haveStand == true){
 printf("Bingo\n");
 }else if((double)countU/n-0.5>0.003 ||(double)countU/n-0.5<-0.003){
 printf("Fail\n");
 }else{
       
 
 //cout<<countU<<"  "<<n<<endl;
 int g=gcd(countU,n);
 //printf("%d\n",g) ;
 printf("%d/%d\n",countU/g,n/g);
 }
 return 0;
}
代码分析:

一开始

scanf("%d",&n) ;getchar();
出没加
getchar();
导致一直WA,然后就看别人的代码找错,刚开始觉得是后面的double除法时候不精确什么的,然后觉得是我的求最大公倍数不对,一一检查;后发现没有问题,最好一看前面才发现计数后的U和D的个数不对,D的个数少一个,然后想到可能是输入n之后按得回车换行吃了一个scanf("%c",&tmp)。

这里查找到了scanf()读入和缓冲区的机理的文章。文章地址:http://blog.csdn.net/veniversum/article/details/52901619

优秀代码:

01. #include<cstdio>
02. int u,d;
03. int gcd(int a,int b)
04. {
05. if(a==0) return b;
06. else return gcd(b%a,a);
07. }
08. int main()
09. {
10. int n;
11. char c;
12. scanf("%d",&n);
13. getchar();
14. for(int i=0;i!=n;i++)
15. {
16. c=getchar();
17. if(c=='S') { puts("Bingo");return 0;}
18. if(c == 'U') ++u;
19. else ++d;
20. }
21. int g=gcd(u,u+d);
22. if((double)u/(u+d)-0.5>0.003 ||(double)u/(u+d)-0.5<-0.003) puts("Fail");
23. else printf("%d/%d",u/g,(u+d)/g);
24. }



  相关解决方案