#23315: 我是全部算完再討論狀況


s1092923@g.ncyu.edu.tw (安)

學校 : 不指定學校
編號 : 132782
來源 : [114.39.171.74]
最後登入時間 :
2021-07-22 18:39:23
a010. 因數分解 | From: [120.113.184.171] | 發表日期 : 2020-11-07 00:32

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int num;
    cin>>num;
    int count;
    
        for(int i=2; i<=num; i++)
        {
            count=0;
            while(num%i==0)
            {
                num/=i;
                count+=1;
            }
            if(count>1) //有次方以上
                {
                    if(num==1) //表示數已經被全部因數分解完了,所以後面不加*
                        cout<<i<<"^"<<count;
                    else
                        cout<<i<<"^"<<count<<" * ";
                }
            else if (count==1)
                {
                    if(num==1)
                        cout<<i;
                    else
                        cout<<i<<" * ";
                }
            else //其他狀況如count=0就回去迴圈繼續計算
                continue;
        }
}
 
ZeroJudge Forum