Coin Test
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
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int gcd(int m,int w)
{
int x;
while(w>0){
x=m%w;
m=w;
w=x;
}
return m;
}
int main()
{
char a[65540];
int n,y=0;
float m=0.0,x=0.0;
cin>>n;
scanf("%s",a);
for(int i=0;i<n;i++){
if(a[i]=='S')
y=1;
if(a[i]=='U')
m++;
}
x=gcd(m,n);
if(y==1)
printf("Bingo\n");
else if(((m/n)-0.5<=0.003)&&((m/n)-0.5>=-0.003))
printf("%d/%d\n",(int)(m/x),(int)(n/x));
else
printf("Fail\n");
return 0;
}
思路解析:输出结果只有三种情况:
当输入的字符中有“S”,则输出“Bingo”;
当输入的字符串中,字符“U”出现的频率为between the result and 0.5 is no larger than 0.003,即在0.5左右的0.003范围内。。
就输出他的频率,因为要用最简分数表示,所以,要求其最大公约数,SO,gcd()函数出现了,,,现在知道gcd()函数,不再<math.h>库函数中,所以要自己定义构造,,,以后要仔细关注这一方面!!!
当不再其范围内,就输出“Fail”;;;