To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as better than others. In particular, a cow whose serial number has the highest prime factor enjoys the highest social standing among all the other cows.

(Recall that a prime number is just a number that has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, is not).

Given a set of N (1 <= N <= 5,000) serial numbers in the range 1..20,000, determine the one that has the largest prime factor.

Input* Line 1: A single integer, N

* Lines 2..N+1: The serial numbers to be tested, one per lineOutput* Line 1: The integer with the largest prime factor. If there are more than one, output the one that appears earliest in the input file.Sample Input

4
36
38
40
42

Sample Output

38
 /*
Name: WTZPT
Copyright:
Author:
Date: 14/08/17 11:13
Description: 先输入数字的个数n,然后n次输入数字,输出其中拥有最大素数因子的数字
*/
#include<bits/stdc++.h>
using namespace std;
const int MAX = ;
int prime[MAX],temp[MAX];
void init()
{
memset(temp,,sizeof(temp));
for(int i = ; i < MAX; i++) /*先建立一个素数表*/
if(temp[i])
{
temp[i] = ; //素数
for(int j = i*; j < MAX; j+=i)
temp[j] = ; //非素数
} prime[] = ; //素数
int count = ;
for(int i = ; i < MAX; i++) /*把素数存储到一个容器内*/
{
if(temp[i])
prime[count++] = i;
} }
int main()
{
init();
int t,ans,max;
while(cin>>t)
{
max = ;
memset(temp,,sizeof(temp));
for(int i = ; i<= t; i++)
scanf("%d",&temp[i]); for(int i = ; i <= t; i++) /*对每个输入的数进行处理*/
for(int j = ; prime[j] <= temp[i]; j++) /*二营长,把老子的意大利炮拉出来*/
if(temp[i] % prime[j] == ) /*应题目要求 因子为素数的情况*/
if(max < prime[j]) /*当目前的素数因子最大时*/
{
max = prime[j];
ans = i; /*存储原数据的位置*/
}
cout<<temp[ans]<<endl;
}
return ;
}

ACM Max Factor的更多相关文章

  1. 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)

    素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...

  2. HDU-2710 Max Factor

    看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. Max Factor(素数筛法)题解

    Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. POJ3048 Max Factor

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  5. HDOJ/HDU 2710 Max Factor(素数快速筛选~)

    Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...

  6. hdu 2710 Max Factor 数学(水题)

    本来是不打算贴这道水题的,自己却WA了三次.. 要考虑1的情况,1的质因子为1 思路:先打表 ,然后根据最大质因子更新结果 代码: #include<iostream> #include& ...

  7. Max Factor 2710 最大的合数的质数因子

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2710 思路:用类似“埃氏筛法”求素数的方法 只是不在把合数标记为1 而是标记为他是因子数. 最后比较大小即 ...

  8. poj 3048 Max Factor(素数筛)

    这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...

  9. hdu2710 Max Factor

    题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...

随机推荐

  1. Java 高级开发必修知识---反射

    Class类的使用 1) 在面向对象的世界里,万事万物皆对象 A. Java语言中,普通数据类型,静态成员不是对象,其他皆对象 B. 每一个类也是对象 C. 类是java.lang.Class类的实例 ...

  2. python/ Django之中间件

    python/ Django之中间件 一.中间件 中间件共分为: (1)process_request(self,request) (2)process_view(self, request, cal ...

  3. python 资产管理

    python 资产管理 一.Agent 方式 1.这个方法的优点:使用简单,速度快,适合服务器较多场景使用,缺点:服务器比较占资源,性能会变低. 2.使用Agent的前提条件是客户端(服务器)特别多的 ...

  4. spark2.1:flatMap的用法

    代码示例: val sample_data_combine_result=List( (0,(List(FitModel(4022,1447.92,-8.38983306721434,2.0)),1) ...

  5. spring boot 系列之三:spring boot 整合JdbcTemplate

    前面两篇文章我们讲了两件事情: 通过一个简单实例进行spring boot 入门 修改spring boot 默认的服务端口号和默认context path 这篇文章我们来看下怎么通过JdbcTemp ...

  6. github git 在GitHub上创建项目并将本地项目push到网站上

    众所周知,git是与svn类似的版本控制系统,git的去中心化.分布式等的优点,在不久将来用户量大有可能超过svn, 常见的代码托管网站有GitHub,coding.net, gitee.com 码云 ...

  7. jQuery.noConflict() 函数详解

    jQuery.noConflict()函数用于让出jQuery库对变量$(和变量jQuery)的控制权. 一般情况下,在jQuery库中,变量$是变量jQuery的别名,它们之间是等价的,例如jQue ...

  8. nativescript——轮播图组件

    import { Directive, ElementRef, AfterViewInit, Input, OnDestroy } from "@angular/core"; im ...

  9. [LeetCode] Kill Process 结束进程

    Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...

  10. boot.img格式文件拆解实例结构解析

    以msm8226为例,讲解android源码编译生成boot.img的结构.boot.img包括boot.img header.kernel以及ramdisk文件系统.下面是对boot.img的结构进 ...