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

来源:互联网 发布:淘宝男士鞋子店铺 编辑:程序博客网 时间:2024/06/04 19:25
素数判断:

一、根据素数定义,该数除了1和它本身以外不再有其他的因数。

详见代码。

1 int prime()2 {3     for (int i=2; i*i<=n; i++)4     {5         if (n%i==0)    //不是素数6             return 1;  //返回17     }8     return 0;          //是素数返回09 }

二、打表,将所有的素数一一列出,存在一个数组里。

详见代码。

 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;               //不是素数标记为110             }11         }12     }13 }

提供一种技巧、如果题目里面所有的计算都是素数之间的转化的话、可以如下。

 1 void prime() 2 { 3     int k=0; 4     for (int i=2; i<20050; i++)    5     { 6         if (hash[i]==0)             7         { 8             sushu[k++]=i;               //所有的素数都存在了sushu的数组里面,或者放在队列里面q.push(i); 9             for (int j=2; i*j<20050; j++) 10             {11                 hash[i*j]=1;              12             }13         }14     }15 }

 

 

举个例子: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 } 

最小公倍数

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

 

注意基本!!!

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 退苹果id重新登陆照片没了怎么办 苹果升级后照片效果没了怎么办 360云盘个人云盘停止服务怎么办 小米手机账号掉了手机卡没了怎么办 百度网盘下载后不能注册怎么办 微信发出的文件无法撤回怎么办 微信群发错消息无法撤回怎么办 百度网盘登录要验证码怎么办 百度网盘备份记录怎么办能删掉 手机酷狗听歌耳机声音太小了怎么办 苹果6s微信通话声音变粗怎么办 千千静听多个列表合成一个了怎么办 别人用手机号注册了邮箱怎么办 微信身份信息验证未通过怎么办 快递地址填错了已经发到了怎么办 孩子威胁同学给她买东西吃怎么办 在超市买小孩米粉过期了怎么办 出口货物被海关扣了说仿牌怎么办 有一批仿牌被宁波海关查了怎么办 付了定金不想要车了怎么办 泥墙刮石灰上涂料现在刮腻子怎么办 月经最后一天同房了怎么办吃什么药 奶水太多了宝宝总是呛到了怎么办 婴儿刚吃完奶大量喷奶怎么办 我干活细致领导说我慢怎么办 怀孕初期吃了加明矾的粉皮怎么办 绿豆面黄豆面白面蒸馒头怎么办 画油画时把颜料染在衣服上怎么办 买的布衣柜少了一个管子怎么办 小2球刚好进2球怎么办 大2球刚好进2球怎么办 大球2进了2球怎么办 身上剩的几百块钱有掉了怎么办 刚买一天的手机电板有问题怎么办 宝宝长湿疹穿少了就感冒怎么办 照片跟视频被孩子删了怎么办 老公总是说一些不开心的话题怎么办 孩子沉迷手机篮球课不爱去怎么办 坐久了屁鼓会长疮怎么办 套胶的胶皮与海绵分离怎么办 换肾15天了沒尿怎么办?