国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Josephus環類問題,java實現

2019-11-14 15:10:08
字體:
來源:轉載
供稿:網友

  寫出一個雙向的循環鏈表,弄一個計數器,我定義的是到三的時候,自動刪除當前節點,很簡單。

  

package Com;import java.util.Scanner;/* * 約瑟夫環問題,有n個人組成的圈,數到3的那個人出列,下個人繼續從一開始 */public class Josephus {        public static void main(String[] args) {        Scanner s = new Scanner(System.in);        int n = Integer.parseInt(s.nextLine());        Node first = new Josephus().startRun(n );        int count = 1;        while(first.next != first) {            first = first.next;            count++;            if(count == 3) {                first.PRevious.next = first.next;                first.next.previous = first.previous;                first = first.next;                count = 1;            }        }        System.out.println("最后剩下來的數字為:"+first.n);    }        public Node startRun(int n) {        Node first = new Node();        first.previous = null;        first.n = n ;   //這里給鏈表賦值,倒敘        Node current = first;        Node last = first;        while((--n)>0) {            current.next = new Node();            current = current.next;            current.n = n;            current.previous = last;            last = current;        }        current.next = first;        first.previous = current;        return first;    }    class Node {        int n ;         Node next;        Node previous;    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西丰县| 荔浦县| 喀喇| 南皮县| 青岛市| 洞头县| 西青区| 庆云县| 洱源县| 余干县| 乌鲁木齐市| 通海县| 珲春市| 平乐县| 阜宁县| 沂南县| 定南县| 上饶县| 德格县| 郑州市| 白山市| 永嘉县| 施秉县| 太湖县| 绥德县| 松潘县| 隆化县| 黑龙江省| 自治县| 康保县| 敖汉旗| 绥化市| 田阳县| 乡城县| 新泰市| 浙江省| 汨罗市| 安塞县| 饶阳县| 任丘市| 称多县|