最大质因数

蓝桥杯ALGO-331 最大质因数

问题描述
  给出N个数字,求出有最大的最大质因数的那个数
输入格式
  第一行:一个整数N。
  接下来的N行,每行一个整数A_i,表示给出的那N个数字。
输出格式
  第一行:一个整数,拥有最大的最大质因数的那个数。
样例输入
4
36
38
40
42
样例输出
38
数据规模和约定
  60%的数据满足:N<=100
  100%的数据满足:N<=2500,A_i<=20000

import java.util.Scanner;

public class Main {
	static int result = 0;
	static int n = 0;
	static int[] arr = new int[2502];
	static int[] yin = new int[2502];
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
	    n = sc.nextInt();
	    for(int i=0;i<n;i++){
	    	arr[i] = sc.nextInt();
	    }
	    f(arr);
	}
	
	private static void f(int[] arr){
		int temp = 0;	//存储最大的输入数
		int tmp = 0;	//存储最大的质因数
		for(int i=0;i<n;i++){
			int count = 0;
			temp = arr[i];
			for(int j=2;j<temp;j++){
				if(arr[i] % j==0){
					yin[count] = j;	//j是arr[i]的一个因数
					temp = temp/j;	//temp是arr[i]的另一个因数
					count++;
				}
			}
			if(temp != 0){	//判断一下arr[i]的另一个因数temp是否是质数
				yin[count] = temp;
			}
			for(int j=0;j<count+1;j++){
				boolean book = false;
				for(int m=2;m<yin[j];m++){
					if(yin[j]%m==0){
						book = true;
						break;
					}
				}
				if(book==false && yin[j]>tmp){
					tmp = yin[j];
					result = arr[i];
				}
			}
		}
		System.out.println(result);
	}
}
原文地址:https://www.cnblogs.com/techgy/p/12775472.html