POJ3048 HDU2710 Max Factor

筛选法不仅能够用来计算最小的若干素数,也可以用来求整数的最大公因子。

问题链接:POJ3048 HDU2710 Max Factor。基础训练级的题,用C语言编写。

问题简述:测试数据有多组,每组先输入n,然后输入n个正整数,输出n个正整数中,素因子最大的那个数。

问题分析:可以使用类似于筛选法的过程求得一定范围内的各个整数的最大素因子。

程序中,打表是合适的。数组mpf[]中存放最大素因子,若mpf[i]=x,则x为i的最大素因子。

AC通过的C语言程序如下:

/* POJ3048 HDU2710 Max Factor */

#include <stdio.h>
#include <memory.h>

#define MAXN 20000

/* 若mpf[i] = x,则x为i的最大因子(素数) mpf = maxprimefactor */
int mpf[MAXN+1]/* = {1, 1, 2, 3}*/;

void maketabel(int n)
{
    int i, j;

    memset(mpf, 0, sizeof(mpf));

    mpf[0] = 0;
    mpf[1] = 1;

    /* 对于偶数,令其因子暂时为2 */
    for(i=2; i<=n; i+=2)
        mpf[i] = 2;

    /* 对于素数i,设置j=k*i的暂时因子为i */
    for(i=3; i<=n; i+=2)
        if(0 == mpf[i])
            for(j=i; j<=n; j+=i)
                mpf[j] = i;
}

int main(void)
{
    int n, max, val, i;

    maketabel(MAXN);

    while(scanf("%d", &n) != EOF) {
        scanf("%d", &val);

        max = val;
        for(i=2; i<=n; i++) {
            scanf("%d", &val);
            if(mpf[val] > mpf[max])
                max = val;
        }

        printf("%d\n", max);
    }

    return 0;
}
时间: 2024-10-16 10:13:56

POJ3048 HDU2710 Max Factor的相关文章

抓其根本(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)

素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. 1 int prime() 2 { 3 for (int i=2; i*i<=n; i++) 4 { 5 if (n%i==0) //不是素数 6 return 1; //返回1 7 } 8 return 0; //是素数返回0 9 } 二.打表,将所有的素数一一列出,存在一个数组里. 详见代码. 1 void prime() 2 { 3 for (int i=2; i<20050; i++) //从2开始一个

HDU2710 Max Factor【水题】【素因子】

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4110    Accepted Submission(s): 1342 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1

【poj3048】 Max Factor

http://poj.org/problem?id=3048 (题目链接) 题意 给出n个数,问其中哪个数拥有最大的质因子. Solution 线性筛求素数 细节 .. 代码 // poj3048 #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #define MOD

HDU2710 Max Factor

题意:求输入的n个数中所含素因子最大的数字. 注意:此题中1也是素数. 思路:数据不算太大,朴素判断素数即可. max标记最大素因子. pos标记所含最大素因子的数. #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int f(int n) { for(int i = 2;

hdu 2710 Max Factor(找最大素数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710 Problem Description 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 unawa

HDU 2710 Max Factor 数论(水

Max Factor Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 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

HDU 2710 Max Factor (筛选求素数)

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3995    Accepted Submission(s): 1301 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1

POJ3048 Max Factor

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权!   Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct s

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

Problem Description 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 be