题目:请写一个程序,读入一个正整数,把它的所有质因子找出来。例如,输入是72,27 = 2^3 * 3^2,于是质因子就有2与3;如果输入是181944,那么因为181944 = 2^3 * 3^2 * 7 * 19^2,所以质因子为2、3、7、19。为了方便起见,2^3 * 3^2 * 7 * 19^2可以用2(3)3(2)7(1)19(2)作输出,也就是说,如果分解开来有a^b,在输出中就有a(b)。
#include <iostream>
#include <map>
using namespace std;
void getFactors(int number, map<int, int> &imap)
{
if (number <= 1)
return;
int base = 2;
int count = 0;
while (number != 1)
{
if ((number & 0x01) == 0)
{
number >>= 1;
count++;
}
else
break;
}
if (count != 0)
imap.insert(make_pair(base, count));
base++;
while (number != 1)
{
count = 0;
while (number != 1)
{
if (number % base == 0)
{
number /= base;
count++;
}
else
break;
}
if (count != 0)
imap.