a010

輸入說明 :

一個整數, 大於1 且 小於等於 1000000

輸出說明 :

一個字串

先判斷是不是1,不是的話做下去(題意說大於1)所以其實不用這個判斷,接著對讀進來的數,做從2到那個數本身的mod,要小心一個數本身對同一個因數可能有好幾次,ex:45是 335 三會有兩次,所以寫一個while迴圈判斷,最後面那兩個if else 判斷是因為有四種情況,就是

1.已經找到最大的一個因數了,而且這個因數會出現一次以上

2.已經找到最大的一個因數了,但只出現一次

3.還沒到最大因數,但這個因數會出現一次以上

4.還沒到最大因數,而且這個因數只出現一次

#include<stdio.h>
int main()
{
    int  number,factor,times;

    while(scanf("%d",&number)!=EOF)
    {   
        if(number==1)
            printf("%d\n",1);

        else    
        for(factor=2;factor<=number;factor++)
        {
            times=0;
            while(number%factor==0)
            {
                number/=factor;
                times++;
            }

            if(number==1)
            {
                if(times>1)
                    printf("%d^%d\n",factor,times);
                else if(times==1)
                    printf("%d\n",factor);
            }    
            else 
            {
                if(times>1)
                    printf("%d^%d * ",factor,times);
                else if(times==1)
                    printf("%d * ",factor);
            }    
        }    
    }
    return 0;
}