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程序员的情书

    java程序员的情书 我能抽象出整个世界但是我不能抽象出你因为你在我心中是那么的具体所以我的世界并不完整我可以重载甚至覆盖这个世界里的任何一种方法但是我却不能重载对你的思念也许命中注定了 你在我的世界 ...

  2. yagmail让发邮件更简单

    这是我迄今为止碰到的最良心的库,真tm简单啊 import yagmail # 连接邮箱服务器 yag = yagmail.SMTP(user="wuyongqiang2012@163.co ...

  3. typeof与instanceof的区别

    一.instanceof运算符:       此运算符可以判断一个变量是否是某个对象(类)的实例,返回值是布尔类型的.想要理解它的作用,必须对面向对象有所理解: 代码实例如下: var str=new ...

  4. [LeetCode] Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  5. 基于线程池的多线程售票demo2.0(原创)

    继上回基于线程池的多线程售票demo,具体链接: http://www.cnblogs.com/xifenglou/p/8807323.html以上算是单机版的实现,特别使用了redis 实现分布式锁 ...

  6. Jmeter启动问题总结

    下载下来的jmeter文件,双击jmeter.bat文件打开的时候,系统提示如下: 查询安装的环境,java的jdk存在,并且版本在1.7以上,详情如下: 在环境变量PATH中添加:%SystemRo ...

  7. .NET CORE 2.0之 httpcontext

    HttpContext  在之前的.NET framework 是一个非常常用且强大的类,在.NET CORE2.0中要像以前用是不太方便的了, 要是用sesson 首先需要在startup 的Con ...

  8. Struts1开山篇

    ·本次学习的是Struts1的最终版本--struts-1.3.10. ·开发环境: Java版本:1.8.0_131 Tomcat版本:apache-tomcat-9.0.0.M21 下下来完整安装 ...

  9. Java IO(三)

    在Java IO提供的类中,除了前面介绍的RandomAccessFile类之外,还有一系列的io操作类. 主要分为两大类.字符流和字节流.关系图如下: 在Java IO的操作中,很好的体现了Java ...

  10. Java集合详解二

    前一篇我们已经讲过了Collectin是存放单值的最大接口, 可以看到Map接口和其主要之类的关系图如下: 快速访问 HashMap    HashTable  TreeMap  Map输出 那Map ...