0 pts 求调
查看原帖
0 pts 求调
1060003
xiyu000楼主2024/11/9 15:08
#include<bits/stdc++.h>
#define long long int
using namespace std;
bool is_prime(int n){
	if(n<2){
		return 0 ;
	}
	for(int i = 2 ; i <= n/i ; i++){
		if(n%i==0){
			return 0 ;
		}
	}
	return 1 ;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	int n ;
	cin >> n ;
	if(is_prime(n)){
		cout << n ;
		return 0 ;
	}
	bool flag = 1 ;
	for(int i = 2 ; i <= n/i ; i++){
		int cnt = 0 ;
		if(is_prime(i)){
			while(n%i==0){
				cnt++;
				n/=i;
			}
			if(cnt>1){
				if(flag){
					cout << i << "^" << cnt ;
					flag = 0 ;
				}
				else{
					cout << " * " << i << "^" << cnt ;
				}
			}
			else if(cnt==1){
				if(flag){
					flag = 0 ;
					cout << i ;
				}
				else{
					cout << " * " << i ;
				}
			}
		}
	}
	if(n>1){
		cout << " * " << n ;
	}
	return 0 ;
}

2024/11/9 15:08
加载中...