Coins POJ - 3210

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
  題意比較難理解。。。。給定n個硬幣,無論硬幣怎麼放置,你都能通過x次翻轉(每次僅能翻轉一次),讓硬幣全正或者全反面。思路:當n==2是,桌面爲一正一反,應該翻奇數次,若全爲正全爲反,應該翻偶數次

                     由此可推,當n爲偶數時,正面爲奇數,反面爲奇數,一定要翻奇數次才能保證全正或者全反。正面爲偶數,一定要翻偶數次才符合題意。可知n若爲偶數,一定不可能翻出要求的情況。

  當n爲奇數時,若有1個與其他面不同,假設翻轉僅一次,對其他條件不符合(如果有兩個面與其他不同),所以應該翻n-1次,可以達到無論初始怎麼放置,都可以達到要求。


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int main()
{
    while(~scanf("%d",&n),n)
    {
        if(n%2==0)
            printf("No Solution!\n");
        else
        {
            printf("%d\n",--n);
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章