nyoj-204Coin Test

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”;;;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值