簡體   English   中英

顯示質因數提示用戶使用StackOfIntegers Java類進行整數運算

[英]Display Prime Factors Prompting User for Integer Using StackOfIntegers Java Class

因此,我已經完成了這項工作,並且一直在努力尋求幫助或反饋,歡迎您:)

問題

(顯示主要因子)編寫一個程序,提示用戶輸入正整數,並以降序顯示所有最小因子。 使用StackOfIntergers類。

這是我到目前為止的內容,程序可以編譯並運行,但是得到質數而不是質因數。

package primefactors;
import java.util.Scanner;

public class PrimeFactors {
public static void main(String[] args) {
System.out.print("Enter a positive number: ");
Scanner scanner = new Scanner (System.in);
final int number = scanner.nextInt();
int count = 0;
StackOfIntegers stack = new StackOfIntegers();    

// Repeatedly find prime factors
for (int i = 2; i <= number; i++)
  if (isPrime(i)) {
    stack.push(i);
    count++; // Increase the prime number count
  }

// Print the prime factors
System.out.println("The prime numbers are \n");
final int NUMBER_PER_LINE = 10;

while (!stack.empty()) {
  System.out.print(stack.pop() + " ");

  if (stack.getSize() % NUMBER_PER_LINE == 0)
    System.out.println(); // advance to the new line
}
}

public static boolean isPrime(int number) {
// Assume the number is prime
boolean isPrime = true;

for (int divisor = 2; divisor <= number / 2; divisor++) {
  //If true, the number is not prime
  if (number % divisor == 0) {
    // Set isPrime to false, if the number is not prime
    isPrime = false;
    break; // Exit the for loop
  }
}

return isPrime;
}
}

* Update#2 *所以我只需要使主要因素起作用,所以這是在研究了更多內容和編碼后最終得出的結果。

有用。 現在,我需要讓程序在一個漂亮的列表中同時顯示素數和素數。

感謝您的反饋和建議。

import java.text.MessageFormat;
import java.util.Scanner;

public class PrimeFactor {
    public static void main(String[] args) {
    System.out.print("Enter a positive number: ");
    Scanner scanner = new Scanner (System.in);
    int number = scanner.nextInt();
    int count;
    StackOfIntegers stack = new StackOfIntegers();    
    for (int i = 2; i<=(number); i++) {
        count = 0;
        while (number % i == 0) {
            number /= i;
            count++;
        }
            if (count == 0) continue;
              stack.push(i);
              count++;
    }
    System.out.println("The prime factors are \n");
    final int NUMBER_PER_LINE = 10;

     while (!stack.empty()) {
     System.out.print(MessageFormat.format("{0} ", stack.pop()));

    if (stack.getSize() % NUMBER_PER_LINE == 0)
     System.out.println(); // advance to the new line
    }
  }
} 

StackOfInterger類

public class StackOfIntegers {
private int[] elements;
private int size;

/** Construct a stack with the default capacity 16 */
  public StackOfIntegers() {
    this(16);
 }

 /** Construct a stack with the specified maximum capacity */
 public StackOfIntegers(int capacity) {
   elements = new int[capacity];
 }

 /** Push a new integer into the top of the stack */
 public int push(int value) {
   if (size >= elements.length) {
     int[] temp = new int[elements.length * 2];
     System.arraycopy(elements, 0, temp, 0, elements.length);
     elements = temp;
   }

   return elements[size++] = value;
 }

 /** Return and remove the top element from the stack */
 public int pop() {
   return elements[--size];
 }   

 /** Return the top element from the stack */
 public int peek() {
   return elements[size - 1];
 }

 //whether the stack is empty */
 public boolean empty() {
   return size == 0;
 }

  /** Return the number of elements in the stack */
 public int getSize() {
   return size;
 }
 }

如描述的那樣工作。 當我用20運行它時,得到以下輸出:19 17 13 11 7 5 3 2

這些都是質數。 我不確定您為什么期望5,3,2,2。

我相信下一步是遍歷所有這些素數,看看它們是否是20的素數。應該是5和2。為此,您應該遍歷列表並查看輸入%(即模數)您要測試的數字等於0。

嘗試此操作,如果有5個素數因子,它將返回給定數的5個最小素數因子。 如果要顯示兩個列表,一個用於質數,一個用於質因數,為什么不創建兩個堆棧並分別顯示。

公共課程Temp2 {

public static void main(String[] args) 
{
    int number, integer, count = 0;
    StackOfIntegers stack = new StackOfIntegers();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a positive integer: ");
    integer = input.nextInt();

    //Find prime factors
    for (number = 2; number < integer; number++) 
    {
        if (isPrime(number) && integer % number == 0)
        { 
            stack.push(number);
            count++;
            if(count == 5) break;
        }
    }

    // Print the smallest prime factors in decreasing order
    System.out.println("The smallest prime factors of " + integer + " are ");

    while (!stack.empty()) 
    {
        System.out.print(stack.pop() + ", ");
    }
    System.out.println(); //Advance to the new line
}//ENDMAIN

public static boolean isPrime(int number) 
{
    for (int divisor = 2; divisor <= number / 2; divisor++) 
    {
        //If true, the number is not prime, return false
        if (number % divisor == 0) 
            return false;
    }
    return true;
}

} // ENDCLASS

可以在以下位置找到有關此分配問題的可行解決方案: http : //www.besteduweb.com/assignment-solution-using-stack-of-integers-class.html

這是100%可行的解決方案,對我有用。 推薦100%。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM