POJ-3006 Dirichlet's Theorem on Arithmetic Progressions
2015-07-22 11:37
549 查看
#include <iostream> using namespace std; const int maxn = 1000000; int num[maxn]; int main() { int a,d,n; while(cin >> a >> d >> n) { if(!a && !d && !n) break; int i = 0; do { int t = 1; if(a == 0 || a == 1) t = 0; for(int j = 2; j * j <= a; j++) if(a % j == 0) { t = 0; break; } if(t == 1) { num[i] = a; i++; } a+=d; }while(i <= n-1); cout << num[i-1] << endl; } return 0; }
题意:输入 a d n 。相当于自建一个等差数列 a,a+d,a+2d,a+3d....之后取其中的素数,取第n个素数。
题解:水题吧。用了do while(最后的while注意要用;隔开 一开始没想起来)边录入元素到数组,并判断。注意i++要放入if(t == 1)里面(又是一开始的错误),否则i只会不断自加而素数不会录入正确的位置。当i = n 跳出并输出。(跳出时 i = n,需-1)。(题目中的n是从1开始算起 而i从0开始)</span>
相关文章推荐
- Spark: sortBy和sortByKey函数详解
- linq group by max 多表链接实例
- webservice远程调试开启
- HDU 1312:Red and Black(DFS搜索)
- 大数据处理分析的工具介绍
- 使用C++程序获取新浪行情数据的方法
- PHP结合jQuery实现找回密码
- 数组的下标访问和指针访问方式效率分析比较
- 数组的下标访问和指针访问方式效率分析比较
- Hadoop例子中WordCount参数分析
- 阿里巴巴装袋问题
- hdu 5289 Assignment(2015 Multi-University Training Contest 1)
- Gradle DSL method not found: 'android()
- 环境变量
- 工控自动化通讯测试工具总结之--串口
- Extjs和jQuery的区别
- 几个交互设计的原则
- 异地备份mongo
- swift UITableViewCell 中的单选控制样式
- Git fetch和git pull的区别(转)