40分,wa
查看原帖
40分,wa
1127424
damuzhi楼主2024/3/11 20:55
#include <bits/stdc++.h>

using namespace std;



bool is_prime(long long a){
    if(a < 2){
        return 0;
    }
    for(long long i = 2; i <= sqrt(a); i++){
        if(a % i == 0){
            return 0;
        }
    }
    return 1;
}

// short num[1000000];

struct yi{
    long long yi;
    long long n;
};

bool cmp(yi a,yi b){
    if(a.yi < b.yi){
        return true;
    }
    return false;
}

vector<yi> v;

int main() {
    long long a;
    cin >> a;
    if(is_prime(a)){
        cout << a << endl;
        return 0;
    }
    long long c = a;
        for(long long i = 2; i <= sqrt(a)+1; i++){
            if(c%i == 0){
                    yi tmp;
                    tmp.n = 0;
                    tmp.yi = i;
                    v.push_back(tmp);
                    while(c % i == 0 && c > 1){
                        v[v.size()-1].n++;
                        c /= i;
                    }
            }
        }
    bool fir = 0;
    sort(v.begin(),v.end(),cmp);
    for(vector<yi>::iterator i = v.begin(); i != v.end(); i++){
        if((*i).n > 0){
            if(!fir){
                if((*i).n > 1){
                    cout << (*i).yi << "^" << (*i).n;
                }
                else{
                    cout << (*i).yi;
                }
                fir = 1;
            }
            else{
                if((*i).n > 1){
                    cout << " * " << (*i).yi << "^" << (*i).n;
                }
                else{
                    cout << " * " << (*i).yi;
                }
            }
        }
    }
    cout << endl;
    return 0;
}
2024/3/11 20:55
加载中...