zoukankan      html  css  js  c++  java
  • Relax! It's just a game(排列组合,简单)

    Relax! It's just a game

    Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

    Description

    Download as PDF
    You: What's the score? Did I miss much?
    Me: It's 2-1 for elAhli and the second half just started. The first half was quite boring.
    You: Who scored first? elAhli or ezZamalek?
    Me: What difference does it make?
    You: Big difference! I can predict the outcome of the match if I knew the order of which goals were scored in the first half.
    Me: What do you mean?
    You: It's 2-1 for elAhli, right? One of three things could have happened: elAhli scored two goals then ezZamalek scored; Or, elAhli scored its first goal, then ezZamalek, then elAhli again; Or, ezZamalek scored first, then elAhli scored its two goals.
    Me: So?!! I still don't understand what difference does that make? It's still 2-1 for elAhli! Why don't you just relax and let us continue watching the game in peace.
    You: You don't understand!! I believe theprobability of who'll win depends on the order of how goals were scored. Now I have to predict the outcome for 3 possibilities.
    Me: And what if the score was 3-2? What would you have done then?
    You: I would have to work for 5 different possibilities. No?
    Me: Of course not! The number of possibilities isn't always equal to the sum.
    You: Can you tell me when will it be equal to the sum?
    Me: You're a programmer, why don't you write a program that counts the number of possibilities and compare it to the sum?
    You: I don't have the time, I want to watch the match. Besides, I have nine other problems to worry about.
    Me: I'll give you a hint. The possibilities will be equal to the sum only if one of the teams scored a certain number of goals.

    Input

    Your program will be tested on one or more test cases. Each test case specifies two natural numbers (A and B) (separated by one or more spaces) representing the score of the first half. No team will be able to score more than 10 goals. The last line of the input file contains two -1's (which is not part of the test cases.)

    Output

    Format For each test case where the number of possibilities is equal to the sum, print:


    A+B=C


    Where A and B are as above and C is their sum. If the number of possibilities is not equal to the sum, replace the `=' sign with `!=' (without the quotes.)

    Sample Input

    2 1
    1 0
    -1 -1
    

    Sample Output

    2+1=3
    1+0=1
    


    思路:进球总数为a+b,容易想到所有排列情况为(a + b)!,但注意同一个队伍所进的球在排列中不应当视为互异,比如甲队进2个球:a1,a2,则a1a2和a2a1是同一种排列,于是所有排列情况数应为:(a + b)! / (a ! * b!),所以可能的情况数等于两队进球总和时有:

    (a + b)! / (a ! * b!)= a + b,即是(a + b - 1)! / (a ! * b!) = 1……(1)。.利用这个关系就能判断对于特定的a和b,情况数和a+b的关系。注意题目中给的提示“The possibilities will be equal to the sum only if one of the teams scored a certain number of goals”,化简(1),可以得到,a、b中最小值为1,也就是说,a和b至少有一个为1。

    还需要注意的是,a = b = 0时,情况数不等于进球总数,这个时候情况数为1.

    AC CODE:

     1 //Memory: 0 KB         Time: 12 MS
     2 //Language: C++ 4.1.2         Result: Accepted
     3 
     4 #include <iostream>
     5 #include <cmath>
     6 #include <string>
     7 using namespace std;
     8 
     9 long long fact(long long n)
    10 {
    11     if(n == 1 || n == 0) return 1;
    12     else return n * fact(n - 1);
    13 }
    14 
    15 int main()
    16 {
    17     long long a, b;
    18     int idx;
    19     double p;
    20     string str[2] = {"=", "!="};
    21     while(cin >> a >> b && a != -1 && b != -1)
    22     {
    23         idx = 1;
    24         if(a+b)
    25         {
    26             p = 1.0 * fact(a + b - 1) / fact(a) / fact(b);
    27             if(fabs(p - 1) <= 1e-8)
    28             idx = 0;
    29         }
    30         cout << a << "+" << b << str[idx] << a + b << endl;
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    disable_irq与disable_irq_nosync使用场景
    linux中断处理原理分析
    工作队列(workqueue) create_workqueue/schedule_work/queue_work
    使用git建立远程仓库,让别人git clone下来
    C中字符串的几种定义方法及说明
    Linux 2.6内核Makefile浅析
    探究platform_driver中的shutdown用途
    匆匆
    至强CPU性能排行,从X3210起,由低至高排列。
    Linux 命令行快捷键
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910470.html
Copyright © 2011-2022 走看看