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?
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.
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!
”
2 3 0
No Solution! 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;
}