回覆列表
  • 1 # 錢布斯

    package com.leejiliang.HomeworkDemo2;

    import java.util.ArrayList;

    import java.util.List;

    import java.util.Scanner;

    public class MinimalElements {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("請輸入一個整數:");

    int inner = input.nextInt();

    Stack stack = getElements(inner);

    System.out.println(inner + "的所有最小銀子為:" + stack);

    }

    /*

    * 此方法摘抄自網上

    */

    public static Stack getElements(int val) {

    Stack stack = new Stack();

    int i;

    for (i = 2; i <= val;)// 注意是小於等於

    {

    if (val % i == 0) {

    stack.getList().add(i);

    val /= i;// 記得每次進行除i

    } else

    i++;// 因為求的是最小因子,所以只有在i小的時候不能除盡,才進行i加一的操作,

    }

    return stack;

    }

    }

    class Stack {

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

    public List<Integer> getList() {

    return list;

    }

    public void setList(List<Integer> list) {

    this.list = list;

    }

    @Override

    public String toString() {

    String s = "{";

    for (Integer i : list) {

    s += i + ",";

    }

    return s.substring(0, s.length() - 1) + "}";

    }

    }

  • 中秋節和大豐收的關聯?
  • 怎麼看手機是否支援aptx?