#7827: 一直WA


a0323456897 (小豬)


#include<iostream>
using namespace std;
int main()
{
    int num, c;
    int n = 2;
    while ( cin >> num)
    {
      while ( n <= num )
     {
        c = 0;
        while ( num % n == 0 )
        {
            num = num / n;
            ++c;
        }
        if ( c > 0 )
        {
            cout << n;
            if ( c > 1 )
                cout << "^" << c;
            if ( num > 1 )
                cout << " * ";
        }
        ( n >= 3  ) ? ( n += 2 ) : ++n ;
      }
     cout << endl;
    }
    return 0;
}
 
 
只有第一個輸出是對的,其他都輸出錯誤,但不知道為什麼

請大大幫解答謝謝 

#7828: Re:一直WA


akira0331 (小迷糊)


#include
using namespace std;
int main()
{
    int num, c;
    int n = 2;
    while ( cin >> num)
    {
      while ( n <= num )
     {
        c = 0;
        while ( num % n == 0 )
        {
            num = num / n;
            ++c;
        }
        if ( c > 0 )
        {
            cout << n;
            if ( c > 1 )
                cout << "^" << c;
            if ( num > 1 )
                cout << " * ";
        }
        ( n >= 3  ) ? ( n += 2 ) : ++n ;
      }
     cout << endl;
    }
    return 0;
}
 
 
只有第一個輸出是對的,其他都輸出錯誤,但不知道為什麼

請大大幫解答謝謝 


你的程式測試結果第一次送20會輸出答案,但第次再給20豃什麼都沒有

從程式看來 n++之後,n 值就回不去原本預設的2