sicily1634. Relax! It's just a game

Description
    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 the probability 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.)

 

 

首先由题意得所求的possibility相当于对a+b个空格进行无区别的填空,即组合,假设这里填a(显然,填了a之后b的组合也是完全确定的),所求式子为

 

这里还有一个很有趣的地方:

当possibility和sum相等时,化简得

 

注意到在原题中a=0时,原式化为

当且仅当b=1原式成立,换而言之当b=1时,若a=0此式成立。考虑到一开始对a、b选取的随意性,b=1和前面a=1是一样的,都是a、b其中为1,故这里排除对a=0的讨论

代入可知a=1为该式成立之充分条件,下面求证a=1也是该式成立之必要条件

假设a !=0且a!=1,则必有a>1,有

 

此式必不成立,所以a=1是该式成立之必要条件

换而言之,该式成立的充要条件是a=1或b=1

所以当a=1或b=1时,输出"=",否则输出“!=”

 

第一种AC代码,判断组合数是否与a+b相等

View Code
 1 #include<stdio.h>
 2 int fac ( int n )
 3 {
 4     int ans = 1, i;
 5     
 6     if ( n == 0 )
 7         return 1;
 8     else
 9     {
10         for ( i = 1; i <= n; i++ )
11             ans *= i;
12     }
13     
14     return ans;
15 }
16 int main()
17 {
18     int a, b;
19 
20     while( scanf("%d %d", &a, &b) && a != -1 )
21     {
22         printf("%d+%d", a, b);
23         printf("%s", fac(a+b)/(fac(a)*fac(b))==a+b ? "=" : "!=");
24         printf("%d\n", a+b);
25     }
26     
27     return 0;
28 }

 

第二种AC代码,判断是否a、b其中为1

View Code
 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int a, b;
 6 
 7     while( scanf("%d %d", &a, &b) && a != -1 )
 8     {
 9         printf("%d+%d%s%d\n", a, b, a==1||b==1?"=":"!=", a+b );
10     }
11     
12     return 0;
13 }

 

转载于:https://www.cnblogs.com/joyeecheung/archive/2013/01/30/2883022.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值