第十一週專案6.4.3輸出1000以內所有的迴文素數
阿新 • • 發佈:2019-02-02
問題及程式碼:
執行結果:/* *Copyringht (c) 2014,煙臺大學計算機學院 *All right reserved. *檔名稱: test.cpp *作 者:馬明城 *完成日期:2014年11月10日 *版本號: v1.0 *問題描述: *輸入描述: *程式輸出: */ #include <iostream> #include <cmath> using namespace std; bool isPalindrome(int); bool isPrimer(int); int main() { int i; cout<<"1000以內的迴文素數是:"; for(i=10; i<=1000; ++i) { if ( isPalindrome(i)&&isPrimer(i) ) cout<<i<<" "; } return 0; } bool isPalindrome(int n) { int r,m=0,l; l=n; bool isPalindrome=false; while(l>0) { r=l%10; m=m*10+r; l=l/10; } if(m==n) isPalindrome=true; return isPalindrome; } bool isPrimer(int n) { bool primer=true; int a=sqrt((int)n); for(int i=2;i<=a;++i) { if(n%i==0) { primer=false; break; } } return primer; }
知識總結:一定不要忘記開始時註明要定義的函式,還有後面的;不要忘了