问题描述
设计算法,用户输入合数,程序输出若个素数的乘积。例如,输入6,输出2*3。输入20,输出2*2*5。
样例
与上面的样例输入对应的输出。
例:
数据规模和约定
输入数据中每一个数在int表示范围内。
#include <iostream>
#include <cstdio>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <stack>
#include <list>
using namespace std;
typedef long long ll;
bool isPrime(int n){
bool flag = false;
int i;
for(i=2;i<=n/2;i++){
if(n%i == 0){
break;
}
}
if(i == n/2+1){
flag = true;
}
return flag;
}
int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
int m = n;
int cnt = 0;
for(int i=2;i<=n/2;i++){
if(m==0){
break;
}
if(isPrime(i) && m>0 && m%i==0){
if(cnt)
cout<<"*";
cout<<i;
m=m/i;
i--;
cnt++;
}
}
if(isPrime(n)){
cout<<n<<endl;
}
return 0;
}