a010.
因數分解
| From: [219.91.1.199] |
發表日期
:
2022-11-22 21:40
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
int x, i = 2;
vector <int> y; //動態陣列
cin >> x;
while ( x != 1 ){ //分析所有因數
if ( x % i == 0 ){
y.push_back(i);
x = x / i;
}
else{
i++;
}
}
sort( begin(y), end(y)); //排列
int m = 1;
for ( int k = 0 ; k < y.size() ; k++ ){ //歸納結果
if ( y[k] == y[k+1] ){
m++ ;
}
else if( m == 1 ){
cout << y[k] ;
if ( k != ( y.size() - 1 ) ){
cout << " * ";
}
m = 1;
}
else{
cout << y[k] << "^" << m ;
if ( k != ( y.size() - 1 ) ){
cout << " * ";
}
m = 1;
}
}
return 0;
}