#include <iostream>#include <cmath>#include <iomanip>using namespace std;void sushu(){ int m,k,i; cin>>m; k=int (sqrt((double)m)); if(m==1) { cout<<"該正整數不為素數!"<<endl; return; } else if(m==3||m==2) { cout<<"該正整數為素數!"<<endl; return; } else { for(i=2;i<=k;i++) { if(m%i==0) { cout<<"該正整數不為素數!"<<endl; return; } } cout<<"該正整數為素數!"<<endl; } }int main(){ cout<<"Please Enter m:"; sushu(); cout<<endl; return 0;}
#include <iostream>#include <cmath>#include <iomanip>using namespace std;void sushu(){ int m,k,i; cin>>m; k=int (sqrt((double)m)); if(m==1) { cout<<"該正整數不為素數!"<<endl; return; } else if(m==3||m==2) { cout<<"該正整數為素數!"<<endl; return; } else { for(i=2;i<=k;i++) { if(m%i==0) { cout<<"該正整數不為素數!"<<endl; return; } } cout<<"該正整數為素數!"<<endl; } }int main(){ cout<<"Please Enter m:"; sushu(); cout<<endl; return 0;}
錯誤的地方對照看對比一下,應該能發現,sushu這個函式最好為void型別,呼叫的時候不需要前面的int,sushu函式判斷是否素數如果每次都if判斷輸出會出現太多結果