亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Eratosthenes篩子(使用LINKEDLIST)

Eratosthenes篩子(使用LINKEDLIST)

慕娘9325324 2022-08-03 12:47:08
我試圖弄清楚我如何操縱列表,以便找到用戶提供的所有素數,我有一個列表步驟,我試圖遵循哪些是創建并填充可能的素數列表基本上是一個數組列表,其中包含所有數字,直到提供的數字,我已經完成了該部分為素數創建列表我有那部分下來雖然仍然有可能的數字也就是說,雖然可能的素數列表不是空的。將可能列表中的第一個數字添加到素數列表中把那部分也放下了從可能的素數列表中刪除它及其倍數這就是我開始發呆的地方,我以為我有那個部分,但我得到了一個錯誤,我不知道為什么。打印素數打印素數列表,基本上只是System.out.println(素數);這是我目前的代碼import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Scanner;public class Sieve {public static void main(String[] args) {    int maxNum;    String howItsGoing, greatDetail;    Scanner scnr = new Scanner(System.in);    Scanner scnr2 = new Scanner(System.in);    // get upper limit    System.out.print("What's the biggest number I should check? ");    maxNum = scnr.nextInt();    // check for verbose mode    System.out.print("Shall I tell you how it's going? ");    howItsGoing = scnr2.nextLine().toUpperCase();    System.out.print("Shall I tell you in great detail? ");    greatDetail = scnr2.nextLine().toUpperCase();    // create and fill list of possible primes    List<Integer> nums = new LinkedList<>();    for (int i = 2; i <= maxNum; i++) {        nums.add(i);             }    // create list for the primes    List<Integer> primes = new ArrayList<>();    // while there are still possible numbers    // add the first number from the list of possibles to the list of primes    for(int i=2; i<=maxNum; i++) {        if(2 % i == 0) {            nums.remove((Integer) i);            primes.add((Integer) i);        }    }        // remove it and its multiples from the list of possible primes    // print the prime numbers    System.out.println("Primes up to " + maxNum);    System.out.println(nums);    System.out.println(primes);}}忽略 howItsGoing 字符串和 greatDetail,我稍后會添加它們。我如何讓這個程序正常工作,其他每個問題都有布爾數組中的解決方案,這不是我想要的。有什么想法嗎?輸出What's the biggest number I should check? 9Shall I tell you how it's going? nShall I tell you in great detail? nPrimes up to 9[3, 4, 5, 6, 7, 8, 9][2]
查看完整描述

2 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

弄清楚了,這就是代碼應該是什么樣子的


import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Scanner;


public class Sieve {

public static void main(String[] args) {

    int maxNum;

    boolean possible = true;

    String howItsGoing, greatDetail;

    Scanner scnr = new Scanner(System.in);

    Scanner scnr2 = new Scanner(System.in);


    // get upper limit

    System.out.print("What's the biggest number I should check? ");

    maxNum = scnr.nextInt();


    // check for verbose mode

    System.out.print("Shall I tell you how it's going? ");

    howItsGoing = scnr2.nextLine().toUpperCase();


    if (howItsGoing.startsWith("N")) {

        greatDetail = "N";

    } else {

        System.out.print("Shall I tell you in great detail? ");

        greatDetail = scnr2.nextLine().toUpperCase();

    }


    // create and fill list of possible primes

    List<Integer> nums = new LinkedList<>();


    for (int i = 2; i <= maxNum; i++) {

        nums.add(i);

    }


    // create list for the primes

    List<Integer> primes = new ArrayList<>();

    // while there are still possible numbers

    while (possible) {

        // add the first number from the list of possibles to the list of

        // primes


        primes.add(nums.get(0));


        if (howItsGoing.startsWith("Y")) {

            System.out.println();

            System.out.println();

            System.out.print("Found prime: ");

            System.out.printf("%1d ", nums.get(0));

            System.out.println();

        }

        // remove it and its multiples from the list of possible primes

        int divisor = nums.get(0);


        nums.remove(nums.get(0));


        for (int i = divisor; i <= maxNum; i++) {


            if (i % divisor == 0) {

                if (greatDetail.startsWith("Y")) {

                    System.out.println(

                            "    Removing " + i + " from possibles");

                }

                nums.remove((Integer) i);

            }


        }

        System.out.println();

        if (nums.size() > 0) {

            if (howItsGoing.startsWith("Y")) {

                System.out.print("Possibles:\n    ");

                for (int i = 0; i < nums.size(); i++) {

                    System.out.printf("%6d ", nums.get(i));

                }


            }

        }

        if (nums.size() < 1) {

            possible = false;

        }

    }


    // print the prime numbers

    System.out.println();

    System.out.println("Primes up to " + maxNum);

    for (int i = 0; i < primes.size(); i++) {

        System.out.printf("%6d ", primes.get(i));

    }


}

}

輸出


What's the biggest number I should check? 20

Shall I tell you how it's going? yes

Shall I tell you in great detail? yes



Found prime: 2 

Removing 2 from possibles

Removing 4 from possibles

Removing 6 from possibles

Removing 8 from possibles

Removing 10 from possibles

Removing 12 from possibles

Removing 14 from possibles

Removing 16 from possibles

Removing 18 from possibles

Removing 20 from possibles


Possibles:

     3      5      7      9     11     13     15     17     19 


Found prime: 3 

Removing 3 from possibles

Removing 6 from possibles

Removing 9 from possibles

Removing 12 from possibles

Removing 15 from possibles

Removing 18 from possibles


Possibles:

     5      7     11     13     17     19 


Found prime: 5 

Removing 5 from possibles

Removing 10 from possibles

Removing 15 from possibles

Removing 20 from possibles


Possibles:

     7     11     13     17     19 


Found prime: 7 

Removing 7 from possibles

Removing 14 from possibles


Possibles:

    11     13     17     19 


Found prime: 11 

Removing 11 from possibles


Possibles:

    13     17     19 


Found prime: 13 

Removing 13 from possibles


Possibles:

    17     19 


Found prime: 17 

Removing 17 from possibles


Possibles:

    19 


Found prime: 19 

Removing 19 from possibles



Primes up to 20

 2      3      5      7     11     13     17     19 


查看完整回答
反對 回復 2022-08-03
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

我已經突出顯示了錯誤:


    // remove it and its multiples from the list of possible primes

    for(int i=0; i<=maxNum; i++) {

        if(i % 2 == 0) {  // first bug

            nums.remove(i); // second bug

        }

    }

第一個錯誤是 i % 2 == 0 檢查 i 是否是 2 的倍數,但它應該檢查它是否是當前素數的倍數。


第二個錯誤是


nums.remove(i);

是模棱兩可的。 聲明兩種不同的 remove 方法:刪除列表中的第 i 個條目,以及 ,刪除等于 的條目。由于 a 可以轉換為 ,因此兩種方法都與參數的類型匹配,但與參數類型更接近,因此被使用,而您可能希望刪除(Integer)...您可以通過轉換參數來解決此問題:ArrayList<Integer>remove(int)remove(Integer)iintIntegerremove(int)


nums.remove((Integer) i);

這應該使代碼正常工作,但你很快就會意識到代碼相當慢。這是因為實際上是一個相當昂貴的操作,因為它涉及迭代整個操作,直到找到要刪除。也就是說,對于您淘汰的每個主要候選人,您都會與所有其他主要候選人進行交互。由于其中有很多,因此您的代碼將非常慢。remove(Integer)ListInteger


解決方案是選擇一個支持更有效地按值刪除的數據結構。這就是為什么每個人在實現此算法時使用a的原因。boolean[]


查看完整回答
反對 回復 2022-08-03
  • 2 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號