您的答案為: * 3^2 * 5 * 11 * 23^2
正確答案為: 3^2 * 5 * 11 * 23^2
#include <iostream>
using namespace std;
bool isPrime( int );
void display( int, int );
int main()
{
int number;
int factor;
while( cin >> number )
{
for( factor = 2; factor < number; factor++ )
if( number % factor == 0 && isPrime( factor ) )
display( number, factor );
cout << endl;
}
system( "pause" );
return 0;
}
bool isPrime( int factor )
{
int i;
for( i = 2; i * i <= factor; i++ )
if( factor % i == 0 )
return false;
return true;
}
void display( int number, int factor )
{
static bool first = true;
int power = 0;
while( number % factor == 0 )
{
number = number / factor;
power++;
}
if( first )
{
if( power == 1 )
cout << factor;
else
cout << factor << '^' << power;
first = false;
}
else
{
if( power == 1 )
cout << " * " << factor;
else
cout << " * " << factor << '^' << power;
}
}
您的答案為: * 3^2 * 5 * 11 * 23^2
正確答案為: 3^2 * 5 * 11 * 23^2
#include
using namespace std;
bool isPrime( int );
void display( int, int );
int main()
{
int number;
int factor;
while( cin >> number )
{
for( factor = 2; factor < number; factor++ )
if( number % factor == 0 && isPrime( factor ) )
display( number, factor );
cout << endl;
}
system( "pause" );
return 0;
}
bool isPrime( int factor )
{
int i;
for( i = 2; i * i <= factor; i++ )
if( factor % i == 0 )
return false;
return true;
}
void display( int number, int factor )
{
static bool first = true;
int power = 0;
while( number % factor == 0 )
{
number = number / factor;
power++;
}
if( first )
{
if( power == 1 )
cout << factor;
else
cout << factor << '^' << power;
first = false;
}
else
{
if( power == 1 )
cout << " * " << factor;
else
cout << " * " << factor << '^' << power;
}
}