Coins(poj3210,推论题)

/*
http://poj.org/problem?id=3210
Coins
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 6532 Accepted: 4271
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
Source


POJ Monthly--2007.04.01, Snoopy
[Submit]   [Go Back]   [Status]   [Discuss]
解析:
题意:
给出n枚硬币,它们的正反面是任意的,最少需要返翻多少次一定可以将全部变为正面或全部变为反面
思路:
这里分奇偶情况讨论,设正面为"1",反面为"0"
一、n%2==0:
(1).偶数+偶数=偶数:即偶数个正面+偶数个反面,例:110000,可以翻,2,4,6,8,10......次,即偶数次
(2).奇数+奇数=偶数:即奇数个正面+奇数个反面,例:100000,可以翻1,3,5,7,9......奇数次
故:(1)的解只能偶数,(2)的解只能是奇数,所以无解
二、n%2==1:
(1).偶数+奇数=奇数:即偶数个正面+奇数个反面,例:11000,可以翻2,3,4,5,6.....
(2).奇数+偶数=奇数:即奇数个正面+偶数个反面,例:11100,可以翻2,3,4,5,6
故:(1),(2)的情况是可以等价的,只需翻偶数个正面或偶数个反面即可,偶数最多的情况下是n-1,故答案为n-1
Accepted 164 KB 0 ms C++ 218 B
*/
#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
int main()
{ int n;
 while(scanf("%d",&n)!=EOF&&n!=0)
{
	if(n%2==0)
	printf("No Solution!\n");
	else
	printf("%d\n",n-1);
}
    return 0;
}


你可能感兴趣的:(Coins(poj3210,推论题))