POJ 3210(逻辑推理)

127 篇文章 0 订阅
85 篇文章 2 订阅

Coins

Time Limit: 1000MS Memory Limit: 131072K

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.

Sample Input

2
3
0

Sample Output

No Solution!
2

问题分析

说实话,这题可折磨我半天(抓狂),主要还是没理解题意,以及逻辑推理能力太差了QAQ。
史努比有n个硬币,经过x次翻转之后可以让所有的硬币全部正面或者反面朝上,求x的最小值。

①如果有偶数枚硬币,则有奇数枚朝上(下)+奇数枚朝下(上)和偶数枚朝上(下)+偶数枚朝下(上)这俩种情况。如果是前者,则史努比经过奇数次翻转就可以使全部硬币统一方向,如果是后者,则史努比经过偶数次翻转就可以使全部硬币统一方向。

举个栗子,比如现在有4枚硬币,初始状态有0001,0011,0111,……(我们在这里用0表示朝下,1朝上)。如果是0001,,史努比可以翻转1次或者3次来实现;如果是0011,史努比可以翻转两次来实现;如果是0111,史努比可以翻转3次或1次来实现。假设现在x的值为3,那么初始状态为0001,0111的硬币都可以实现,但是如果是0011,当翻转两次之后,硬币就已经满足目标状态了,此时再翻第三次就一定会打破状态。所以,总结来说就是当n为偶数时将没有确切的x值实现目标状态。

②如果有奇数枚硬币,则有偶数枚朝上(下)+奇数枚朝下(上)这一种情况。那么史努比经过偶数次翻转,一定可以使得全部硬币满足目标状态。

同样举个栗子,现在有5枚硬币,初始状态有00001,00011,……。如果是00001,史努比可以翻转1次或4次来实现;如果是00011,史努比可以翻转2次或3次来实现。如果求x的最小值,我们就来看翻转次数最多的状态是否可以满足其他状态即可。这里我们可以看到翻转次数的最多是4次,状态为00001,我们来看其他一种状态00011,我们可以两次来使得00011->00000,剩下的两次可以通过来翻转同一个硬币来实现,这里目标状态不被破坏是因为我们总是可以通过偶数次翻转来实现目标状态,然后剩余偶数次翻转我们可以翻转同一个硬币来保持目标状态(一个硬币翻转偶数次不还是原来的状态嘛^_^)。

◆总结

所以来说,当n为偶数时为答案为No Solution!;当n为奇数时,答案为x = n - 1。

ok,上code。

#include<iostream>

using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        if(n&1)
        cout<<n-1<<endl;
        else
        cout<<"No Solution!"<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值