题目很简单,求第100002个素数(2也是素数)答案貌似是:1299721:-P
题目很短,好吧,我承认题目也很简单,但是算法与我不熟(^^),所以才用逐个比对o(╯□╰)o
所以在考场上等到第二题算完后,程序算出来啦,再然后,由于没用pause把程序停下来,结果你懂得(@﹏@)~
加个pause,重新运行程序(幸好比赛时间为4小时,程序终于在我无比的怨念的中迸出了结果)
虽然承认自己算法效率太低(记得当时考场怨念很重^^,貌似……)
好吧,把代码放在这里,留在下次比赛来取,希望会有所改进吧
//2、3、5、7、9、11、13、17、19
#include
#include
main()
{
bool temp = false;
int count = 2;
for(int i=3; i<10000000;i=i+2)
{
temp = true;
for(int j=2; j<i;i=i++)
{
if(i%j==0)
{
temp=false;
break;
}
}
if (temp)
{
count++;
if (count == 100002) break;
}
}
printf("%d",i);
system("pause");
}
终于想出来算法啦,原来可以这样。。。
//2、3、5、7、9、11、13、17、19
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
bool temp = false;
int count = 2;
for(int i=3; i<10000000;i=i+2)
{
temp = true;
for(int j=2; j<sqrt(i)+1; j++)
{
if(i%j==0)
{
temp=false;
break;
}
}
if (temp)
{
count++;
if (count == 100002) break;
}
}
printf("%d",i);
system("pause");
}

留言