作者fujinawa (大腳與小腳)
看板NTUEE107HW
標題Re: 有沒有人是修王勝德的課啊 我不會他的第二次 …
時間Tue Oct 21 19:49:25 2003
同學如果你還在奮鬥的話:
※ 引述《santana111 (風中呢喃的燕子)》之銘言:
: 如題
: An integer is said to be prime if it is divisible only by two distinct
: factors 1 and itself. For example, 2, 3, 5, and 7 are prime, but 4, 6, 8
: and 9 are not. [Note: The number 1 is not a prime number.] Write a
: function that determines if a number is prime. The following is a main
: program that calls the function to determine and print all the prime
: numbers between 1 and 10,000.
就是要做檢查質數的工具,質數的定義是從2開始檢查可不可以整除,要是到本身都不能整除
就是一個質數了:因為這章教的是函數,所以要用函數寫。
: // prime.cpp
: #include <iostream>
: using std::cout;
: using std::endl;
: #include <iomanip>
: using std::setw;
: bool prime( int ); /* prototype for function prime */
: int main()
: {
: int count = 0;
: cout << "The prime numbers from 1 to 10000 are:\n";
: for ( int loop = 2; loop <= 10000; ++loop )
作一個檢查1-10000的迴圈
: if ( prime(loop) ) { /* make call to function prime */
: 這就是你要做的工作了
++count;
: cout << setw( 6 ) << loop;
: if ( count % 10 == 0 )
: cout << '\n';
: } // end if
: cout << '\n' << "There were " << count
: << " prime numbers\n";
: return 0;
印出值來
: } // end main
所以你應該再作一個迴圈
對於一個給定的loop值
從2開始檢查是不是質數
假定你的檢查子是test好了
就是要對if(loop % test = = 0)
繼續做下去
而對else跳出
個人淺經驗
請參考
另外提醒一下如果要用數學運算子
請記得加上
#include <cmath>
加油
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.18.117
→ valken:我沒加#include<cmath>耶... 推140.112.246.157 10/21
→ timrau:數學函數才要#include <cmath> 推 210.85.10.126 10/21