【原】 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: }
posted @ 2010-11-08 19:43  Allen Sun  阅读(403)  评论(0编辑  收藏  举报