ZZU第四届程序设计大赛 7-Coin Test

7.Coin Test
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 and try.In the past there
were some famous mathematicians working on this.They repeat the throwing job once
again .But Jacmy is a lasy 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 to bank
14
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
possibility of the coin on the right side as a fractional number if the possibility 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.
Input
There will be tow lines 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 letters.The
letters 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.
Output
If the 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".
Sample
input   output
6
UUUDDD
1/2
input output
9
UUUUUDDDD
Fail
input output
6
UUUUUS
Bingo

 

英语题,很简单,只是英语比较恶心人~~~先判断是否符合条件,然后按分数形式输出~~~按分数形式输出要判断最大公约数~~~

 1 #include<stdio.h>
2 #include<math.h>
3 int gcd(int x, int y)
4 {
5 int z;
6 while(x != 0)
7 {
8 z = y % z;
9 y = x;
10 x = z;
11 }
12 return y;
13 }
14 int main()
15 {
16 int u, d, n, ok, a, i;
17 char c;
18 scanf("%d", &n);
19 getchar();
20 u = d = 0;
21 ok = 1;
22 for(i = 0; i < n; i++)
23 {
24 scanf("%c", &c);
25 if(c == 'S')
26 {
27 ok = 0;
28 break;
29 }
30 else if(c == 'U')
31 u++;
32 else if(c == 'D')
33 d++;
34 }
35 if(!ok)
36 {
37 printf("Bingo\n");
38 }
39 else if(fabs(1.0*u/(u+d) - 1.0/2) <= 0.003)
40 {
41 a = gcd(u, u+d);
42 printf("%d/%d\n", u/a, (u+d)/a);
43 }
44 else
45 {
46 printf("Fail\n");
47 }
48 return 0;
49 }

 

你可能感兴趣的:(test)