一、题目链接
http://noi.openjudge.cn/ch0105/43/
二、解题思路
◎ 根据质因数分解的唯一性,用给定整数n去除从2开始的每个数k,如果能除尽,则n/k就是n的较大质因数。
三、实施步骤
◆ 方法public int greaterPrime(int n)处理所有业务逻辑:
→ 参数n为int类型的整数,代表给定整数;
→ 方法greaterPrime返回int类型的整数,代表n的较大质因数。
◇ 方法greaterPrime实施步骤如下:
◎ 首先,定义int类型的整数smallerPrime,代表较小的质因数;
◎ 其次,在n%smallerPrime!=0时,循环处理如下:
→ 令smallerPrime++;
◎ 最后,返回n/smallerPrime。
◇ 在方法main中输入给定整数n,将其作为参数注入方法greaterPrime,输出计算结果。
四、Java程序
import java.util.Scanner;
public class Main {
public int greaterPrime(int n) {
int smallerPrime = 2;
while (n % smallerPrime != 0) {
smallerPrime++;
}
return n / smallerPrime;
}
public static void main(String[] args) {
Main test = new Main();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.print(test.greaterPrime(n));
}
}