质因子分解

该程序旨在读取一个正整数并找出其所有质因子。例如,输入72,输出为2(3)3(2),181944的输出为2(3)3(2)7(1)19(2)。程序通过分解质因数来展示每个质因子的幂次。
摘要由CSDN通过智能技术生成

题目:请写一个程序,读入一个正整数,把它的所有质因子找出来。例如,输入是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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值