zoukankan      html  css  js  c++  java
  • POJ 3210 : Coins

    POJ 3210 : Coins

    Time Limit: 1000MS Memory Limit: 131072K
    Total Submissions: 7001 Accepted: 4616

    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

    题意我认为非常难理解,意思是n个硬币,不管是怎么摆的(正面或是反面)。是否能找到一个最小值x,使得这n个硬币,通过x次翻转,必能到达全正或是全反的状态。能。则输出x的值。不能,则输出“No Solution!”

    分情况讨论:

    如果n是一个偶数:
    如果向上的数量为偶数。则向下的数量也一定为偶数,所以这个最小值x也一定是偶数。


    如果向上的数量是奇数,则向下的数量也是奇数,这个最小值x一定得是奇数。


    此时。得到的x在两种情况中互相矛盾,所以偶数的话,要输出“No Solution!

    如果n是一个奇数:
    事实上n是一个奇数的话。仅仅有一种情况了,向上的数量是m,向下的数量是n-m,两者一定一个奇数。一个偶数。

    此时向上即向下,向下即向上。
    此时会发现偶数次翻转就可以完毕任务,仅仅需将状态不同的两种硬币中,为偶数的数量的那一堆翻转就可以,此时两堆硬币已经达到了同上或是同下,这时,多出来的反转次数。仅仅需对一个硬币来回翻转,由于是偶数次,所以不影响最后状态了。


    那么这个x最小是多少?
    如果n个硬币,1个向上。n-1个向下。依据上面所述,这时须要n-1次翻转,这时是满足全部情况的最小值了。

    代码:

    #include<iostream>
    using namespace std;
    
    int main()
    {
        int n;
        while(cin>>n)
        {
            if(n==0)
                break;
    
            if(n%2)
            {
                cout<<n-1<<endl;
            }
            else
            {
                cout<<"No Solution!"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    什么是用户画像——从零开始搭建实时用户画像(一)
    一站式Kafka平台解决方案——KafkaCenter
    Druid 0.17入门(4)—— 数据查询方式大全
    流媒体与实时计算,Netflix公司Druid应用实践
    解读银行卡支付背后的原理
    求求你了,不要再自己实现这些逻辑了,开源工具类不香吗?
    编程坑太多,Map 集合怎么也有这么多坑?一不小心又踩了好几个!
    设计数据库 ER 图太麻烦?不妨试试这两款工具,自动生成数据库 ER 图!!!
    一口气带你踩完五个 List 的大坑,真的是处处坑啊!
    轻轻一扫,立刻扣款,付款码背后的原理你不想知道吗?|原创
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7356627.html
Copyright © 2011-2022 走看看