一、题目链接
http://noi.openjudge.cn/ch0105/43/
二、解题思路
◎ 根据质因数分解的唯一性,用给定整数n去除从2开始的每个数k,如果能除尽,则n/k就是n的较大质因数。
三、实施步骤
◆ 函数int greaterPrime(int n)处理所有业务逻辑:
→ 参数n为int类型的整数,代表给定整数;
→ 函数greaterPrime返回int类型的整数,代表n的较大质因数。
◇ 函数greaterPrime实施步骤如下:
◎ 首先,定义int类型的整数smallerPrime,代表较小的质因数;
◎ 其次,在n%smallerPrime!=0时,循环处理如下:
→ 令smallerPrime++;
◎ 最后,返回n/smallerPrime。
◇ 在主函数main中输入给定整数n,将其作为参数注入函数greaterPrime,输出计算结果。
四、C++程序
#include <iostream>
using namespace std;
int greaterPrime(int n)
{
int smallerPrime = 2;
while (n % smallerPrime != 0)
{
smallerPrime++;
}
return n / smallerPrime;
}
int main()
{
int n;
cin >> n;
cout << greaterPrime(n);
return 0;
}