[問題] 判斷質數的程式 覺得簡單但一直超時
看板C_and_CPP (C/C++)作者tw00088437 (喵貓 loves fish)時間16年前 (2009/10/25 17:38)推噓3(3推 0噓 18→)留言21則, 5人參與討論串1/1
http://zerojudge.tw/ShowProblem?problemid=d447
題目
使用平台:dev c++
就只是要判斷質數 只是我第一次送出TLE(超過一秒)
後來我建了一些表又用了6N+-1判斷法
還是TLE = =
請問還有哪些地方可以改進變得更快@@
#include<iostream>
#include<cmath>
using namespace std;
int N,a,b,c;//c =counter
int main()
{
while(cin>>N)
{
b=1;
c=0;
a=43;
if(N%2==0&&N!=2)
cout<<"no"<<endl;
else if(N%3==0&&N!=3)
cout<<"no"<<endl;
else if(N%5==0&&N!=5)
cout<<"no"<<endl;
else if(N%7==0&&N!=7)
cout<<"no"<<endl;
else if(N%11==0&&N!=11)
cout<<"no"<<endl;
else if(N%13==0&&N!=13)
cout<<"no"<<endl;
else if(N%17==0&&N!=17)
cout<<"no"<<endl;
else if(N%19==0&&N!=19)
cout<<"no"<<endl;
else if(N%23==0&&N!=23)
cout<<"no"<<endl;
else if(N%29==0&&N!=29)
cout<<"no"<<endl;
else if(N%31==0&&N!=31)
cout<<"no"<<endl;
else if(N%37==0&&N!=37)
cout<<"no"<<endl;
else if(N%41==0&&N!=41)
cout<<"no"<<endl;
else if(N%43==0&&N!=43)
cout<<"no"<<endl;
else
{
while(a<=sqrt(N))
{
if(N%a==0)
{ cout<<"no"<<endl;
b=0; }
a=a+(c%2)*2+2;
c++;
}
if(b)
cout<<"yes"<<endl;
}
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.228.103.110
推
10/25 17:52, , 1F
10/25 17:52, 1F
→
10/25 17:52, , 2F
10/25 17:52, 2F
→
10/25 19:01, , 3F
10/25 19:01, 3F
→
10/25 19:01, , 4F
10/25 19:01, 4F
→
10/25 19:02, , 5F
10/25 19:02, 5F
→
10/25 19:03, , 6F
10/25 19:03, 6F
→
10/25 19:03, , 7F
10/25 19:03, 7F
→
10/25 19:18, , 8F
10/25 19:18, 8F
→
10/25 19:27, , 9F
10/25 19:27, 9F
→
10/25 19:28, , 10F
10/25 19:28, 10F
→
10/25 19:28, , 11F
10/25 19:28, 11F
→
10/25 22:26, , 12F
10/25 22:26, 12F
→
10/25 22:26, , 13F
10/25 22:26, 13F
→
10/25 22:28, , 14F
10/25 22:28, 14F
推
10/25 23:44, , 15F
10/25 23:44, 15F
→
10/26 01:04, , 16F
10/26 01:04, 16F
→
10/26 01:05, , 17F
10/26 01:05, 17F
→
10/26 01:41, , 18F
10/26 01:41, 18F
→
10/26 01:41, , 19F
10/26 01:41, 19F
→
10/26 01:42, , 20F
10/26 01:42, 20F
推
10/26 01:59, , 21F
10/26 01:59, 21F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章