您的位置:首页 > 其它

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: