算法题(报数问题)
来源:互联网 发布:计算复杂性 正规算法 编辑:程序博客网 时间:2024/05/17 22:19
题目:13个人围坐一圈报数(123的报数),凡是报到3的人出队,问最后剩下的人的序号
JAVA实现
/** * Created by Kameleon on 14-5-29. * 30个人围坐在一起报数,报到3的人出圈 */public class Count { public static void main(String[] args){ boolean[] arr = new boolean[13];//用于记录哪些人已经出队了 for(int i = 0;i < arr.length;i++){ arr[i] = true; } int leftCount = 13; int countNum = 0; int index = 0;//记录报数的人的序号,会超过13,所以之后的操作需要求余 while(leftCount > 1){ if(arr[index % 13] == true){ countNum++; if(countNum == 3){ countNum = 0; arr[index % 13] = false; leftCount--; } } index++; } //输出剩余的序号 for(int i = 0;i < arr.length;i++){ if(arr[i] == true){ System.out.println(i); } } }}
答案是:12
0 0
- 算法题(报数问题)
- 小孩报数问题(模拟水题)
- 算法与数据结构 数 报数问题
- 【算法】约瑟夫环问题(报数出列)
- 报数问题 (数组实现)
- 报数问题(约瑟夫环)
- 小孩报数问题(3750)
- 报数(约瑟夫环问题)
- 报数问题
- 报数问题
- 报数问题
- 报数问题
- 报数问题
- 报数问题
- 报数问题
- 围圈报数问题(约瑟夫问题)
- 约瑟夫环问题(报数问题)
- 算法:模拟法(报数游戏)
- discuz7.2的基本--设置前台的显示信息
- C#winform控件多线程访问
- 编程能力的四种境界
- 入职后的书单
- pvr与png的内存占用
- 算法题(报数问题)
- 邻接矩阵计算节点对最短路径
- 软件开发中的“瑞士军刀综合征”
- IO空间的静态映射基本过程
- 第八章 Spring 对 DAO 的支持 《Spring3.x》
- 文件系统 busybox and initramfs
- MapReduce实现排序功能
- Android实现分享内容到微信朋友圈
- PendingIntent的相关基础知识