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 ...
随机推荐
- 无用代码清除tip
测试提了个bug过来,说是有个ajax请求报404了. 我一看,后台代码被人删了,问了同事,因为实现机制变了,是应该删,但删多了. 把service和controller都恢复后,一个接口中除了我那个 ...
- [Luogu1801] 黑匣子 - Treap
Description Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命 ...
- python Django学生管理
Django 学生管理系统 1. 一对一 班级 模态增加 编辑 <!DOCTYPE html> <html lang="en"> <head> ...
- python基础——继承实现的原理
python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...
- Hibernate(十五):QBC检索、本地SQL检索和HQL删除
QBC检索 QBC查询就是通过使用Hibernate提供的Query By Criteria API来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口. 1)通过Cr ...
- JavaScript正则表达式学习笔记之一 - 理论基础
自从年前得空写了两篇文章之后就开始忙了,这一忙就是2个月
- devops基本流程
概要 源代码管理: 工具:Subversion (SVN) Concurrent Version System (CVS) Git SCCS Revision control systems Bitb ...
- drupal 8 之 calendar模块
一.安装模块 calendar.view.date三个模块同时安装 二.创建一个事件内容类型 主要的是有一个时间字段 然后添加一个事件内容进行测试 [保存并发布] 三.创建日历视图 点击[结构]> ...
- json pickle ;shelve
import json dic={'name':'alex'} """ f=open("new_hello","w") # dic ...
- Java中数据表的建立
class Emp{ private int empno;//职工编号 private String ename;//姓名 private String job;//职位 private double ...