#40761: 解答 c++


n0970616056@gmail.com (CIOU-HE-CHEN)

學校 : 不指定學校
編號 : 273811
來源 : [111.253.1.171]
最後登入時間 :
2024-06-14 11:55:43
a010. 因數分解 | From: [27.247.62.93] | 發表日期 : 2024-06-11 22:09

#include <iostream> #include <cmath> using namespace std; int main() { int num; cin >> num; bool first = true; for(int factor = 2; factor <= sqrt(num); factor++) { if(num % factor == 0) { int power = 0; while(num % factor == 0) { num /= factor; power++; } if (!first) { cout << " * "; } first = false; if(power == 1) cout << factor; else cout << factor << "^" << power; } } // 如果 num 還不是 1,那麼它本身就是一個質數因數 if (num > 1) { if (!first) { cout << " * "; } cout << num; } cout << endl; return 0; }

 
ZeroJudge Forum