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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值