抓其根本(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开始一个一个找
 4     {
 5         if (hash[i]==0)             //这一个判断可以减少很多重复的,节省很多时间
 6         {
 7             for (int j=2; i*j<20050; j++) //只要乘以i就一定不是素数
 8             {
 9                 hash[i*j]=1;               //不是素数标记为1
10             }
11         }
12     }
13 }

举个例子:hdu2710 Max Factor

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710

Max Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4168    Accepted Submission(s):
1366

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 better than others. In particular, a cow whose serial
number has the highest prime factor enjoys the highest social standing among all
the other cows.

(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 line

Output

* 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

题目大意:找到所给数的最大素因子,然后在比较这些素因子的大小,找到最大的,最后输出原有的那个数。

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 int hash[20050];
 8
 9 void prime()
10 {
11     for (int i=2; i<20050; i++)
12     {
13         if (hash[i]==0)
14         {
15             for (int j=1; i*j<20050; j++)
16             {
17                 hash[i*j]=i;//i表示的是最大的素因子
18             }
19         }
20     }
21     //return hash[n];
22 }
23
24 int main ()
25 {
26     int T;
27     memset(hash,0,sizeof(hash));
28     sushu();
29     while (~scanf("%d",&T))
30     {
31         int Max=0,x=1;
32         while (T--)
33         {
34             int n;
35             scanf("%d",&n);
36             if (hash[n]>Max)
37             {
38                 Max=hash[n];
39                 x=n;
40             }
41         }
42         printf ("%d\n",x);
43     }
44     return 0;
45 }

最大公约数(gcd)

详见代码。

1 int gcd(int a,int b)
2 {
3     return a%b?gcd(b,a%b):b;
4 } 

最小公倍数

求解最小公倍数,一般都要借助最大公约数。辗转相除求得最大公约数,再用两数之积除以此最大公约数,得最小公倍数。

注意基本!!!

时间: 2024-10-05 18:15:46

抓其根本(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)的相关文章

POJ3048 HDU2710 Max Factor

筛选法不仅能够用来计算最小的若干素数,也可以用来求整数的最大公因子. 问题链接:POJ3048 HDU2710 Max Factor.基础训练级的题,用C语言编写. 问题简述:测试数据有多组,每组先输入n,然后输入n个正整数,输出n个正整数中,素因子最大的那个数. 问题分析:可以使用类似于筛选法的过程求得一定范围内的各个整数的最大素因子. 程序中,打表是合适的.数组mpf[]中存放最大素因子,若mpf[i]=x,则x为i的最大素因子. AC通过的C语言程序如下: /* POJ3048 HDU27

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

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

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: 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

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

12--c完数/最大公约数/最小公倍数/素数/回文数

完数/最大公约数/最小公倍数/素数/回文数 2015-04-08 10:33 296人阅读 评论(0) 收藏 举报  分类: C/C++(60)  哈尔滨工业大学(8)  版权声明:本文为博主原创文章,未经博主允许不得转载. 1.一个正整数的因子是所有可以整除它的正整数.而一个数如果恰好等于除它本身外的因子之和,这个数就称为完数.例如6=1+2+3(6的因子是1,2,3). [cpp] view plain copy #include <stdio.h> #include <math.h

POJ 3048 Max Factor (筛素数)

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 better tha