zoukankan      html  css  js  c++  java
  • 报数游戏_数组解决方法

    
    
    package com.cn;

    /**
    * 100个人围成一圈,每个人有一个编码,编号从1开始到100.他们从1开始依次报数,报到为M的人自动退出圈圈,然后下一个人接着从1开始报数,
    * 直到剩余的人数小于M。请问最后剩余的人在原先的编号为多少?例如输入M=3时,输出为:“58,91”,输入M=4时,输出为: “34,45, 97”。
    * 如果m小于等于1, 则输出“ERROR!”;
    * 如果m大于等于100,则输出“ERROR!”;
    */
    public class NumGame {
    // public static int getSum(int[] arr) {
    // int sum = 0;
    // for (int i = 0; i < arr.length; i++) {
    // if (arr[i] == 0) {
    // sum++;
    // }
    // }
    // return sum;
    // }
    static int sum_0 =100;
    public static int[] playame(int m) {
    int[] persons = new int[100]; //初始化为0 表示均没有跳出圈子
    int index = 1;
    //求出没有跳出圈的个数
    // while ( getSum(persons>=m)){
    while (sum_0>=m) {
    for (int i = 0; i < persons.length; i++) {
    if (persons[i] == 0) { //在圈内
    if (index % m == 0) {
    persons[i] = 1;
    sum_0 --;
    }
    index++;
    }
    }
    }
    return persons;
    }

    public static void main(String[] args) {
    int[] result = playame(4);
    for (int i = 0; i < result.length; i++) {
    System.out.print(result[i] + ",");
    }
    System.out.println();
    System.out.println("结果:");
    for (int i = 0; i < result.length; i++) {
    if(result[i]==0){
    System.out.print((i+1)+",");
    }
    }
    }

    }

    运行的结果:
     


    不要因为怕被玫瑰的刺伤到你,就不敢去摘玫瑰。
  • 相关阅读:
    MySQL—2、B-Tree,B+Tree,聚集索引,非聚集索引
    transient关键字的作用及使用方法
    通过Executors创建线程池和注意小点
    @Validated校验
    Elasticsearch-head插件的安装与配置
    bayaim_java_入门到精通_听课笔记bayaim_20181120
    bayaim_hadoop2_hdfs_20181107
    bayaim_hadoop1_2.2.0伪分布式搭建
    bayaim_hadoop 开篇 0.0
    bayaim_linux_configure_oracle
  • 原文地址:https://www.cnblogs.com/gccwelcome/p/12786023.html
Copyright © 2011-2022 走看看