hdu 5108 Alexandra and Prime Numbers

本文介绍了一种通过遍历和除法操作来寻找给定整数n的最大质因子的算法。该算法首先检查输入是否为1,若是则直接返回0;否则通过循环迭代的方式找到最大的质因子,并将其输出。

数论题,本质是求出n的最大质因子

 1 #include<time.h>
 2 #include <cstdio>  
 3 #include <iostream>  
 4 #include<algorithm>
 5 #include<math.h> 
 6 #include <string.h>  
 7 #include<vector> 
 8 #include<queue>
 9 using namespace std;
10 
11 int main()
12 {
13     int n,t;
14     while(scanf("%d",&n)!=EOF)
15     {
16         if(n == 1)  //注意1不是素数,找不到最大质因子 
17         {
18             printf("0\n");
19             continue;
20         }
21         
22         else
23         {
24             t=n;
25             int maxx=1;
26             for(int i=2;i<=int(sqrt(n));i++)
27                 while(t%i==0)
28                 {
29                     t/=i;
30                     maxx=i;
31                 }
32             maxx=max(t,maxx);
33             printf("%d\n",n/maxx);
34         }
35     }
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/pter/p/5409988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值