#21387: 求解 WA35%


Easonsfriend (去寫./Problems?ownerid=89827)

學校 : 不指定學校
編號 : 89827
來源 : [106.105.188.90]
最後登入時間 :
2024-04-23 23:15:35
e912. n! 的因數分解 -- it's david | From: [106.105.188.90] | 發表日期 : 2020-05-24 23:13

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

 

int main() {

    ios::sync_with_stdio(false);cin.tie(0);

    int prime[26]={

        2,3,5,7,11,13,17,19,23,29, 31,37,41,43,47,53,59,61,67,71, 73,79,83,89,97,101

    };

    int n;

    while(cin>>n){

        int x[26]={0};

        vector<int>other;

        for(int i=2;i<=n;i++){

            int t=i;

            for(int j=0;j<26;j++){

                if(t%prime[j]==0){

                    while(t%prime[j]==0){

                        x[j]++;

                        t/=prime[j];

                    }

                }

            }

            if(t!=1)other.push_back(t);

        }

        sort(other.begin(),other.end());

        cout<<n<<"! = ";

        int c=0;

        for(int i=0;i<26;i++){

            if(x[i]!=0){

                if(c)cout<<" * ";

                cout<<prime[i]<<"^"<<x[i];

                c=1;

            }

        }

        for(int i=0;i<other.size();i++){

            cout<<" * "<<other[i]<<"^";

            int cnt=1;

            while(other[i]==other[i+1]){

                cnt++;

                i++;

            }

            cout<<cnt;

        }

        cout<<'\n';

    }

    return 0;

 

}

 

 

 

自己測試都是對的

 
ZeroJudge Forum