#9836: (C++)新手用for迴圈寫TLE


han880805 (Yu)


 我的程式碼如下
#include <iostream>
#include <math.h>
using namespace std ;
int main(){
int n,sta,stb;
while (cin >> n){
sta=1;
for (int stb=2;stb<=sqrt(n);stb++){
if (n%stb==0) sta=0;
if (sta==1) cout << "質數" << endl;
else cout << "非質數" << endl;
}
}
但這樣寫雖然自己跑會對 但傳上來不管怎麼樣都TLE 請問大家這是為甚麼呢 @@? 
#9837: Re:(C++)新手用for迴圈寫TLE


p5351280 (小張)


 我的程式碼如下
#include
#include
using namespace std ;
int main(){
int n,sta,stb;
while (cin >> n){
sta=1;
for (int stb=2;stb<=sqrt(n);stb++){
if (n%stb==0) sta=0;
if (sta==1) cout << "質數" << endl;
else cout << "非質數" << endl;
}
}
但這樣寫雖然自己跑會對 但傳上來不管怎麼樣都TLE 請問大家這是為甚麼呢 @@? 


用這種方式跑一定會超時...