Joseph Problem With Passwords In Java

问题描述:

编号为1,2,......,n的n个人(每个人的信息有编号、姓名和密码三项)按照顺时针方向围坐一圈,

每个人有且只有一个密码(正整数,密码用随机方式赋值,范围1-15)。一开始任选一个正整数作为报数

上限值,从第一个人开始顺时针方向自1开始报数,报到m时停止报数。报m 的人出列,将他的密码作为新

的m 值,从他在顺时针方向的下一个人开始重新报数,如此下去,直到所有人全部出队为止。设计一个程

序来求出队顺序。

分析:

为解决约瑟夫问题而设计的单向循环链表,应实现如下功能 :
1  添加元素
2  拥有指示当前选中元素的游标
3  游标可循环访问链表中各元素
4  可将游标向前移动指定步数
5  可删除当前游标所指定的元素
*输入:每个人的信息;起始m值
*输出:出队的人信息。

步骤:

1 确定数据类型
2 建立链表
3 实现循环方法
4 输出结果

 1 import  java.util.*;
 2 import  java.io.*;
 3
 4 public class JosephProb {
 5     static int N;
 6
 7     public static void main(String[] args) throws IOException{
 8         CircleLinkList list=new CircleLinkList();
 9         Scanner sc=new Scanner(System.in);
10         System.out.print("请输入参加的总人数N :");
11         N=sc.nextInt();
12
13         int i,data[][]=new int [N][2];
14         String name[]=new String[N];
15         System.out.println("请输入每个人的编号和姓名:");
16         for( i=0;i<N;i++){
17             data[i][0]=sc.nextInt();
18             name[i]=sc.nextLine();
19         }
20         System.out.print("请输入初始密码值(正整数):");
21         int m=sc.nextInt();
22
23         //生成密码
24         List<Integer> l = new ArrayList<Integer>();
25         while(l.size()<N){
26             int j = (int)(Math.random()*15+1);
27                 l.add(j);
28         }
29         //初始化
30         for(i=0;i<N;i++){
31             data[i][1]=l.get(i);
32             list.Init(data[i][0],name[i],data[i][1]);
33         }
34         //出列
35         list.Operation(m);
36         System.out.println("Over!");
37     }
38 }
39 class Person{
40     int number;
41     int password;
42     String names;
43     Person next;
44     public Person (int number,String names,int password){
45         this.number=number;
46         this.names=names;
47         this.password=password;
48         this.next=null;
49     }
50 }
51 class CircleLinkList {
52     Person head;//头结点
53     Person current;
54
55     public boolean isEmpty(){ return head==null; }
56
57     public void Init(int number,String names,int password) {
58         Person tmp=new Person(number,names,password);
59         if(this.isEmpty()){
60             head=tmp;
61             current=head;
62         }
63         else{
64             current.next=tmp;
65             current=tmp;
66         }
67         //最后一个节点的next指向第一个
68         current.next = head;
69     }
70
71     public void Operation(int m){
72         System.out.println("出列人信息:[编号 姓名 密码]");
73         while(current!=current.next){
74             for(int i=1;i<m;i++){    //从1开始报数,到m时停止报数
75                 current=current.next;//指针移动到出列的前一个节点
76             }
77             m=current.next.password;//修改密码为出列人的密码
78             //输出-出队人的信息
79             System.out.print("["+current.next.number+" "+current.next.names+" "+current.next.password+"]\n");
80             current.next=current.next.next;//删除
81         }
82         System.out.println();
83         System.out.println("最后剩余的是  ["+current.number+" "+current.names+" "+current.password+" ]");
84     }
85 }

补充:

可以将出队的人用队列进行存储,并输出。

实现就是新建一个QueueList,添加Insert()输出Dequeue()即可。

代码略。

原文地址:https://www.cnblogs.com/alu5060/p/10645123.html

时间: 2024-08-30 13:01:38

Joseph Problem With Passwords In Java的相关文章

Joseph Problem(详细解法)

前言 这几天学习了队列,于是尝试了一下Joseph Problem,然后一直有一个地方没有通过,终于经点拨后,通过了.对于队列的使用还是加深了印象.建议如果队列的练手可以去尝试解决这个问题. 正文 Joseph Problem(35分) 铺垫: 假设有N个人围成一圈,然后第一个人把第二个人杀了,刀子给第三个人,第三个人把第四个人杀了,以此类推...... 首先注意:难点1:围成一圈,我们会想用什么数据结构呢?这么多人,首先应该想到用数组对不对,可是怎么表示围成一圈呢?重点来了,其实我们可以不断的

Java Secure Socket Extension (JSSE) Reference Guide

Skip to Content Oracle Technology Network Software Downloads Documentation Search Java Secure Socket Extension (JSSE) Reference Guide This guide covers the following topics: Skip Navigation Links Introduction Features and Benefits JSSE Standard API S

java标准-密码用数组比用字符串安全

转载:http://my.oschina.net/jasonultimate/blog/166968 1) Since Strings are immutable in Java if you store password as plain text it will be available in memory until Garbage collector clears it and since String are used in String pool for reusability th

Java 基础知识点(必知必会其二)

1.如何将数字输出为每三位逗号分隔的格式,例如“1,234,467”? 1 package com.Gxjun.problem; 2 3 import java.text.DecimalFormat; 4 import java.util.Scanner; 5 6 7 /* 8 * 如何将数字输出为每三位逗号分隔的格式, 9 * 例如“1,234,467”? 10 * */ 11 12 public class FloatDirve { 13 14 public static void main

Java&#39;s Volatile Keyword

转自 http://tutorials.jenkov.com/java-concurrency/volatile.html The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the com

为啥在java中不要使用char类型

背景 最近项目中遇到一个问题,反复测试才发现问题出在了数据库中,由于使用了 Hibernate 这种ORM框架,因此,在java中写的 EntityBean 就可以直接通过ORM映射到Oracle数据库了,这也导致了很多的问题.当然,查了很多的资料,最终解决了这个问题,并且对Oracle的数据类型也有了一个更深层次的理解.下面是我的译文(原文是英文版的). 译文 要理解char类型,您首先必须了解Unicode编码模式.Unicode的发明克服了传统的字符编码方案的局限性.在Unicode出现之

Core Java Volume I — 3.3. Data Types

3.3. Data TypesJava is a strongly typed language(强类型语音). This means that every variable must have a declared type(每个变量都必须声明类型). There are eight primitive types in Java(Java有8种原始类型). Four of them are integer types; two are floatingpoint number types;

LeetCode Problem 90. Subsets II

python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ """ 思路整理:DFS recursion 1)对nums进行排序以避免nums中重复元素

android导入项目常见问题解决

android导入项目常见问题解决 标签: androideclipseapipropertiescompilertools 2011-12-25 21:52 11268人阅读 评论(7) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 初学android,最近在使用现成的代码的过程中发现直接导入的eclipse项目大都不能直接使用,总结下出现的问题和解决方案: 1.project.properties或default. Properties的问题 有的项目这两个问题不能在ec