【原】 POJ 3210 Coins 巧妙的简单题 解题报告

 

http://poj.org/problem?id=3210

 

方法:
当n个硬币初始同面,则只能偶次翻转才能保持同面,所以答案肯定是偶数。
n为偶数时,假设1个为正,n-1个为反,两中情况同为奇数,所以只能通过奇数次翻转才能保持同面,
   这与上面的情况矛盾,所以n为偶数时无解
n为奇数时,正反的个数必然是一个偶数一个奇数。假设1个为正,n-1个为反,为了和答案是偶数吻
   合,则最少需要翻n-1次。所以证得n为奇数时答案为n-1

 

Description

Snoopy has three coins. One day he tossed them on a table then and tried to flip some of them so that they had either all heads or all tails facing up. After several attempts, he found that regardless of the initial configuration of the coins, he could always achieve the goal by doing exactly two flippings, under the condition that only one coin could be flipped each time and a coin could be flipped more than once. He also noticed that he could never succeed with less than two flippings.

Snoopy then wondered, if he had n coins, was there a minimum number x such that he could do exactly x flippings to satisfy his requirements?

Input

The input contains multiple test cases. Each test case consists of a single positive integer n (n < 10,000) on a separate line. A zero indicates the end of input and should not be processed.

Output

For each test case output a single line containing your answer without leading or trailing spaces. If the answer does not exist, output “No Solution!

Sample Input

2
3
0

Sample Output

No Solution!
2

 

   1: #include <stdio.h>
   2:  
   3: void run3210()
   4: {
   5:     int n ;
   6:     while( scanf("%d",&n) && n!=0 )
   7:     {
   8:         if( n%2 ==0 )
   9:             printf("No Solution!\n");
  10:         else
  11:             printf("%d\n",n-1) ;
  12:     }
  13: }

转载于:https://www.cnblogs.com/allensun/archive/2010/11/08/1872066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值