描述
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
 
 
 
代码:
 
#include <stdio.h>
#include <math.h>

//处理数据
static void handlerData(int readCount);
//计算a和b的最大公约数
static int calCommonDivisor(int a,int b);

int main()
{
    int readCount = 0;
    scanf("%d",&readCount);
    getchar();
   
    handlerData(readCount);
   
    return 0;
}

//处理数据
static void handlerData(int readCount)
{
    //输入总数
    int inputCount = readCount;
   
    //right side
    int rightSide = 0;
    //other side
    int otherSide = 0;
   
    int isBingo = 0;
   
    while (readCount > 0)
    {
        char inputChar;
        scanf("%c",&inputChar);
       
        if (inputChar == 'U')
        {
            ++rightSide;
        }
        else if(inputChar == 'D')
        {
            ++otherSide;
        }
        else
        {
            isBingo = 1;
        }
       
        --readCount;
    }
   
    if (isBingo == 1)
    {
        printf("Bingo\n");
        return;
    }
   
    // 0.003 0.5
    if(6*inputCount > 2000 * rightSide ||
       1000 * inputCount < 2000 * rightSide)
    {
        printf("Fail\n");
        return;
    }
   
    //分母通分
    int tmpValue = calCommonDivisor(rightSide,inputCount);
    printf("%d/%d\n",rightSide/tmpValue,inputCount/tmpValue);
}

//计算a和b的最大公约数
static int calCommonDivisor(int a,int b)
{
    int maxNum = a>b?a:b;
    int minNum = a<b?a:b;
   
    int midResult = maxNum % minNum;
    while(midResult != 0)
    {
        maxNum = minNum;
        minNum = midResult;
        midResult = maxNum % minNum;
    }
   
    return minNum;

}
 
 
需要注意题目意思:
1.题目中“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”,这句话的意思竟然是说“0.003<=result<=0.5”
 
2.0.003<=result<=0.5 的比较。有两种方法:第一种,很简单,很好想,就是转化成分数后通分,使分数不等式转化成整数不等式,即(6 * N <= 2000 * u) && (2000 * u <= 1000 * N);第二种,声明一个double变量c,使得c = u * 0.1 / N,这样,u,N,还是整形的,只要if(c >= 0.003 && c <= 0.5)就可以了,这个应该是比较大的收获。
 
该题目整体来说是将正面的个数/总数目,如果符合条件则以最简分式输出。
 

38-语言入门-38-Coin Test的更多相关文章

  1. Delphi XE2 之 FireMonkey 入门(38) - 控件基础: TPopupMenu、TMenuItem、TMenuBar、TMainMenu

    Delphi XE2 之 FireMonkey 入门(38) - 控件基础: TPopupMenu.TMenuItem.TMenuBar.TMainMenu 相关控件: TMenuBar.TPopup ...

  2. 【南阳OJ分类之语言入门】80题题目+AC代码汇总

    小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位. 本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载. 声明: 题目部分皆为南阳OJ题目. 代 ...

  3. C语言入门(21)——使用DBG对C语言进行调试

    C语言入门(21)--使用DBG对C语言进行调试 程序中除了一目了然的Bug之外都需要一定的调试手段来分析到底错在哪.到目前为止我们的调试手段只有一种:根据程序执行时的出错现象假设错误原因,然后在代码 ...

  4. 《JavaScript语言入门教程》记录整理:面向对象

    目录 面向对象编程 实例对象与 new 命令 this关键字 对象的继承 Object对象的方法 严格模式(strict mode) 本系列基于阮一峰老师的<JavaScrip语言入门教程> ...

  5. C语言入门教程-(3)基本数据类型

    1.数据类型 在C语言中,数据类型指的是用于声明不同类型的变量或函数的一个广泛的系统.C语言数据类型可以分为四种: 1.基本类型:它们是算术类型,包括两种类型:整数类型和浮点类型. 2.枚举类型:它们 ...

  6. 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)

    1.前言与作者 首先声明,我是由于非常偶然的机会获得<C语言入门很简单>这本书的,绝对不是买的.买这种书实在丢不起那人. 去年这书刚出版时,在CU论坛举行试读推广,我当时随口说了几句(没说 ...

  7. 我为什么反对推荐新人编程C/C++语言入门?

    虽然我接触编程以及计算机时间比较早,但是正式打算转入程序员这个行当差不多是大学第四年的事情 从03年接触计算机,07年开始接触计算机编程, 期间接触过的技术包括 缓冲区溢出(看高手写的shellcod ...

  8. 《C语言入门1.2.3—一个老鸟的C语言学习心得》—清华大学出版社炮制的又一本劣书及伪书

    <C语言入门1.2.3—一个老鸟的C语言学习心得>—清华大学出版社炮制的又一本劣书及伪书 [薛非评] 区区15页,有80多个错误. 最严重的有: 通篇完全是C++代码,根本不是C语言代码. ...

  9. c语言入门教程 / c语言入门经典书籍

    用C语言开始编写代码初级:C语言入门必备(以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言的数 ...

  10. 【转】c语言入门教程 / c语言入门经典书籍

    用C语言开始编写代码 初级:C语言入门必备 (以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言 ...

随机推荐

  1. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  2. 机器学习&&数据挖掘之一:决策树基础认识

    决策树入门篇 前言:分类是数据挖掘中的主要分析手段,其任务就是对数据集进行学习并构造一个拥有预测功能的分类模型,用于预测未知样本的类标号,把类标号未知的样本按照某一规则映射到预先给定的类标号中. 分类 ...

  3. 事务并发处理: DB+ORM+逻辑代码

    在学习了马士兵有关事务并发处理的视频后, 感觉对事务并发处理的概念,问题以及解决方式有了一定的了解,赶紧记录下来以备后用. 1. 事务:一系列操作要么都完成,要么一个都不完成 2. 事务并发:多个事务 ...

  4. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

  5. NYOJ-205 求余数 AC 分类: NYOJ 2014-02-02 12:30 201人阅读 评论(0) 收藏

    这题目看一眼以为难度评级出错了,只是一个求余数的题目,,后来才发现,位数小于百万位,,,我还以为是大小小于百万呢,所以借鉴了另一大神的代码, 用大数,重点是同余定理: (a+b)mod m=((a m ...

  6. .NET设计模式(11):组合模式(Composite Pattern)(转)

    概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦. 意图 将对 ...

  7. [Qt] 界面美化 [2013-06-17更新](转载)

    - 经验总结 1. 可用对话框(QDialog)模拟类似Android中toast的效果.     - 设置程序界面风格 在main函数中QApplication::setStyle("wi ...

  8. MVC 中 Razor 无限分类的展示

    在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上.可能本人水平有限,实在是不会用. 那我就用最简单爆力的办法来做. Model: public class NewsCa ...

  9. 什么是Nib文件?(Nib文件是一种特殊类型的资源文件,它用于保存iPhone OS或Mac OS X应用程序的用户接口)

    Nib文件是一种特殊类型的资源文件,它用于保存iPhone OS或Mac OS X应用程序的用户接口.Nib文件是Interface Builder文档.通常您会使用Interface Builder ...

  10. Python异步IO --- 轻松管理10k+并发连接

    前言   异步操作在计算机软硬件体系中是一个普遍概念,根源在于参与协作的各实体处理速度上有明显差异.软件开发中遇到的多数情况是CPU与IO的速度不匹配,所以异步IO存在于各种编程框架中,客户端比如浏览 ...