# 解題紀錄 ## 題目:a010. 因數分解 ## 📙 題目描述 各位在國小時都學過因數分解,都瞭解怎麼樣用紙筆計算出結果,現在由你來敎電腦做因數分解。 因數分解就是把一個數字,切分為數個質數的乘積,如 12=2^2 * 3 其中, 次方的符號以 ^ 來表示 ```txt 輸入說明 輸入共一行。每行包含一個整數,符合 大於1 且 小於等於 100000000 ``` ```txt 輸出說明 針對每一行輸入整數輸出一個因數分解字串 ``` ```txt 範例輸入 #1 20 範例輸出 #1 2^2 * 5 ``` ```txt 範例輸入 #2 17 範例輸出 #2 17 ``` ```txt 範例輸入 #3 999997 範例輸出 #3 757 * 1321 ``` --- ## ✒️ 解題思路 因為我是新手所以就不討論其他演算法了(質數問題讓人又愛🥰又恨) 單純對每一個數進行試除法😑 --- ## 💻 C++解法 ```cpp #include <bits/stdc++.h> using namespace std; class Solution { private: int n; public: Solution() { cin >> n; } void ans() { for(int i = 2;i <= n;++i){ int power {1}; if(n % i == 0) { n /= i; while(n % i == 0) {//判斷是否來能繼續除 n /= i; power++; } if(power == 1) cout << i; else cout << i << "^" << power; if(n != 1) cout << " * "; } } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solution s; s.ans(); return 0; } ```