A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老衲玩IT 中级黑马   /  2013-8-27 09:00  /  1889 人查看  /  0 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itheima;
  2. import java.util.ArrayList;

  3. /**
  4. *  有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。
  5. * 然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
  6. *  @author Administrator
  7. *
  8. */
  9. public class Test10 {

  10.         public static void main(String[] args) {
  11.                 //使用基本数据类型数组存储
  12.                 circle();
  13.                 //使用动态数组存储
  14.                 circleList();
  15.         }
  16.         public static void circle(){
  17.                 //二维数组表示100个人的编号和状态0表示出局,1表示在场
  18.                 int[][] men=new int[100][1];               
  19.                 /////////初始化数组,所有人标记为在场///////////////////////
  20.                 for (int i = 0; i < men.length; i++) {
  21.                         men[i][0]=1;
  22.                 }
  23.                 /////////初始化数组,所有人标记为在场///////////////////////
  24.                
  25.                 boolean flag=true;
  26.                 //报数
  27.                 int num=0;
  28.                 //记录出局者数量
  29.                 int outs=0;
  30.                 while (flag) {
  31.                         //////////判定是如果在场,进行报数,报到14,标记0状态出局,重置报数//////////////////////
  32.                         for (int i = 0; i < men.length; i++) {
  33.                                 if (men[i][0]==1) {
  34.                                         if (++num==14) {
  35.                                                 men[i][0]=0;
  36.                                                 num=0;
  37.                                                 outs++;
  38.                                         }
  39.                                 }
  40.                         }
  41.                         //////////判定是如果在场,进行报数,报到14,标记0状态出局,重置报数//////////////////////
  42.                        
  43.                         //////////判断在场人数是否小于14结束报数,是则停止报数/////////
  44.                         if (100-outs<14) {
  45.                                 flag=false;
  46.                         }
  47.                         //////////判断在场人数是否小于14结束报数,是则停止报数/////////
  48.                 }
  49.                 //输出剩余在场者的标号
  50.                 for (int i = 0; i < men.length; i++) {
  51.                         if (men[i][0]==1) {
  52.                                 System.out.println("alive:"+(i+1));
  53.                         }
  54.                 }
  55.         }
  56.         public static void circleList(){
  57.                 ArrayList<Man> circle=new ArrayList<Man>();
  58.                 for (int i = 0; i < 100; i++) {
  59.                         circle.add(new Man(i+1,false));
  60.                 }
  61.                 int outs=0;
  62.                 boolean flag=true;
  63.                 int num = 0;
  64.                 while (flag) {               
  65.                         for (int i = 0; i < circle.size(); i++) {
  66.                                 if (!circle.get(i).isOut()) {
  67.                                         if (++num == 14) {
  68.                                                 circle.get(i).setOut(true);
  69.                                                 outs++;
  70.                                                 num = 0;
  71.                                         }
  72.                                 }

  73.                         }
  74.                         if (100-outs<14) {
  75.                                 flag=false;
  76.                         }
  77.                 }
  78.                 for(Man man : circle){
  79.                         if (!man.isOut()) {
  80.                                 System.out.println(man);
  81.                         }
  82.                        
  83.                 }
  84.         }

  85. }
  86. class Man{
  87.         private int num;
  88.         private boolean isOut;
  89.         public Man(int num, boolean isOut) {
  90.                 this.num = num;
  91.                 this.isOut = isOut;
  92.         }
  93.         public boolean isOut() {
  94.                 return isOut;
  95.         }
  96.         public void setOut(boolean isOut) {
  97.                 this.isOut = isOut;
  98.         }
  99.         public int getNum() {
  100.                 return num;
  101.         }
  102.         @Override
  103.         public String toString() {
  104.                 return "Man [num=" + num + ", isOut=" + isOut + "]";
  105.         }
  106.        
  107. }
复制代码
本人测试过两种方式的用时,用bean和动态数组所消耗的时间是用基本类型数组耗时的两倍,太史公日:便利是有代价的。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马