2010年8月1日 星期日

高中 a010: 因數分解



想看題目請點我

#include <iostream>
using namespace std;

int main (){
    int input ;
    while(cin >> input)
    {
        if((input > 1) && (input <= 1000000) )
        {
             int flag = 0 ; // 判斷是否為第一個數字來給定 * 的符號 
             for(int i = 2 ; i <= input ; i++ )
             {
                 int pow = 0 ;
                 while((input % i )== 0)
                 {
                     input /= i;
                     pow++ ;
                 }
                 if (pow==0) continue;
                 if(pow > 1)
                 {
                     if(flag ==0 )
                     {
                         cout << i << "^" << pow ;
                         flag = 1 ;
                     }
                     else cout << " * " << i << "^" << pow ;
                 }
                 else
                 {
                     if(flag == 0)
                     {
                         cout << i ;
                         flag = 1 ;
                     }
                     else 
                         cout << " * " << i ;
                 }
             }
             cout << endl ;
        } 
    }
}