Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13481    Accepted Submission(s): 4765

Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
 
Input
Each line will contain one integer n(0 < n < 1000000).
 
Output
Output the LPF(n).
 
Sample Input
1
2
3
4
5
 
Sample Output
0
1
2
1
3
 
题意:众所周知,一个数可以由素数因子(和其它因子素数或者非素数 )相乘而形成,比如2的质数因子是2(也就是第1个质数),3是3(也就是第2个质数),4是2(也就是第1个质数)……然后题意就是问你是第几个素数。
解题思路:表示刚刚看到题目是懵逼的,看了人家的题解也是懵逼的,可能和自己在家里好久没A题目了有关系。
                  题目思路是这样:1.先做一个全局变量(如果不是全局变量就会出现栈溢出),然后把那个全部置为0。a[i]=x  i表示是某个数 x表示是第几个质数
                                               2.再开始打表,从第a[2]个开始看它是不是素数,是就把当前cnt做a[i]=cnt,然后把i的倍数 a[n*i]=cnt(循环),然后因为后面这些数都是2的倍数,它们就不是质数,但如果有质                                                    数比2 大且是它们的素数因子的话 就会再之后的判断中更新。
                                               3.用scanf("%d",&n)!=EOF来读取数据,不然会超时,不加EOF也不行因为不加的话会不断的读取数据,还是会超时。
附上代码:
#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<sstream>
#include<string.h>
#include<stdio.h>
using namespace std; //题目的意思是找到最大素数因子的位置 就是第几个素数而不是
//找到最大素数因子
int a[];//要定义全局变量 不然会栈溢出
int main()
{
int cnt=;//记录排第几个 memset(a,,sizeof(a));//把所有的数组中的数全部置为0
// a[1]=0;
for(int i=;i<;i++)//素数从2开始排 因为题目要求是1
{
if(a[i]==)//表示是素数 因为没有它的因子让它置为其它数
{
a[i]=cnt; for(int k=*i;k<;k=k+i)//是素数就先把它的倍数给置数
{// 然后遇到大的就不断刷新
a[k]=cnt;
}
cnt++;
}
}
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",a[n]);
}
return ;
}

这题感觉花了一定的时间才弄懂。

杭电 2136 Largest prime factor(最大素数因子的位置)的更多相关文章

  1. HDU 2136 Largest prime factor (素数打表。。。)

    题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. ...

  2. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  3. HDOJ(HDU) 2136 Largest prime factor(素数筛选)

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  4. 2136 Largest prime factor(打表)

    Problem Description Everybody knows any number can be combined by the prime number.Now, your task is ...

  5. HDU 2136 Largest prime factor 參考代码

    #include <iostream> #include <vector> #include <cmath> using namespace std; const ...

  6. HDU 2136 Largest prime factor

    题目大意:求出比给出数小的互质的质数个数. 题解:直接用筛法求素数,稍微改编一下,将原先的布尔数组变为数组用来记录信息就可以了. 注意点:大的数组定义要放在程序的开头,不要放在main里面,不然会栈溢 ...

  7. [HDU2136] Largest prime factor(素数筛)

    传送门 题意 给出若干个数n(n<=1000000),求每个n的最大质因子的排名. 质数的排名:如果素数p是第k小的素数,那么p的排名就是k. 思路 乍一看不知道怎么搞. 其实可以想想我们怎么筛 ...

  8. 【沙茶了+筛选保存最大质因数】【HDU2136】Largest prime factor

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. 【ACM】Largest prime factor

    /*打表把素数能组合的数先设置成相应的位数*/ /* if n equals two and n is No.1 position of prime factors  so four position ...

随机推荐

  1. 对已经存在的没有唯一标识的表添加一个自增的id字段(利用序列sequence)操作过程

    1.原始的数据表 2.操作 -- 创建序列 test_data_file_Id_Seq -- create sequence Test_data_file_Id_Seq increment by 1 ...

  2. python面试的100题(1)

    题目:有一个jsonline格式的文件file.txt大小约为10K def get_lines(): with open('file.txt','rb') as f: return f.readli ...

  3. XSS 4

    第四题 进去后是这个样子的 然后我们随便输入第三题中的数据发现 不可以运行 看一下右边 发现()被替换成空内容 然后我们改一下  括号可以用倒引号替换 然后我们改一下 然后就通过了 原因:这个题是有关 ...

  4. Object 反射 List

    public static object DeserializeModel<T>(object dataModel) { Type type = typeof(T); Object ent ...

  5. Git提交时提示“Please make sure you have the correct access rights and the repository exists.”的解决方法

    1.首先打开Git Bash设置名字和邮箱: git config --global user.name "你的名字" git config --global user.email ...

  6. maven私服搭建&使用

    Maven私服搭建教程 一.nexus安装 1,解压安装包 安装包下载地址 2,以管理员身份打开cmd 3,进入到nexus的bin目录 (1) 安装:nexus install (2) 启动:nex ...

  7. 联合查询:union

    1.联合查询:union 1.1 作用:将多条select语句的结果,合并到一起,称之为联合操作. 1.2 语法:( ) union ( ); 例子:(select name from info_or ...

  8. 搭建vue工程遇到的问题汇总

    1.vue搭建环境- vue init webpack my-project无响应(如下报错) 原因:node 版本过高.v8.1.0 解决拌饭: 降级,终端指令 n v8.0.0; mac用户: s ...

  9. (原创)Windows下编译的Shell脚本不能再Linux中运行的解决办法

    一.原理 Windows编译的文件和Linux编译的文件格式不太一样,导致在Linux运行Shell脚本的时候会提示:/bin/bash^M: bad interpreter: 没有那个文件或目录. ...

  10. 安卓开发:图片的显示Mode

    安卓开发中将图片放置在ImageView中展示,涉及到图片显示的模式,跟iOS开发中UIView的contentMode属性是一个意思,在安卓UI元素中使用的属性是scaleType,其中枚举值的效果 ...