Those years, learn together Java 7-2

Source: Internet
Author: User

/**

* 7-2

* Joseph question

* Programmed by the keyboard to receive a value of n, from the number of 1 people began to count,

* The man who counted to 7

* What is the number of the last person left in the output */

import java.util.*;p Ublic class test {public static void main (String[]  args) {System.out.println ("Enter a value of n from the keyboard"); Scanner input = new scanner (system.in); Int num = input.nextint (); node[] array = new node[num+1];//declares n+1 node//initialization node for  (int i = 1; i  < num; i++) Array[i] = new node (1, i+1);//Set the last node to point to the first node array[num] =  new node (1, 1);//The game starts with a node pointing to the first person int k = num;for  (int i = 0;  i < num-1; i++)//keep circulating until the last person is left {for  (int j = 0; ;) {if  (j &NBSP;&LT;&NBSP;7)//number to 7th person {k = array[k].readnext (); J += array[k].readdata ();} Elsebreak;} Array[k].setdata (0);} Enter the last remaining person for  (int i = 1; i < num+1; i++) if  (array[i].readData ()  == 1) System.out.print (" "  + i +  "  ");}} class node{private int m_data;private int m_next;//Constructor node  (Int data,  int next) {M_data = data;m_next = next;} Sets the data value of the node void setdata  (int data) {m_data = data;} Reads the data value of the node int readdata  () {return m_data;} Sets the node's point to void setnext  (Int next) {m_next = next;} Read node to int readnext  () {return m_next;}}


Those years, learn together Java 7-2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.