zoukankan      html  css  js  c++  java
  • 【原】 POJ 3210 Coins 巧妙的简单题 解题报告

    http://poj.org/problem?id=3210

    方法:
    当n个硬币初始同面,则只能偶次翻转才能保持同面,所以答案肯定是偶数。
    n为偶数时,假设1个为正,n-1个为反,两中情况同为奇数,所以只能通过奇数次翻转才能保持同面,
       这与上面的情况矛盾,所以n为偶数时无解
    n为奇数时,正反的个数必然是一个偶数一个奇数。假设1个为正,n-1个为反,为了和答案是偶数吻
       合,则最少需要翻n-1次。所以证得n为奇数时答案为n-1

    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
       1: #include <stdio.h>
       2:  
       3: void run3210()
       4: {
       5:     int n ;
       6:     while( scanf("%d",&n) && n!=0 )
       7:     {
       8:         if( n%2 ==0 )
       9:             printf("No Solution!\n");
      10:         else
      11:             printf("%d\n",n-1) ;
      12:     }
      13: }

    如果您满意我的博客,请点击“订阅Allen Sun的技术博客”即可订阅,谢谢:)

    原创文章属于Allen Sun
    欢迎转载,但请注明文章作者Allen Sun和链接
  • 相关阅读:
    怎样评价海贼王中艾斯的死?
    interleaving-string
    打败微信的将是怎么样的一款产品?
    javascript实例——文本特效篇(包含3个小例子)
    javascript实例——时间日期篇(包含5个实例)
    【转】Js获取当前日期时间及其它操作
    【转】外国朋友出的js题目,你能对几道
    【转】30+有用的CSS代码片段
    php的ajax简单实例
    Border属性的各种变化
  • 原文地址:https://www.cnblogs.com/allensun/p/1872066.html
Copyright © 2011-2022 走看看