穷举所有质因子,穷举法
#include<iostream>
using namespace std;
bool isprime(int n)
{
for (int i = 2; i <= n / i; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
int main(int argc, char const* argv[])
{
int a, b;
cin >> a >> b;
for (int k = a; k <= b; k++)
{
int i=k;
if (isprime(i))
cout << i << "=" << i << endl;
else
{
int temp = 0;
cout << i << "=";
for (int j = 2; j <= i/j ; j++)
{
if (i % j == 0)
{
while(i%j==0)
{
i = i / j;
if (!temp)
{
cout << j;
temp = 1;
}
else
cout << "*" << j;
}
}
}
if (i > 1)
cout << "*" << i << endl;
else
cout<<endl;
}
}
return 0;
}