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] + " ");
    }
    */

    }

    }

  • 相关阅读:
    lua中的冒号和点
    NuGet使用简要说明
    C#浅谈类实体与DataTable执行效率
    win8 使用技巧
    Bitnami Redmine 中文附件名 报错修复
    Markdown 测试
    几种常用网页返回顶部代码
    文字超出隐藏并显示省略号
    手机正则写法
    安卓内存管理相关关键字
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12358775.html
Copyright © 2011-2022 走看看