史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   Dev-c++請教 (http://forum.slime.com.tw/thread217700.html)

孤鷹 2007-10-06 11:24 PM

Dev-c++請教
 
各位大大..這一個程式,我哪裡寫錯了..為何無法執行??:on_47:幫我看看吧!!!!

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(void)
{
int i;
double n;
cout<<"請輸入一正整數"<<endl;
cin>>n;
//double sqrt(double n); //把n 開根號
for(i=2;i<=sqrt(n);i++)
{
for(n%i==0)
break;
}
if(i<=(sqrt)n)
cout<<n<<"不為質數!"<<endl;
else
cout<<n<<"為質數!"<<emdl;

system("pause");
return 0;
}

joebin 2007-10-07 05:26 PM

引用:

作者: 孤鷹 (文章 1822235)
語法:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(void)
{
int i;
double n;
cout<<"請輸入一正整數"<<endl;
cin>>n;
 //double sqrt(double n);  //把n 開根號
  for(i=2;i<=sqrt(n);i++) 
    {
    if(n%i==0)
    break;
    }
  if(i<=(sqrt)n)
  cout<<n<<"不為質數!"<<endl;
  else
  cout<<n<<"為質數!"<<endl;
 
  system("pause");
  return 0;
}


紅色部份:on_28:,另一寫法(參考)
語法:

#include <iostream>
using namespace std;
int main(){
int i,j=0,n;
cout<<"請輸入一正整數"<<endl;
cin>>n;
for(i=2;i*i<=n;i++)
    if(n%i==0&&n!=2){
      cout<<n<<"不為質數!"<<endl;
      j=1;
      break;
    }
  if(j==0)
      cout<<n<<"為質數!"<<endl;
 
  system("pause");
  return 0;
}


孤鷹 2007-10-08 12:23 AM

感謝大大
 
恩恩.....感謝大大的教導.小弟了解了.:on_28:..再次感謝了


所有時間均為台北時間。現在的時間是 04:02 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1