1285: Factors

参考

  hadis_fukan的博客——wustoj 1285 Factors

题目

  输入一个数n,找出1~n之间(包括1,n)的质因子最多的数(x)的质因子个数(f[x])。更多内容点击标题。

分析

  很明显,这题的结果是可以直接算出来的,保存到数组中。通过题目,就可以了解到,我们需要算出2-2000000之间所有的数的质因子的个数。这样才能找出1~n之间质因子最多的数x。这里你可能想是不是需要先将质数单独求出来。那是完全没必要的。我们只需要在求因子的过程中,顺带着区分出质数即可。并且合理利用已经计算出的质数。

测试数据

 输入

2
3
4
5
6
7
8
9

 输出

1
1
2
2
2
2
3
3

代码

/**
* time 1148ms
* @author PengHao
* @version 1.0
* @date 2019-05-03 上午9:34:02
* Environment: Windows 10
* IDE Version: Eclipse 2019-3
* JDK Version: JDK1.8.0_112
*/ import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner; public class Main {
private Scanner sc;
/**
* @Field <code>MAXN</code> N的最大值
*/
private static final int MAXN = 2000000;
/**
* @Field <code>prime</code> 保存素数
*/
private ArrayList<Integer> prime;
/**
* @Field <code>f</code> x的质因子的个数为f[x]
*/
private byte[] f;
/**
* @Field <code>maxF</code> 1-n之间质因子最多的数的质因子个数
*/
private byte[] maxF; public Main() {
sc = new Scanner(new BufferedInputStream(System.in));
initF(); // 初始化f
initMaxF(); // 初始化maxF
int n;
while (sc.hasNext()) {
n = sc.nextInt();
System.out.println(maxF[n]);
}
sc.close();
} /**
* Initialize the array f.
*/
private void initF() {
prime = new ArrayList<Integer>(); // 保存素数
f = new byte[MAXN + 1]; // 记录质因子个数
boolean iPrime; // true 质数,false 非质数
for (int i = 2; i <= MAXN; i++) {
iPrime = true; // 默认i是质数
for (int j : prime) { // 取出一个质数j
// 如果j的平方大于i,那么j肯定不是i的因子
// 因此i就是质数
if (j * j > i) {
break;
}
if (0 == i % j) { // 如果j是i的因子
iPrime = false; // 那么i是不是质数
// i的质因子个数等于另一个因子的质因子个数加1
f[i] = (byte) (f[i / j] + 1);
break;
}
}
if (iPrime) { // 如果i是质数
prime.add(i); // 将i加到质数数组列表中
f[i] = 1; // 并且质数i的质因子个数为1
}
}
} /**
* Initialize the array maxF.
*/
private void initMaxF() {
maxF = new byte[MAXN + 1];
byte max = 0; // 记录质因子个数的最大值
for (int i = 2; i <= MAXN; i++) {
if (f[i] > max) { // 当前数i的质因子个数比max大
max = f[i]; // 更新最大值
}
maxF[i] = max; // 将最大值保存到数组中
}
} public static void main(String[] args) {
new Main();
}
}

写在最后:

  1. 如需转载,请于首页注明链接形式的wowpH的博客——WUSTOJ 1285: Factors(Java)
  2. 代码原创,如需公开引用,不能删除首行注释(作者,版本号,时间等信息)。
  3. 如果有疑问欢迎评论留言,尽量解答。

WUSTOJ 1285: Factors(Java)的更多相关文章

  1. WUSTOJ 1319: 球(Java)并查集

    题目链接:1319: 球 参考:wustoj 1319 球-wust_tanyao,并查集 并查集系列:WUSTOJ 1346: DARK SOULS(Java)并查集 Description Icy ...

  2. WUSTOJ 1283: Hamster(Java)

    1283: Hamster 参考博客 wust_tanyao的博客 题目   第0个月有1对仓鼠.仓鼠的寿命是M个月,仓鼠成年后每个月生一对仓鼠(一雌一雄),问N个月后有仓鼠多少对.更多内容点此链接 ...

  3. WUSTOJ 1326: Graph(Java)费马数

    题目链接:1326: Graph 参考博客:HNUSTOJ-1617 Graph(费马数)--G2MI Description Your task is to judge whether a regu ...

  4. WUSTOJ 1332: Prime Factors(Java)

    题目链接:1332: Prime Factors Description I'll give you a number , please tell me how many different prim ...

  5. WUSTOJ 1349: TLE(Java)算法优化

    题目链接:1349: TLE Description WH在刷题时,设计出了如下代码: #include<stdio.h> int main() { int i, j, cnt, k, N ...

  6. WUSTOJ 1282: Start(Java)

    1282: Start 题目   判断一个字符串是不是回文串.例如:"abcba"是回文串.更多内容点击标题. 分析   水题,自己思考. 代码 /** * time 838ms ...

  7. WUSTOJ 1347: GCD(Java)互质

    题目链接:1347: GCD Description 已知gcd(a,b)表示a,b的最大公约数. 现在给你一个整数n,你的任务是在区间[1,n)里面找到一个最大的x,使得gcd(x,n)等于1. I ...

  8. WUSTOJ 1325: Distance(Java)坐标计算

    题目链接:1325: Distance Description There is a battle field. It is a square with the side length 100 mil ...

  9. WUSTOJ 1313: 数列(Java)进制转换

    题目链接:

随机推荐

  1. [WEB安全]Dirsearch工具命令

    下载项目,并打开 ┌─[root@kali]─[/kali] └──╼ #git clone https://github.com/maurosoria/dirsearch ┌─[root@kali] ...

  2. DIOCP任务队列和工作线程

    DIOCP任务队列和工作线程 涉及4个单元文件:utils_strings.pas,utils_queues.pas,utils_queueTask.pas,utils_grouptask.pas. ...

  3. VS中卸载Visual Assist X

    Tools=>Extensions and updates=>找到Visual Assist X 卸载:

  4. python代码-leetcode1 两数相加

    1.两个循环 class Solution: def twoSum(self, nums, target): n=len(nums) for i in range(n): for j in range ...

  5. tensorflow cpu问题

    返回: -- ::] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 ...

  6. React Native 中的 Flex Box 的用法(水平布局、垂直布局、水平居中、垂直居中、居中布局)

     版权声明:本文仅供个人学习. CSS 中 Flex-Box 语法链接 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex 是 ...

  7. Django之model补充:一对多、跨表操作

    表结构概述 model.py : class Something(models.Model): name = models.CharField(max_length=32) class UserTyp ...

  8. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  9. 货币转换函数:CURRENCY_CONVERTING_FACTOR

    针对不同币别要做金额栏位转换 计算规则: 金额 = 原始金额 * 转换率 以下转自博客:https://www.cnblogs.com/sanlly/p/3371568.html 货币转换函数:CUR ...

  10. LeetCode_69. Sqrt(x)

    69. Sqrt(x) Easy Implement int sqrt(int x). Compute and return the square root of x, where x is guar ...