ACM Max Factor
(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的更多相关文章
- 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Max Factor(素数筛法)题解
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- POJ3048 Max Factor
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- hdu 2710 Max Factor 数学(水题)
本来是不打算贴这道水题的,自己却WA了三次.. 要考虑1的情况,1的质因子为1 思路:先打表 ,然后根据最大质因子更新结果 代码: #include<iostream> #include& ...
- Max Factor 2710 最大的合数的质数因子
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2710 思路:用类似“埃氏筛法”求素数的方法 只是不在把合数标记为1 而是标记为他是因子数. 最后比较大小即 ...
- poj 3048 Max Factor(素数筛)
这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...
- hdu2710 Max Factor
题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...
随机推荐
- 【WebGL入门】画一个旋转的cube
最近搜罗了各种资料,发现WebGL中文网特别好用,很适合新手入门:http://www.hewebgl.com/article/getarticle/50 只需要下载好需要的所有包,然后用notepa ...
- .net core 使用阿里云短信发送SMS
阿里云官方的skd(aliyun-net-sdk-core,aliyun-net-sdk-dysmsapi)在dnc中发送短信会出错,nuget上的包貌似也一样不管用.直接改下sdk当然也可以,但就发 ...
- 解决VS2017编译后的EXE文件不能在其他电脑上运行的问题
笔者昨天写了个超简单画图程序,很是激动啊,立马给同学分享了自己写的程序,结果发现无法运行 错误是这样的 解决方法如下: 1.将Debug改为Release 2.进入[项目]-[**属性] 3.[C/C ...
- JAVA_将唐诗按照古文样式输出
1. 如有唐诗: 锄禾日当午 汗滴禾下土 谁知盘中餐 粒粒皆辛苦 要求将这首唐诗按照古文样式输出,输出格式如下: 粒谁汗锄 粒知滴禾 皆盘禾日 辛中下当 苦餐土午 public class Text ...
- java中的多态案例
多态性实际上有两种: 1.方法的多态性: 1.1方法重载:相同的方法名,会根据传入的参数的类型和个数不同执行不同的方法 1.2方法覆写:同一个方法名称,会根据子类的不同实现不同的功能 2.对象的多态性 ...
- [LeetCode] Max Area of Island 岛的最大面积
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- Spring Cloud Eureka 自我保护机制
Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果 ...
- 用js来实现那些数据结构08(链表02-双向链表)
其实无论在任何语言中,一种数据结构往往会有很多的延伸和变种以应对不同场景的需要.其实前面我们所学过的栈和队列也是可以用链表来实现的.有兴趣的小伙伴可以自己尝试着去实现以下. 有点跑题了...,我们还是 ...
- [NOI 2011]阿狸的打字机
Description 题库链接 给你 \(n\) 个单词, \(m\) 组询问,每组询问形同 \((x,y)\) ,询问 \(x\) 串在 \(y\) 串中出现多少次. \(1\leq n,m\le ...