短除法 质因数分解 c++

#include<iostream>
using namespace std;
int main()
{
    
    
	cout <<"please input the integer: "<< endl;
	int num;
	cin >> num;
	for (int i = 2;i <= num;i++) //短除法
	{
    
    
		while(num%i==0)
        {
    
    
            cout<<i<<" ";
            num/=i;
        }
	}
	return 0;
}

示例一
输入:24
输出:2 2 2 3

示例二
输入:90
输出:2 3 3 5

参考资料:https://www.luogu.com.cn/blog/yhhh/solution-p2043

猜你喜欢

转载自blog.csdn.net/weixin_42378324/article/details/109257461