有什麼問題都可以問我
綽號:若G (ㄐ)
FB:胡若家
line:kellyhu6686
DEV C++
Sublime text 3
notepad++
網上編譯
~課程開始~
在 寫 程 式 的 時 候 , 常 會 碰 到 某 些 資 料 重 覆 使 用, 如 果 以 正 常 的 方 式 來 寫 作 程 式 , 可 能 會 增 加 無 謂 的 程 式 片 段 , 使 的 程 式 看 起 來 會 笨 重 不 堪 、不 俐 落。
電 腦 會 把 這 些 模 組 存 在 C: 內,要 用 的 時 候 再 include 出 來。
前 置 處 理 器 的 解 釋 , 如 圖 所 示 , 因 為 它 是 在 程 式 編 譯 之 前 執 行 , 所 以 顧 名 思 義 稱 之 前 置 處 理 。
什麼是前置處理指令?
在編譯程式前將 iostream 這個檔案放到目前的原始程式中來
有用到cout 物件的程式皆需加以上這個前置處理指令
當程式中需要用到數學公式,例如:log、abs、pow
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main(){
return 0;
}如果沒有打上去程式可能就會出錯
cout << "hello world" << endl;std::cout << "hello world" << std::endl;using namespace std;命名
cin >> x;做一個輸入班上30人成績的程式
不可能取30個不同的變數名稱去輸入
int arr[5];
陣列的index(起始位元值)永遠都從0開始
//0,1,2,3,4
可以存入5個整數
cout << "hello world" << endl << 123;
cout << "hello world\n" << 123;cin >> x;cout << "hello world" << endl;每一行程是後面都要加 " ; "
只要是一行程式,後面都要加!!!
(非常重要)
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
int x;
cout << "請輸入數字: ";
cin >> x;
cout << x << endl;
return 0;
}for( 起始 ; 結束 ; 每次增加多少 ){ ... }
結束 - 起始 = 要做的次數
for(i=0;i<5;i++){
cout << i << ",";
}() 中的 i 記得也要宣告
if( 關係式 ){ ... }
else{ ... }
如果成立做這裡
如果沒成立做這裡
int i=1;
if(i==1)
cout << "yes" << endl;
else
cout << "no" << endl;i=8
i==8做 { ... }當我的(判斷式)成立的時候
int i=1;
do{
cout << "OH!!!" << endl;
i++;
}while( i<=5 );至少會做一次
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
return 0;
}先把基底打上吧
int i,j,n[5];srand(time(NULL));for(i=1;i<=5;i++){
}do{
}while( )int i,j,n[5],re;n[i-1]=rand()%5+1;re=0; //歸零for(j=1;j<i;j++){
}if([n-i]==[n-j]){
}如果符合
re++;do{
n[i-1]=rand()%5+1;
re=0;
for(j=0;j<i;j++){
if(n[i-1]==n[j-1])
re++;
}
}while(re>=1);
當 re>=1重新做一次
cout << "n[" << i << "]=" << n[i-1] << endl;來測測看可不可以用吧
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
int i,j,n[5],re;
srand(time(NULL));
for(i=1;i<=5;i++) {
do{
n[i-1]=rand()%5+1;
re=0;
for(j=1;j<i;j++){
if(n[i-1]==n[j-1
])
re++;
}
}while(re>=1);
cout << "n[" << i << "]=" << n[i-1] << endl;
}
return 0;
}END