六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 25|回复: 0

Poj3750 约瑟夫循环

[复制链接]

升级  18.67%

20

主题

20

主题

20

主题

秀才

Rank: 2

积分
78
 楼主| 发表于 2013-2-3 10:56:24 | 显示全部楼层 |阅读模式
package easy;import java.io.BufferedInputStream;import java.util.LinkedList;import java.util.Scanner;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** *Poj3750 easy 约瑟夫循环 * @author NC */public class Poj3750 {    public static void main(String[] args) {        Scanner scanner = new Scanner(new BufferedInputStream(System.in));        //用nextInt的话,与下文的nextLine会有点冲突,会报异常,还是统一用一个方法就好        int n = Integer.parseInt(scanner.nextLine());        LinkedList<String> queue = new LinkedList();        for (int i = 0; i < n; i++) {            String name = scanner.nextLine();            queue.add(name.trim());        }        String ws = scanner.nextLine();        String[] wss = ws.split(",");        int w = Integer.parseInt(wss[0]);        int s = Integer.parseInt(wss[1]);        int count = 0;//找出第w个人,从第w个人开始报数        while (true) {            count++;            if (count == n + 1) {                count = 1;            }            if (count == w) {                break;            }            String person = queue.removeFirst();            queue.add(person);        }//从第w个开始报数,报到s的出队        count = 0;        while (true) {            //从1开始报数            count++;            if (count == n + 1) {                count = 1;            }            if (queue.isEmpty()) {                break;            }            //出来一个人报数            String person = queue.removeFirst();            if (count == s) {                System.out.println(person);                count = 0;//出完队后,重新报数            } else {                queue.add(person);            }        }    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表