zoukankan      html  css  js  c++  java
  • 38-语言入门-38-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 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);
    //计算ab的最大公约数
    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 ");
           
    return;
        }
       
       
    // 0.003 0.5
       
    if(6*inputCount > 2000 * rightSide ||
          
    1000 * inputCount < 2000 * rightSide)
        {
           
    printf("Fail ");
           
    return;
        }
       
       
    //分母通分
       
    int tmpValue = calCommonDivisor(rightSide,inputCount);
       
    printf("%d/%d ",rightSide/tmpValue,inputCount/tmpValue);
    }

    //计算ab的最大公约数
    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)就可以了,这个应该是比较大的收获。
     
    该题目整体来说是将正面的个数/总数目,如果符合条件则以最简分式输出。
     
  • 相关阅读:
    ActiveReports 报表应用教程 (3)---图表报表
    ActiveReports 报表应用教程 (4)---分栏报表
    ActiveReports 报表应用教程 (5)---解密电子商务领域首张电子发票的诞生(套打报表)
    hdu4467 Graph
    ActiveReports 报表应用教程 (6)---分组报表
    [leetcode]Search a 2D Matrix
    参加百度开放云编程马拉松后一点总结
    CAS服务器配置
    Tomcat 的 SSL 配置
    在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM
  • 原文地址:https://www.cnblogs.com/sharpfeng/p/5141927.html
Copyright © 2011-2022 走看看