计蒜客 难题题库 219 最大质因数
2015-08-11 09:14
393 查看
52次
13.4%
1000ms
65536K
给出N个数字,试求质因数最大的数字。
输入格式:
第一行,一个整数N,表示数字个数。 接下来N行,每行一个整数A_i,表示给出的数字。
输出格式:
一个整数,表示质因数最大的数字。
N < = 5000 , A_i < = 20000
举例
38和12
38=19*2, 12=2*3*3, 38最大的是19 12最大的是3, 所以本数据要输出38。
输出:
13.4%
1000ms
65536K
给出N个数字,试求质因数最大的数字。
输入格式:
第一行,一个整数N,表示数字个数。 接下来N行,每行一个整数A_i,表示给出的数字。
输出格式:
一个整数,表示质因数最大的数字。
N < = 5000 , A_i < = 20000
举例
38和12
38=19*2, 12=2*3*3, 38最大的是19 12最大的是3, 所以本数据要输出38。
样例1
输入:4 36 38 40 42
输出:
38
#include<iostream> using namespace std; const int maxn = 20001; bool isPrime[maxn]; void init(){ isPrime[2] = 1; int i; for(i = 3; i < maxn; i += 2){ isPrime[i] = 1; } int j; for(i = 3; i < maxn; ++i){ if(isPrime[i]){ for(j = i * 2; j < maxn; j += i){ isPrime[j] = 0; } } } } int max_prime_factor(int n){ for(int i = n; i >= 2; --i){ if(isPrime[i] && n % i == 0){ return i; } } return 0; } int main(){ init(); int n, a, temp; cin >> n; int max_val = -1, res = 0; while(n--){ cin >> a; if(max_val < (temp = max_prime_factor(a))){ max_val = temp; res = a; } } cout << res << endl; }
相关文章推荐
- **Jquery-之jqGrid表格插件学习(一)**
- IOS代码添加控件,控件移动,放大,缩小,旋转
- 高性能JavaScript 重排与重绘
- Android利用Intent与其他应用交互
- 【坑】html5中使用canvas绘制两个strokeRect之间忘了用beginPath()
- maven中使用不同配置文件打包
- ExtraCHM 1.5破解版和破解教程:
- Failed to load JavaHL Library解决方法
- 使用nginx sticky实现基于cookie的负载均衡
- UIScrollView的属性总结
- css3 matrix 2D矩阵和canvas transform 2D矩阵
- GCD队列组2
- Win10重置后出现inaccessible boot device提示 Win10进系统无限重启解决办法
- Ubuntu系统进程绑定CPU核
- 将Eclipse代码导入到AndroidStudio的两种方式
- 解决jdk8之The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .
- Codeforces 569A Music
- LeetCode(162) Find Peak Element
- 方法重构脑图
- Hashmap实现原理