Java - 趣味题(2) --- 约瑟夫环
来源:互联网 发布:淘宝闹鬼的古着店 编辑:程序博客网 时间:2024/06/05 14:15
//约瑟夫环问题总结import java.util.ArrayList;import java.util.List;import java.util.Scanner;可以参照http://baike.baidu.com/link?url=gEHOUvebJJpHjtByuBtthE3qvwIgtADWOCowK7p_4F3gKP_nxJ05waKiCTQoTO88t38_kolAFpKGssT-swyl5K#2_4//问题1:只需要求出最后剩余的人的序号(0 ~ n-1)/* If number = 3 * f(1) = 0 * f(2) = 1 = (f(1) + 3) % 2 * f(3) = 1 = (f(2) + 3) % 3 * f(4) = 0 = (f(3) + 3) % 4 * f(5) = 3 = (f(4) + 3) % 5 * ... * f(n) = x = (f(n-1) + 3) % n * */public class Java_YSF{ public static void main(String arg[]){ System.out.println("\t\tYSF问题总结1"); Scanner sc = new Scanner(System.in); System.out.println("please input the Total num:"); int total = sc.nextInt(); System.out.println("Please input the kill poll num:"); int number = sc.nextInt(); int last = 0; for(int i = 2;i <= total; i++) { last = (last + number) % i; } System.out.println("The index of Last is " + last); }}//问题2:有条件的打印出出队的顺序(从第K个开始数数,每数数number就出队,并且最终打印出队的顺序)public class Java_YSF{ public static void main(String args[]) { System.out.println("\t\tYSF问题总结2"); Scanner sc = new Scanner(System.in); System.out.println("please input the Total num:"); int total = sc.nextInt(); System.out.println("Please input the kill poll num:"); int number = sc.nextInt(); System.out.println("Please input the first kill people number : (0,1,2.....)"); int K = sc.nextInt(); //构造0 1 2 3 4 5 ........ ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 0; i < total; i++) { list.add(i); System.out.print(list.get(i) + " "); } System.out.println(); int account = 1; int i = K; while(list.size() > i) { i++; if(i == list.size()) { i = 0; } account++; if(account == number) { System.out.print(list.get(i) + "*"); list.remove(i); account = 0; i--; } } System.out.println(); for(i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println("此处应该无内容,因为List的内容已经全部删掉了~~"); }}
1 0
- Java - 趣味题(2) --- 约瑟夫环
- 趣味算法-约瑟夫环
- java趣味算法(一)——约瑟夫问题
- 编程趣味题2
- 约瑟夫问题(约瑟夫环) java
- 【Java笔试题】约瑟夫环问题
- 约瑟夫环问题--java
- 约瑟夫环java
- java实现约瑟夫环
- java实现约瑟夫环
- java 实现约瑟夫环
- 约瑟夫环问题 Java
- 约瑟夫环JAVA
- 约瑟夫环--java实现
- 约瑟夫环 java实现
- JAVA求解约瑟夫环
- java写--约瑟夫环
- java约瑟夫环问题
- EMS SQL Manager下载及中文配置
- NIO学习资料
- 使用Picasso下载图片时的错误
- 【NOIp模拟】【dp】俄罗斯方块
- Android 读取assets文件中的文件
- Java - 趣味题(2) --- 约瑟夫环
- 经典SQL练习题
- 微信号开通检测软件选择需谨慎
- ffmpeg混音以及音视频混合
- 组合数学第三次作业
- PL/SQL和sql是什么区别?
- CSS hack 详解
- 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(二)
- ListView等的样式和内容适配器不复用问题