• 【原】 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和链接
  • 相关阅读:
    各大厂面试遇到的91道软件测试面试题+答案纯干货!!
    测试岗面试必看攻略
    自动化测试面试题及答案大全(1)
    自动化测试面试题及答案大全(2)
    自动化测试面试题及答案大全(3)
    自动化测试面试题及答案大全(4)
    自动化测试面试题及答案大全(5)
    Android反编译&Android安全测试
    ALV布局保存
    雨伞的主要材料有哪些?
  • 原文地址:https://www.cnblogs.com/allensun/p/1872066.html
Copyright © 2020-2023  润新知