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

    }

    }

  • 相关阅读:
    CentOS7安装(三)- 配置阿里云yum源
    OSQA的配置
    MySQL学习 (三) Limit-Distinct-Union
    MySQL学习(二)-字段类型及约束
    MySQL学习(一)-基本知识
    Python闭包
    软件测试面试常考点
    人生感悟
    常用的Linux命令
    细说php一些常见的知识点
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12358775.html
Copyright © 2011-2022 走看看