Joseph Java problem with passwords

Learning job record

Problem Description:

Joseph problem with passwords: numbered 1,2, ..., n of n individuals sitting clockwise circle, each person has their own number (positive integer), the name and password (positive integer) three data items. Optionally a positive integer beginning as an upper limit value of the number of packets, number of packets from the first person to stop the clockwise direction from the start when a start packet number report m. M newspaper people out of the line, his password as the new value of m, reported the number of restarts from him the next person in a clockwise direction, and so on until everyone all the team so far. Design a program to find a team order.

Input formats:

Enter the number n, then each row of the input information (items separated by commas), then the number of packets input value m.

Output formats:

Dequeued by sequentially outputting each row of the information between the information per row, data items separated by commas

Sample input:

Here we are given a set of inputs. For example:
. 5
, LIU three 3
2 Li Li 5
3 Wu Yong 8
4 money 2
5 Qi Min 4
2

Sample output:

Given here corresponding output. For example:
2, Li Li 5
3, Wu Yong, 8-
5 Qi Min 4
4 money, 2
, LIU three 3
completed according to the teaching video bilibili still Silicon Valley Java data structures

Code:

import java.util.Scanner;

public class Main3 {

    public static void main(String[] args) {

        Circle circle=new Circle();
        circle.addNode();
        circle.showPerson();
    }


}
class Circle{
    private Node first=null;
    Scanner sc=new Scanner(System .in);
    int n =sc.nextInt();

    public  void addNode() {
        if(n<1){
            return;
        }
        Node current =null;
        for(int i=1;i<=n;i++){
            String temp=sc.next();
            String [] s=temp.split(",");
            int num = Integer.parseInt(s[0]);
            String nm = s[1];
            int c =Integer.parseInt(s[2]);

            Node person = new Node(num,nm,c);
            if(i==1){
                first=person;
                first.next=first;
                current=first;
            }else{
                current.next=person;
                person.next =first;
                current=person;
            }

        }


    }
    public void showPerson(){
        int m=sc.nextInt();

        if(first==null){
            return;
        }
        Node helper =first;
        while(true){
            if(helper.next==first){
                break;
            }
            helper=helper.next;
        }
        int count=0;
        while(true){

            for(int j=0;j<m-1;j++){
                first=first.next;
                helper=helper.next;
            }

            count++;

            System.out.println(first.number +","+first.name+","+first.code );
            if(n==1){
                break;
            }

            m=first.code;
            if(m>(n-count))
                m=m%(n-count);
            if(m==0){
                m=(n-count);
            }
            first=first.next;
            helper.next=first;
            if(helper==first){
                System.out.print(first.number +","+first.name+","+first.code );
                break;
            }
        }

    }
}
class Node{
    int number;
    int code;
    String name;
    Node next;

    public Node(int number ,String name ,int code){
        this.name =name;
        this.number =number;
        this.code =code;
        this.next=null;
    }
}

J77
Released seven original articles · won praise 2 · Views 101

Guess you like

Origin blog.csdn.net/weixin_46053704/article/details/105251004