zoukankan      html  css  js  c++  java
  • Java经典习题37

    /*
    题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,
    问最后留下的是原来第几号的那位。
    */

    import java.util.*;

    public class Class37 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("请输入总人数n:");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    boolean[] arr = new boolean[n];
    for(int i = 0; i < arr.length; i++){
    arr[i] = true;
    }
    int leftCount = n;
    int countNum = 0;
    int index = 0;
    while(leftCount > 1){
    if(arr[index] == true){
    countNum++;
    if(countNum == 3){
    countNum = 0;
    arr[index] = false;
    leftCount--;
    }
    }
    index++;
    if(index == n){
    index = 0;
    }
    }
    for(int i = 0; i < n; i++){
    if(arr[i] == true){
    System.out.println("原排在第" + (i+1) + "位的人留下了。");
    }
    }
    /*
    int[] a = new int[n];
    for(int i = 0; i < n; i++){
    a[i] = i + 1;
    }
    for(int i = 0; i < n; i++){
    System.out.print(a[i] + " ");
    }
    System.out.println();
    int[] b = new int[n];
    for(int i = 0; i < n; i++){
    b[i] = 1;
    }
    int count = 1;
    for(int i = 0; i < 1; i++){
    for(int j = 0; j < n; j++){
    if((count != 3) && (b[j] != 0)){
    b[j] = a[j];
    count++;
    }
    if((count == 3) && (b[j] != 0)){
    b[j] = 0;
    count = 1;
    }
    if((count != 3) && (b[j] == 0)){
    //count--;
    continue;
    }
    if((count == 3) && (b[j] == 0)){
    //count--;
    continue;
    }
    System.out.print(count);
    }
    }
    System.out.println();
    for(int i = 0; i < n; i++){
    System.out.print(b[i] + " ");
    }
    */

    }

    }

  • 相关阅读:
    svn使用教程
    事务的隔离级别--全网最详细
    idea的java类图标C不见,取而代之是J标识,且写代码无提示
    text .global_start 和_start
    transformClassesWithDexForDebug
    RxJava基本流程和lift源码分析
    Rxlifecycle(三):坑
    Rxlifecycle(二):源码解析
    RxJava操作符之Share, Publish, Refcount
    Rxlifecycle(一):使用
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12358775.html
Copyright © 2011-2022 走看看