精華區beta NTUCH-HW 關於我們 聯絡資訊
功能 用到的觀念 CODE #include"iostream"//標頭檔 #include"iomanip" #include"cmath" using namespace std; using std::setw; using std::fixed; using std::setprecision; int _tmain(int argc, _TCHAR* argv[]) { int count=0; unsigned long n,k,i,j,l,prime,temp=3;//n save the number of the end, int m=0;//if m=1 i isn't a prime number. if m=2 i is a prime number cout<<"find the number form 1 to n"<<endl;//顯示內容 cin>>n;//輸入存到n system("cls");//clean the screen cout<<"the prime number are:"<<endl;//顯示內容 if(n>2)//check whether n is bigger than 2 { for(i=3; i<=n;i++)//從3檢查到n判斷是否為質數 { for(j=2;j*j<=i;j++) { if(i%j==0)//if i is divided with no remainder { m=1;//save 1 to m break;//jump from this loop } else if(i%j!=0)//i is divided with remainder { m=2; prime=i; } } if(m==2) { if((prime-temp)==2) { cout<<"("<<temp<<","<<prime<<")"<<endl;//show the content count=count+1;//counter plus 1 } temp=prime; } } } cout<< "there are " << count << " twin number"<<endl;//輸出值數的數量 system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59