---
title: "JAVA经典算法40题(19)"
author: "方XiRui"
date: "2012-12-20T12:37:12Z"
tags: []
categories: ["编程语言"]
slug: "java-40-19"
draft: false
lastmod: "2012-12-20T12:37:12Z"
id: 1750905
url: "https://fxrde.iteye.com/blog/1750905"
---

【程序37】  
题目：有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。

```
import java.util.Scanner;
public class Ex37 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        boolean[] arr = new boolean[n];
        for(int i=0; i
            arr[i] = true;//下标为TRUE时说明还在圈里
        }
        int leftCount = n;
        int countNum = 0;
        int index = 0;
        while(leftCount > 1) {
            if(arr[index] == true) {//当在圈里时
                countNum ++; //报数递加
                if(countNum == 3) {//报道3时
                    countNum =0;//从零开始继续报数
                    arr[index] = false;//此人退出圈子
                    leftCount --;//剩余人数减一
                }
            }
            index ++;//每报一次数，下标加一
            if(index == n) {//是循环数数，当下标大于n时，说明已经数了一圈，
                index = 0;//将下标设为零重新开始。
            }
        }
        for(int i=0; i
            if(arr[i] == true) {
                System.out.println(i);
            }
        }
    }
}
```

 【程序38】  
题目：写一个函数，求一个字符串的长度，在main函数中输入字符串，并输出其长度。

```
import java.util.Scanner;
public class Ex38 {
    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String mys= s.next();
        System.out.println(str_len(mys));
    }
    public static int str_len(String x){
        return x.length();
    }
}
```