Java中怎么實(shí)現(xiàn)一個(gè)約瑟夫環(huán)

今天就跟大家聊聊有關(guān)Java中怎么實(shí)現(xiàn)一個(gè)約瑟夫環(huán),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)專注于井陘礦網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供井陘礦營銷型網(wǎng)站建設(shè),井陘礦網(wǎng)站制作、井陘礦網(wǎng)頁設(shè)計(jì)、井陘礦網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造井陘礦網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供井陘礦網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

什么是約瑟夫環(huán)呢?

約瑟夫環(huán)是一個(gè)數(shù)學(xué)的應(yīng)用問題:已知n個(gè)人(以編號(hào)1,2,3...n分別表示)圍坐在一張圓桌周圍。從編號(hào)為k的人開始報(bào)數(shù),數(shù)到m的那個(gè)人出列;他的下一個(gè)人又從1開始報(bào)數(shù),數(shù)到m的那個(gè)人又出列;依此規(guī)律重復(fù)下去,直到圓桌周圍的人全部出列。

我們用程序說話,實(shí)現(xiàn)約瑟夫環(huán)

import java.util.Scanner;  public class Josephus {     private static class Node {         public int no;// 編號(hào)         public Node next;// 下一個(gè)節(jié)點(diǎn)          public Node(int no) {             this.no = no;         }     }      public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         System.out.print("請(qǐng)輸入總?cè)藬?shù):");         int totalNum = scanner.nextInt();         System.out.print("請(qǐng)輸入報(bào)數(shù)的大?。?quot;);         int cycleNum = scanner.nextInt();         Node header = new Node(1);         Node pointer = header;         for (int i = 2; i <= totalNum; i++) {             pointer.next = new Node(i);             pointer = pointer.next;         }         pointer.next = header;         // 初始化環(huán)形鏈表結(jié)束         System.out.println("以下是出列的順序:");         while (pointer != pointer.next) {             for (int i = 1; i < cycleNum; i++) {                 pointer = pointer.next;             }             System.out.println(pointer.next.no);             pointer.next = pointer.next.next;         }         System.out.println(pointer.next.no);     } }

看完上述內(nèi)容,你們對(duì)Java中怎么實(shí)現(xiàn)一個(gè)約瑟夫環(huán)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前名稱:Java中怎么實(shí)現(xiàn)一個(gè)約瑟夫環(huán)
地址分享:http://bm7419.com/article36/igdpsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、微信公眾號(hào)、網(wǎng)站設(shè)計(jì)公司移動(dòng)網(wǎng)站建設(shè)、、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)