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. React-Native(四):React Native之View学习

    React Native实现以下布局效果:携html5(http://m.ctrip.com/html5/) 基于HelloWord修改项目代码: /** * Sample React Native ...

  2. 智能提示含查询多列(html+JS+handler+ HttpRemoting)一、html示列 加 JS加 请求 Handler

    <html> <head> </head> <body> <form id="recordform" name="r ...

  3. hdu2062 Subset sequence----递推

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2062 题目大意: 给出n和m,集合{1,2,,,,n}的非空子集,按照一定方式排列,例如n==3时, ...

  4. CNN中减少网络的参数的三个思想

    CNN中减少网络的参数的三个思想: 1) 局部连接(Local Connectivity) 2) 权值共享(Shared Weights) 3) 池化(Pooling) 局部连接 局部连接是相对于全连 ...

  5. scrapy爬取豆瓣电影top250

    # -*- coding: utf-8 -*- # scrapy爬取豆瓣电影top250 import scrapy from douban.items import DoubanItem class ...

  6. MongoDB 更新文档

    MongoDB 使用 update() 和 save() 方法来更新集合中的文档.接下来让我们详细来看下两个函数的应用及其区别. update() 方法 update() 方法用于更新已存在的文档.语 ...

  7. 原生js的一些研究和总结(1)

    数据类型 基本类型值包括: undefined,null,Boolean,Number和String,这些类型分别在内存中占有固定的大小空间,它们的值保存在栈空间,我们通过按值来访问的. 引用类型包括 ...

  8. DVA框架统一处理所有页面的loading状态

    dva 有一个管理 effects 执行的 hook,并基于此封装了 dva-loading 插件.通过这个插件,我们可以不必一遍遍地写 showLoading 和 hideLoading,当发起请求 ...

  9. [LeetCode] Dota2 Senate 刀塔二参议院

    In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...

  10. [LeetCode] Student Attendance Record I 学生出勤记录之一

    You are given a string representing an attendance record for a student. The record only contains the ...