zoukankan      html  css  js  c++  java
  • Lesson_4 作业_1 魔术师的秘密

     1 /******************************题目描述***********************************
     2 *     在一次晚会上,一位魔术师掏出一叠扑克牌,取出其中13张黑桃,预先洗好后,
     3 * 把牌面朝下,对观众说:“我不看牌,只数一数就能知道每张牌是什么?”魔术师口
     4 * 中念一,将第一张牌翻过来看正好是A;魔术师将黑桃A放到桌上,继续数手里的余
     5 * 牌,第二次数1,2,将第一张牌放到这叠牌的下面,将第二张牌翻开,正好是黑桃2,
     6 * 也把它放在桌子上。第三次数1,2,3,前面二张牌放到这叠牌的下面,取出第三张
     7 * 牌,正好是黑桃3,这样依次将13张牌翻出,准确无误。现在的问题是,魔术师手中
     8 * 牌的原始顺序是怎样的?
     9 ********************************解题思路***********************************
    10 *     解决这类问题的关键在于利用倒推的方法推出原来牌的顺序。
    11 *     假设桌上摆着13个空盒子,编号为1至13,将黑桃A放入第一个盒子中,从下一个
    12 * 空盒子开始对空盒子计数,当数到第二个空盒子时,将黑桃2放入空盒子中,然后再
    13 * 从下一个空盒子开始对空盒子计数。顺序放入3,4,5等,直到全部放入13张牌,注意
    14 * 在计数时要跳过非空的盒子,只对空盒子计数,最后得到的牌在盒子中的顺序,就是
    15 * 魔术师手中原来牌的顺序。
    16 *
    17 ********************************注释***********************************
    18 *                       2012-1-14 by 樊列龙
    19 ****************************************************************************/
    20 
    21 public class 魔术师的秘密{
    22     public static void main(String []args){
    23         int []poker = new int[14];
    24         String []pokerStr = {"0","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    25         int mem = 1;
    26         int j = 1;
    27         for(int i = 1; i < 14; i++){
    28             int n = 0;
    29             for(j = mem; n != i; j++){
    30                 if (j > 13) j = 1;
    31                 if(poker[j] == 0){
    32                     n++;
    33                 }
    34             }
    35             mem = j--;//这里的自减必不可少,并且只能是后置的自减
    36             poker[j] = i;
    37         }
    38 
    39 //        for(int i = 1; i < 14; i++){
    40 //            System.out.print(poker[i] + " ");
    41 //        }
    42 //        System.out.println();
    43 
    44         for(int i = 1; i < 14; i++){
    45             System.out.print(pokerStr[poker[i]] + " ");
    46         }
    47         System.out.println();
    48     }
    49 }

    老师的程序

     1 //老师写的13-01-15
     2 public class 魔术师的秘密{
     3     public static void main(String []args){
     4         int []pokers = new int[14];//从1开始用
     5         int total = 1;//13张牌
     6         int index = 1;//当前数组的位置
     7         int count = 1;//计数器
     8 
     9 
    10         for(;total != 14;){
    11             if(index > pokers.length-1){
    12                 index = 1;
    13             }
    14             if(pokers[index] == 0){//检查当前位置是否有牌
    15                 if(count == total){
    16                     pokers[index] = total;//放下牌
    17                     count = 1;//计数器复位
    18                     total++;//开始下一张牌
    19                     index++;//计数器的位置后移一位
    20                 } else {
    21                     count++;
    22                     index++;
    23                 }
    24             }else{
    25                 index++;
    26             }
    27         }
    28 
    29         for(int i = 1; i < 14; i++){
    30             System.out.print(pokers[i] + " ");
    31         }
    32     }
    33 }

    运行结果:

      

  • 相关阅读:
    PHP 高精度计算
    PHPWord使用方法
    羽毛球
    大数据(2)
    大数据(1)
    Centos 7 启动错误:XFS_WANT_CORRUPTED_GOTO 修复
    selenium 自动化工具
    python 开发技巧(0)-- 各个系统的python安装
    Yii简单使用阿里云短信教程!
    VMware虚拟机 Ubuntu 实用技巧 (2)桥接模式连接网络与网卡的配置
  • 原文地址:https://www.cnblogs.com/CocoonFan/p/2861642.html
Copyright © 2011-2022 走看看