ZSISC 程式班

by 程教們

程教們:

二和 周俞彤

二和 蔡伊甯

二廉 江品萱

有問題可以問我們~~

line id:  sandy930314

ig:  @zsisc27_cyt

line id:  apple310en

ig:  @zsisc27_ining

line id:  dora921125

ig:  @zsisc27_jumping

THE RULES

上課規範啦--你最好給我認真聽不然就ㄉㄐㄉㄍㄍ

One
Two
Three

電腦教室不准 正大光明||偷偷  吃||喝 東西

準時到電教二,遲到需說明原因

不可以抄襲他人程式碼,不會就問!!

Four
Five
Six

有問題直接 舉手||大叫 ,基本上都會回答

可以小聲討論,不要影響到他人

上課時盡量不要 睡覺||滑手機||寫中叢(其他作業)

p.s. 當然 如果你都會了且解完指定題目,就隨你~

Seven

最感興趣的部分 (ㄅ?

每堂社課上課完都會有 有獎徵答

but only 前三名 才有

玩過幾次ㄌ應該瞭ㄅ

所以想要有禮物的請認真聽課 !!!

Eight

時程規劃

  • 解題時間可能穿插於上課時間中
  • 有問題時可以舉手發問,也可以隨時攔住其他程教

C++

by h94usu/6

#class1_0924

CONTENTS

  • C++ introduction 介
  •  C++ IDE
  • "Hello world! \n"

>> if ( 有雞蛋 ) 買 6 顆蘋果;

      else 買一顆;

DECISION

  • 聽課組
  • 自學組

C++ INTRODUCTION

今晚,我想來點C++...

why C++ ??

  • C++ 兼具了許多程式語言的風格
  • 學了這個就很容易跳到其他語言
  • 對於 想學寫程式||之後想走資工相關科系 的人是個好選擇
    • ​程序導向 (依先後順序、流程)
    • 物件導向 (能夠把功能打包)
    • 編譯語言

difficult ??

  • 你如果問說 C++跟python比是不是比較難 ??
    • 很多人說python很簡單,是因為有很多 "前人留下的智慧",所以你可以用到很豐富很棒的模組
    • 至於C/ C++比相較起來就比較嚴謹,也相較複雜,不過可以讓你學到扎實的電腦知識
      • ex: 變數宣告、記憶體位址

for example

變數宣告

次方

大括號

分號

int x;
x = 924;
a = a*a*a; //或
for (int i=0; i<3; i++) a=a*a;
if (date == "0924") {
    cout << "First class!\n";
}
string str;
str = "Hello";
cout << str + "World!\n";
x = 924;
a = a**3;
if date == "0924":
    print("First class!")
str = "Hello"
print(str + " world!")

C++

python

so ?

  • 簡單來講,不管學甚麼程式語言都能夠訓練你的程式邏輯、運算思維
  • C++作為一個速度夠快的結構化語言,能讓你學到更深更基礎的運作模式,以及對電腦來說怎樣是較清楚、有效率的步驟

所以所以 希望你在星期四的社課中
即使沒有完全聽懂 也能收穫良多!!

一些 迷思

  • (教的)這些語法都要背嗎??
    • 其實多打幾次就不小心背起來了,而且基本上C++語法都可讀性蠻高的
    • 出去打程式競賽有些讓帶參考書,有些不讓

一些 迷思

  • 要空格嗎? 大括號要換行嗎? 變數名稱要有意義嗎?
    • 這其實看大家習慣的coding style
    • 自己琢磨琢磨找到最適合自己的coding style

一些 迷思 (+一些梗圖)

  • 要空格嗎? 大括號要換行嗎? 變數名稱要有意義嗎?

希望大家都能寫出可讀性高
且前後一致的code

該縮牌就縮排
不要擠成一坨

一些 迷思

  • 學了程式只能走資工?
    • 當然不
    • 如果走資訊相關學系,還有電機、資管、圖資、資傳、生資...等
    • 如不資訊相關,會寫程式這項技能在這個時代也是大大大大大的加分阿...!

C++ IDE 

在哪打扣 ??

Code::Blocks/ Dev-C++

Code::Blocks官網連結: http://www.codeblocks.org/

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

就從頭按「同意」、「允許」到尾

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

你想要的位置,通常預設就好

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

建立新檔案(Empty file),也可以新增專案(Create a new project)

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

記得儲存(才會變顏色xd)  p.s. ctrl+s是個好用的東西

副檔名記得調成 .cpp 檔

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

顏色變了就代表儲完存ㄌ!!!  開始開心打扣ㄅ

Code::Blocks download

Code::Blocks官網連結: http://www.codeblocks.org/

譯 + 執行 (compile+run)

vs code  本人蠻喜歡的 大家有興趣可以去研究研究

Hello World!!

「跟世界打招呼」

p.s.為啥都是跟世界說哈囉, "hello my friend!" 感覺也還不錯ㄅ...

complete code

#include<iostream>
using namespace std;

int main () {
  cout << "Hello world";
  return 0;
}

  • 先偷偷讓你看一下完整的程式碼

Basic Structure (我會學到: 基礎架構

#include<iostream> //引入C++, 輸出輸入的函式庫 (input output stream)
using namespace std; //讓你少打一些code的東西

int main () { //主函式
  
  return 0; //可加可不加, 加一下比較好, 之後在教函數時就會瞭啦
} //大括號!!

  • 幾乎每份程式都從這裡起步 (相當於一個模板)

"//" 兩個反斜線後面可以加註解 讓你記得你之前在幹嘛 也增加程式可讀性!!

有個萬用標頭檔 "#include <bits/stdc++.h>" 之後再談談

code (我會學到: 輸出、字串

#include<iostream>
using namespace std;

int main () {
  cout << "Hello world"; //加這行
  return 0;
}

  • 再加上 cout << "Hello world!";
  • 就能向世界打招呼了!!
  std::cout << "Hello world";

cout << "字";

c++ 的 output

輸出用 << (小於)

要輸出的字用 " " (雙引號)框住

code (我會學到: 輸入

#include<bits/stdc++.h>
using namespace std;

int main () {
  string name;
  cin >> name;
  cout << "Hello, " << name << "!";
  return 0;
}

  • 能夠輸出,當然也會有輸入
  • 再加一行,輸入此句主詞 ex: Hello, my friend!
  std::cin >> name;

cin >> 變數;

c++ 的 input

輸入用 >> (大於)

要輸入值進去的變數

practice

suggested answer

#include<iostream>
#include<string>
using namespace std;

int main() {
	string str;
	cin >> str;
	cout << "hello, " << str;
	return 0;
}

Title Text

VARIABLES

by h94usu/6

#class2_1008

CONTENTS

  • variables 變數
  • operator 運算子

一個不能忽視的存在 -- 變數

VARIABLES

variables in Math

  • 在數學領域中:  咱們熟悉的未知數
a^2+2*a*b+b^2=(a+b)^2
x+y=4, x-y=2  x=3, y=2
y=f(x)=ax+b
a^2+b^2=c^2
...

variables in programming

  • 有記憶體
  • 可更改、計算

 

 

  • 由許多形態組成
  • 唯一的值
 
int a = -2147483648;
char _b = 'b';
long long int c = 9223372036854775807;
float d = 3.1415926; //7位
double e = 3.141592653589793; //15位
...

variables

  • 可用半形的數字(0~9) || 字元(a~z, A~Z) || 底線(_)
  • 不可在同一區域重複宣告同一個變數(名稱)
  • 不可以數字開頭命名,ex: 1A, 1a2b
  • 不能用函式庫已有的函式名稱(關鍵字) 如下:
 

(但是好棒棒的編譯器大哥其實會幫你用 粗體 或 不同顏色 標起來)

以後會講這個概念

型態 中文意思 英文字義 儲存空間(位元) 數值範圍
int 整數 Integer 4 bytes/ 32 bits -2^31~2^31-1 (10 位數)
long long 長整數 long long integer 8 bytes/ 64 bits -2^63~2^63-1 (19 位數)
float 浮點數(小數) floating point 4 bytes/ 32 bits 3.4E +/- 38 (7 位數)
double 倍精度浮點數 double 8 bytes/ 64 bits 1.7E +/- 308 (15 位數)
char 字元(半形字) Character 1 byte/ 8 bits 0~255(ASCII)
bool 布林(是非) boolean 1 byte/ 8 bits false, true, 0~255

小數點後XX位

variable type

布林值 false=0, true=1(非0即為true)

declare + assign + i/ o

int num_1, num_2, num_3; 
//宣告整數型, 分別叫num_1, num_2, num_3的變數
int num_4 = 3; 
//宣告一個整數型, 叫num_4的變數, 並分配一個整數給它(賦值)
num_1 = 1; //賦值
cin>> num_2; 
//輸入一個整數進num_2
num_3 = num_1 + num_2
cout<< num_1<<" "<< num_2<< " "<< num_3<< " "<<num_4; 
//分別輸出值

Q: 要輸入己才能輸出"1 1 2 3"的費式數列??

















A: 1

宣告                            賦值                        輸入/ 輸出

 

other examples

int var;  //宣告一個名為var的整數
cin >> var;  //把值輸進var
cout << var;  //輸出var
char c = 'C';  //宣告名為c的字元,並賦值'C'
cout << c << " " << c;  //輸出C C
double a, b = 10.08;  
//宣告名為a、b的,並於b賦值10.08
a = b;  //於a賦值b的值,也就是 a=10.08
cout << a << b;  //輸出10.0810.08
bool _b = false;  //宣告名為_b的布林值,並賦值false
_b = true;  //將_b改為true
cout << "bool" << _b;  //輸出bool1

practice last last week

#include<iostream>
using namespace std;

int main() {
	string str;
	cin >> str;
	cout << "hello, " << str;
	return 0;
}

小學就會的 1+1 = 王田二 -- 運算子

OPERATORS

算數運算子

  • +加,ex: 6 + 5 = 11
  • -減,ex: 6 - 4 = 2
  • *乘,ex: 8 * 7 = 56
  • /除 ,整數(int)的除就是取商,ex: 11 / 3 = 3
    • 如果是小數(float/ double),1.23 / 2.4 = 0.5125
  • ​%mod,取餘數,ex: 8 % 3 = 2
    • 若被除數是負數,餘數會是負的,e.g.-8 % 5 = -3
    • ​你也可以叫它ㄆㄚ、ㄇㄛˊ...之類的

關係運算子

  • >大於
  • <= 小於等於
  • == 等於
    • == 要記得不能打成 = (賦值)
  • ​​​​ex: (3 == 5) 為 false,  (-1 <= 9) 為 true, (8 != 0) 為 true
  • < : 小於
  • >= : 大於等於
  • != : 不等於

邏輯運算子

  • !  非 (not)
  • && 且(and)   [所有條件皆成立即為true]
  •  || 或 (or)   [任一條件成立即為true]
    • && 跟 || 記得是兩個喔!
  • ​ex: (!0) 為 true, (1!=1 && 3 >= 7) 為false, (3==3 || 4<3.999) 為 true

Truth table  真值表

  • i = i + 1 //  i ++ //  i+=1
  • i = i - 1 //  i - - //  i - = 1
  • i = i + 8 //  i += 8
  • i = i - 8 //  i -= 8
  • i = i * 8 //  i *= 8
  • i = i / 8 //  i /= 8
  • i= i % 8 //  i %= 8

operators 

practice1

suggested answer_1

#include<iostream>
using namespace std;

int main() {
    int a, b;
    cout << a+b;
    return 0;
}
#include<iostream>
using namespace std;

int main() {
    int a, b;
    int ans;
    ans = a + b;
    cout << ans;
    return 0;
}

practice2

suggested answer_2

#include<iostream>
using namespace std;

int main() {
    int M, D;
    cin >> M >> D;
    if ((M*2+D)%3 == 0) cout << "普通";
    else if ((M*2+D)%3 == 1) cout << "吉";
    else cout << "大吉";
    return 0;
}

CONDITION

by 阿問

#class3_1022

CONTENTS

  • if statements 條件判斷
  • if 單向選擇
  • if-else 雙向選擇
  • if-else if- else 多向選擇
  • switch (補充)

if STATEMENT

if (我就爛 == true) -- 條件判斷

條件判斷?

  • 當某個條件成立時,執行該條件成立時的動作

>如果今天沒有下雨,那麼我就可以去打球

如果(今天沒有下雨){
    去打球;
}

if

if(!週會) everyone=happy; -- 單向選擇

語法

if(要判斷的條件){
    //條件成立時要執行的指令
}

例子

if(score>=60){
    cout<<"PASS";
}
  • 如果成績及格,就輸出PASS

例子

if(score>=60){
    cout<<"PASS";
    credits+=1;
}
  • 如果成績及格,就輸出PASS,且拿到的學分增加1

完整程式碼

#include <iostreram>
using namespace std;
int main(){
    int score,credits=0;
    cin>>score;
    if(score>=60){
        cout<<"PASS";
        credits+=1;
    }
    return 0;
}

兩個以上的條件?

if(7>5&&3<=4){
    cout<<"YES";
}
  • && 且
    • ​兩邊條件皆須成立
if(1<0||5==5){
    cout<<"YES";
}
  • || 或
    • ​其中一邊條件成立即可

if - else

if(!週會) happy;  else sad; -- 雙向選擇

語法

if(要判斷的條件){
    //條件成立時要執行的指令
}
else{
    //條件不成立時要執行的指令
}

例子

if(score>=60){
    cout<<"PASS";
}
else{
    cout<<"FAIL";
}
  • 如果成績及格,就輸出PASS,否則就輸出FAIL

完整程式碼

#include <iostreram>
using namespace std;
int main(){
    int score;
    cin>>score;
    if(score>=60){
        cout<<"PASS";
    }
    else{
        cout<<"FAIL";
    }
    return 0;
}

if - else if - else

if(!週會) :D ; else if(短) : ) ; else : ( ; -- 多向選擇

語法

if(條件1){
    //條件1成立時要執行的指令
}
else if(條件2){
    //不滿足條件1但條件2成立時要執行的指令
}
else{
    //以上條件皆不成立時要執行的指令
}

例子

if(score>=80){
    cout<<"excellent";
}
else if(score>=60){
    cout<<"good";
}
else{
    cout<<"keep going";
}
  • 如果成績大於等於80,就輸出excellent,否則如果成績大於等於60,就輸出good,否則輸出keep going

完整程式碼

#include <iostream>
using namespace std;
int main(){
    int score;
    cin>>score;
    if(score>=80){
        cout<<"excellent";
    }
    else if(score>=60){
        cout<<"good";
    }
    else{
        cout<<"keep going";
    }
    return 0;
}

也可以寫很多else-if...

if(條件1){
    //條件1成立時要執行的指令
}
else if(條件2){
    //條件2成立時要執行的指令
}
else if(條件3){
    //條件3成立時要執行的指令
}
             .  .  .
else{
    //以上條件皆不成立時要執行的指令
}

TIPS

  • 程式執行順序由上而下執行,

​      在同組條件判斷中,

      當上方條件成立便會執行該對應敘述,

      就不會繼續執行下面的判斷了!

  • 如果沒有用到else可省略

小測驗

#include <iostream>    
using namespace std;    
int main(){    
    int score=95;    
    if(score<60){    
        cout<<"E";    
    }    
    else if(score>=60){    
        cout<<"D";    
    }    
    else if(score>=70){
        cout<<"C";    
    }    
    else if(score>=80){
        cout<<"B";    
    }    
    else{
        cout<<"A";    
    }    
    return 0;    
} 
//D

if? else if?

#include <iostream>    
using namespace std;    
int main(){    
    int score=95;    
    if(score>=90){    
        cout<<"A";    
    }    
    else if(score>=80){    
        cout<<"B";    
    }    
    else if(score>=70){
        cout<<"C";    
    }    
    else if(score>=60){
        cout<<"D";    
    }    
    else{
        cout<<"E";    
    }    
    return 0;    
} 
#include <iostream>    
using namespace std;    
int main(){    
    int score=95;    
    if(score>=90){    
        cout<<"A";    
    }    
    if(score>=80){    
        cout<<"B";    
    }    
    if(score>=70){
        cout<<"C";    
    }    
    if(score>=60){
        cout<<"D";    
    }    
    if(score<60){
        cout<<"E";    
    }    
    return 0;    
} 

Practices

ZEROJUDGE

a003

d064

d065

More Practices

大家有空可以練習~

ZEROJUDGE

a053

d066

d067

d058

參考解答

SWITCH

【補充】

不是電動(x

多向選擇-SWITCH

  • 判斷條件多時不用打冗長的else if
  • 只能用於比較特定變數是否為某整數或字元

>因此應用範圍較不廣泛

語法

switch(變數名稱){
    case 條件1:
        //條件1成立時執行的指令
        break;
    case 條件2:
        //條件2成立時執行的指令
        break;
        . . . 
    default:
        //以上皆不成立時執行的指令
}
//條件只能為某個整數或字元,或者是整數或字元的區間

TIPS

  • ​因為case後面沒有大括號,所以一定要加break 電腦才會知道這個段落結束了

      否則程式會繼續往下進行直到遇到break或程          式碼結束

  • 區間語法:

          整數/字元(空格)...(空格)整數/字元

  • default若不用也可省略

例子

int score;
cin>>score;
switch(score){
    case 100:       //意思為score==100
        cout<<"滿分";
        break;
    case 60 ... 99: //包含60及99
        cout<<"及格";
        break;
    default:
        cout<<"不及格";
}

例子

char grade;
cin>>grade;
switch(grade){
    case 'A':
        cout<<"90~100";
        break;
    case 'B':
        cout<<"80~89";
        break;
    case 'C':
        cout<<"70~79";
        break;
    case 'D':
        cout<<"60~69";
        break;
    case 'E':
        cout<<"0~59";
        break;
    case 'F' ... 'Z':
        cout<<"error";
        break;
}

LOOP

by h94usu/6

#class4_1029

CONTENTS

  • loop 迴圈介
  •  while 迴圈
  •  for 迴圈

讓我們先釐清一下

wait a minute

operator review

  • when a = 1

讓我們先釐清一下

LOOP

why loop??

  • when 罰寫n遍
  •  
    • ​一般人
  •  
    • ​稍微聰明點的

why loop??

  •  
    • ​再厲害點
  • when 罰寫n遍

還有嗎???

of course!!!

why loop??

  •  
    • ​想想我們有個神奇的工具叫電腦
    • 並且我們似乎懂得如何輸出文字
  • when 罰寫n遍
#include <iostream>
using namespace std;

int main () {
    cout << "again and ";
    cout << "again and ";
    cout << "again and ";
    ...
    cout << "again and ";
    cout << "again";
    return 0;
}

ctrl+c & ctrl+v 永遠是我們的好幫手

但似乎有更快的方法??

why loop??

  •  
    • ​有個東西 叫 迴圈 可以讓你重複做出同樣的事
  • when 罰寫n遍
#include <iostream>
using namespace std;

int main () {
    int n;
    cin >> n;
    while (n--) {
        cout << "again and ";
    }
    cout << "again";
    return 0;
}

this is a loop

loop ?_?

  • "迴圈" 顧名思義
  • 就是 "環繞/ 返回 一個 "
  • 當條件成立->執行

this is a circle

also a loop

真是個讓人又愛又恨的東西

loop ?_?

  • 一種常見(用)的控制流程
  • 在特定程式區塊中,重複執行相同的指令
  • C++有三種迴圈​​
    • while loop
    • for loop
    • do-while (後測試迴圈,比較不常用)

well well well

while loop

while loop

  • 當 條件||數值 為true, 執行{ }裡的 statement
  • 注意: condition非 0 即為 true
while(condition){ //條件
    statement; //內容
    //do something...
}

example1

  • 印出 1~99 ??
#include<iostream>
using namespace std;

int main(){
    int i = 1;
    while(i <= 99){
    	cout << i <<endl;
        i++;
    }
    return 0;
}

example2_1

  • 倒著印出 1~99 ??
#include<iostream>
using namespace std;

int main(){
    int i = 99;
    while(i >= 1){
    	cout << i <<endl;
        i--;
    }
    return 0;
}

example2_2

  • 倒著印出 1~99 ??
#include<iostream>
using namespace std;

int main(){
    int i = 99;
    while(i){  //當 i 為 true   // i != 0
    	cout << i <<endl;
        i--;
    }
    return 0;
}

for u ~~

for loop

  • 當條件(中間)成立時,執行{}裡的動作
  • 注意: C語言不可在小括號內宣告
  • 注意: 變數/ 條件/ 更新 用分號隔開

for loop ?_?

for((宣告)初始化變數; 條件; 更新){
    statement;  //內容
    //do something...
}

(int) i=0

i<5

i++

example1

  • 印出 1~99 ??
#include<iostream>
using namespace std;

int main(){
    for(int i = 1; i <= 99; i++){
    	cout << i <<endl;
    }
    return 0;
}

example2

  • 倒印出 1~99 ??
#include<iostream>
using namespace std;

int main(){
    for(int i = 99; i >= 1; i--){
    	cout << i <<endl;
    }
    return 0;
}

for vs while

  • 是否決的似曾相似 ???
#include<iostream>
using namespace std;

int main(){
    int i = 1;
    while(i <= 99){
    	cout << i <<endl;
        i++;
    }
    return 0;
}
#include<iostream>
using namespace std;

int main(){
    for(int i = 1; i <= 99; i++){
    	cout << i <<endl;
    }
    return 0;
}

注意:  C++ 才能在for()裡宣告變數

但要注意區域變數

初始化

條件式

更新 ( 遞增||遞減 )

迴圈裡有迴圈裡有迴圈...

nested loop

for(int i=0; i<5; i++){ //外層迴圈
    for(int j=0; j<5; j++){ //內層迴圈
    	cin>> array[i][j];
    }
}
  • 迴圈裡有一個以上的迴圈
  • 常用在多維陣列

nested loop ?_?

巢狀

 

1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 6*10=60
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 7*10=70
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 8*10=80
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 9*10=90
10*1=10 10*2=20 10*3=30 10*4=40 10*5=50 10*6=60 10*7=70 10*8=80 10*9=90 10*10=100

when 10x10

when 10x10

  •  
#include<iostream>
using namespace std;

int main(){
    int i = 1, j = 1;
    while(i <= 10){
    	while(j <= 10){
    		cout << i << "*" << j;
            cout<< "=" << i*j << " " ;
    		j++;
        }
        cout << endl;
        j = 1;
        i++;
    }
    return 0;
}

"Dormammu, I've come to bargain...."

infinite loop

infinite loop ?_?

while(1){ 
    //do something...
}
  • 1永遠"非 0",條件恆為true
  • 通常會跟continue, break搭配

無窮

 

example

  • 無窮 "true"
#include<iostream>
using namespace std;

int main(){
  while(1) cout << "true ";
  return 0;
}

dont break up, lets continue

break && continue

for(int i=0; i<5; i++){ 
    if(i==3) break;
    cout<< i<<" ";
}
//0 1 2
  • 提前結束迴圈(跳出一層迴圈)
  • 通常放在條件判斷後

break ?_?

for(int i=0; i<5; i++){ 
    if(i==3) continue;
    cout<< i<<" ";
}
//0 1 2 4
  • 直接進入下一個迴圈
  • (跳過本次迴圈的內容)
  • 通常放在條件判斷後

continue ?_?

example

  • 請使用者輸入一個整數,規則如下:

     

    • 如果輸入為負數或零停止
    • =99999什麼都不做,繼續下一個輸入
    • 偶數輸出 “even”
    • 奇數輸出 “odd”
#include <iostream>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        if(n <= 0)
            break;
        else if(n >= 99999)
            continue;
        else if(n % 2 == 1)
            cout << "odd" << endl;
        else
            cout << "even" << endl;
    }
    return 0;
}

example

one! two!!

attention

attention !_!

  • 如大括號裡只有一行指令,可以縮排去大括號
  • 區域變數只能在宣告處以內的地方使用
#include<iostream>
using namesapce std;
int a=5;
int main(){
    int x=1;
    for(int i=0; i<3; i++){
        cout<< i<< " "; //ok
        cout<< x<< " "; //ok
        cout<< a<< " "; //ok
    }
    cout<< i; //X
    return 0;
}

ARRAY

by jumping

#class5_1112

CONTENTS

  • 陣列 array 介紹
  • 一維陣列   arr[ ]
  • 二維陣列   arr[ ][ ]

REVIEW

回憶一下ㄅ

#上上週 我們上了甚麼 ??

while loop

while(condition){ //條件
    statement; //內容
    //do something...
}
int i=5;
while(i){
    cout<< i <<" ";
    i--;
}

for loop 

for((宣告)初始化變數; 條件; 更新){
    statement;  //內容
    //do something...
}
for(int i=5 ; i>=1 ; i--){
    cout<<i<<" ";
}
for(int i=0; i<5; i++){ //外層迴圈
    for(int j=0; j<5; j++){ //內層迴圈
    	cout<< i << j <<endl;
    }
}

nested loop 

巢狀

 

ARRAY

老師你錯了 是從0開始不是1 -- 陣列

Why we need array?_?

 

Q:如果我是天天的小老師,

今天天天要我幫他登記班上10個人的成績,

而且還規定我一定要用程式登記,

(對她就是這麼固執一定要用程式)

我可以怎麼做?

A:我可以

int s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;

Why we need array?_?

 
int s1
int s2
.........
int s100
for(int i=0;i<100;i++){
    int si;
}

Q:可是今天天天說我10個人登記太快了,

他變成要我登記3個班共100個人的成績,

而且仍舊規定我要用程式登記,

我該怎麼辦呢?

A:我想我可能可以....

都不行ㄚ!!!

one-dimensionel

array

一維陣列

declare an array

 
  • 資料型態 變數名稱[儲存空間];
int a[10]; 
// 資料型態: 整數(陣列), 陣列名稱: a, 陣列大小: 10  
// 沒有初始化的陣列
// 裡面也許長這樣 a = {-2147483648, 2147483657, ...}
double b[100] = {0.1, 0.1, 0.2};
// 資料型態: 浮點數(陣列), 陣列名稱: b, 陣列大小: 100
// 陣列值用大括號括
//除了初始化的3個數其他預設為0
char str[6] = {'a'};
// 資料型態: 字元(陣列), 陣列名稱: str, 陣列大小: 6
// 字元陣列會包含最後一個字元'\0', 因此開的大小要字數+1
// 初始化為str[0]為a,後面為空白

//我可以用array來儲存這100個資料!

array!

 
  • 用一個陣列a[7]來儲存我的資料:

a[0]

a[2]

s[5]

s[6]

a[1]

a[4]

  • 注意! 陣列的第一格是從0開始!
  • [ ]中數字為我的儲存空間大小,不是最後一格的編號

a[3]

存取陣列

 
  • 利用迴圈
int a[7];
for(int i=0;i<7;i++) a[i]=i;

a[0]

a[2]

s[5]

s[6]

a[1]

a[4]

a[3]

0

6

5

4

3

2

1

attention

 
  • 不要存取不在陣列空間內的值
  • 如果打成下面那樣的話OJ會出現RE(Runtime Error)
  • 電腦會當機
int a[10];
cout<<a[100]<<endl;
cout<<a[1000]<<endl;

attention

 
  • 陣列的長度在宣告後就固定不變了
  • 那有些人會想說要打成這樣
  • 不建議
int n;
int a[n];

score

 
  • 回到剛剛那題
  • 我要用一個陣列s[100]來儲存我的資料:

s[0]

s[2]

s[98]

s[99]

s[1]

.............

s[97]

int s[100];
for(int i=0;i<100;i++){
    cin>>s[i];
}

practice

 
  • 上課練習題目:
  • 回家練習題目:

THE SIXTH CLASS

by jumping

#class6_1217

Multidimensionel

array

多維陣列

Why we need 二維陣列?_?

 

Q:今天天天要我幫他登記班上10個人的成績,

而且越來越過分

竟然要我一次登記一個人兩科的成績

重點是叫我登記他們國文跟數學的成績

(奇怪了跟他有什麼關係)

我們可以怎麼做?

A:我可以

int chinese[10],math[10];

Why we need 二維陣列?_?

 

Q:今天天天要我幫他登記班上10個人的成績,

但竟然說要身家調查

竟然要我一次登記一個人全科的成績

(他是不是偷偷喜歡哪個學生)

我們可以怎麼做?

A:我可以

int chinese[10],math[10],english[10],social[10],science[10];

declare an array

 
  • 資料型態  變數名稱[儲存空間][儲存空間]....
  • 幾維就開幾格
int a[2][5];
double b[7][1][2];

(0,0)

(0,0)

(1,4)

(0,4)

(1,3)

(1,2)

(0,3)

(0,2)

(1,1)

(1,0)

(0,1)

存取陣列

 
  • 巢狀迴圈
int a[3][3];
for(int i=0;i<3;i++){
    for(int j=0;j<3;j++) 
    	a[i][j]=i+j;
}
0 1 2
0
1
2

0

4

3

3

2

2

1

2

1

practice

 

小提示

 
for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)
    {
        cin>>a[i][j];
    }
}//輸入陣列
for(int i=0;i<a;i++){
    for(int j=0;j<b;j++)
    {
        cout<<a[c][d]<<" ";
    }
    cout<<endl;
}//翻轉陣列

5

2

6

1

4

9

5

1

2 

4

6

9

STRING

by jumping

#class7_1217

CONTENTS

  • escape character 跳脫字元
  • char 字元
  • string 字串

ESCAPE CHARACTER

跳脫字元

Why we need 跳脫字元?_?

 

Q:今天天天想用程式跟別人揮手手

       他想輸出"\o_o/"

cout<<""\o_o/""<<endl;

A:你可能會想

escape character ?_?

  • 跳脫字元 指的是 脫離原字元的意思
  • 通常是一個反斜線(\)加上一個字元或符號
  • 在字串內使用

escape character ?_?

字元 意義 字元 意義
\' 單引號' \t 定位字元tab
\" 雙引號" \b 倒退backspace
\\ 反斜線\ \n 換行enter
\0 空字元NULL \a 發出一聲警告

跟endl一樣,只是放的位置不同

example

#include<iostream>
using namespace std;
int main(){
  cout<<"                            _\n";
  cout<<"                          .\' `\'.__\n";
  cout<<"                         /      \\ `\'\"-,\n";
  cout<<"        .-\'\'\'\'--...__..-/ .     |      \\\n";
  cout<<"      .\'               ; :\'     \'.      |\n";
  cout<<"     /                 | :.       \\   o  \\\n";
  cout<<"    ;                   \\\':.      /  ,-.__;.-;`\n";
  cout<<"   /|     .              \'--._   /-.7`._..-;`\n";
  cout<<"  ; |       \'                |`-\'      \\  =|\n";
  cout<<"  |/\\        .   -\' /     /  ;         |  =/\n";
  cout<<"  (( ;.       ,_  .:|     | /     /\\   | =|\n";
  cout<<"   ) / `\\     | `\"\"`;     / |    | /   / =/\n";
  cout<<"     | ::|    |      \\    \\ \\    \\ `--\' =/\n";
  cout<<"    /  \'/\\    /       )    |/     `-...-`\n";
  cout<<"   /    | |  `\\    /-\'    /;\n";
  cout<<"   \  ,,/ |    \\   D    .\'  \\\n";
  cout<<"    `\"\"`   \\  nnh  D_.-\'L__nnh\n";
  cout<<"            `\"\"\"`\n";
  return 0;
} 
	

Character string 

字元字串

Char 

Char

  • char 儲存一個字元的資料型態
  • char 陣列 == 字串
  • 最後面存'\0',所以空間要比你的資料多一格
char a='s';
cout<<a;  //s
char b[7]="ZSISC";
a[0] a[1] a[2] a[3] a[4] a[5] a[6]

Z

C

S

I

S

\0

未知

function

  • #include <csting>  c++
  • #include <string.h>  c
strlen(s)
strcmp(s1,s2)
strcat(s1,s2)
strcpy(s1,s2)
strstr(s1,s2)

指令

說明

回傳s1的長度

比較s1&s2的內容,一樣回傳0

將s2接到s1後面

將s2的值複製到s1上

回傳s2第一次出現在s1的位置

ASCII

  • American Standard Code for Information Interchange
  • 每個字元皆能對應到一個數字
char a=65;
char b=48;
cout<<a;
//A
cout<<b;
//0
cout<<a+b;
//113
a=a+2;
cout<<a;
//C
char c[10]="ZSISC27";
cout<<c+2;
//ISC27;

String

String

  • string 儲存字串的資料型態
  • 每一格對應到一個字元
  • 不須宣告大小
  • 要先 include <string>
  • 可和數學一樣做運算
string a="ZSISC";
cout<<a;  //ZSISC
cout<<a[2]; //I
a[0] a[1] a[2] a[3] a[4] a[5] a[6]

Z

C

S

I

S

對比 char && string

strlen(s) s.length( ) or s.size( )
strcat(s1,s2) s1+s2
strstr(s1,s2) s1.find(s2,0)
strcpy(s1,s2) s1=s2
strcmp(s1,s2) s1==s2

字串長度

字串串接

char

string

字串尋找

字串複製

字串比較

practice

總複習題庫

ZEROJUDGE

輸出輸入

基本運算子

條件判斷

ZEROJUDGE

迴圈

一維陣列

二維陣列

字元字串

FUNCTIONS

by h94usu/6

#class2_0311

CONTENTS

  • What is "function"?
  • How to make + use?
  • Some examples?

讓我們先釐清一下

What is a Function?

function??

  • 當動詞 -> 工作、運作、起作用
  • 當名詞 -> 工作、運行/ 函式

Function in Math

  • 我們學過的函數
    • 小五學過未知數 概念 -> ( )
    • 小六代入 未知數 -> X, Y
    • 七下學到 函數

Function in Programming

  • 其實國小就學過了??  ( : 會重複做相似的事)
    • 命名 函數與決定 參數
    • 「做」你的函數 -> 定義 你的函數要幹嘛
    • 呼叫 (+給值)

Function in C++

  • 差不多一樣,表示方式不同
    • 命名 函數與決定 參數
    • 「做」你的函數 -> 寫(code) 你的函數要幹嘛
    • 呼叫 (+給值)

#include<iostream>

int func(int a, int b) {
	return a+b; 
}

int main() {
    std::cout << func(2, 3);
    return 0;
}

in Math

  • 給函數 一個數,他 另一個數給你
  • 通常寫 f(x) 代表 f 吃了x 以後吐出來的東西
  • 常見函數: sin、cos、log、...

in Programming

  • 給函數 一個東西,他 另一個東西給你
  • 通常寫 函數名稱(參數) 代表函數吃了參數後吐出來的東西
  • 常見函數: sort、max、...

Life is action, not equal to death.

Make it & Use it

make a Function

  • 不久前剛學到:
  • TYPE:函式 回傳值的型態,即 SOMETHING 的型態
  • FUNCTION:函式的名字
  • (...):傳入的參數
  • return:回傳,函式跑到這邊即結束,回傳SOMETHING
TYPE FUNCTION(TYPE PARAMETER1, TYPE PARAMETER2, ..) {
    //do something...
    
    return SOMETHING;
}

make a Function

TYPE FUNCTION(TYPE PARAMETER1, TYPE PARAMETER2, ..) {
    //do something...
    
    return SOMETHING;
}
  • TYPE:型態 (SOMETHING的型態)

example

int func(int a, int b) {
    int c = a+b;
    return c; 
}
  • 可以 不要有 參數||回傳值
  • 多個參數用 逗號 分開,每個都要寫 型態
  • 回傳值型態與定義的 相同
  • 大括號 就.. 隨意xd
TYPE FUNCTION(TYPE PARAMETER1, TYPE PARAMETER2, ..) {
    //do something...
    
    return SOMETHING;
}
void func() {
    cout << "HelloHa";
}

Use the Function -> call it

int main() {
    TYPE result = FUNCTION(PARAMETER);
    return 0;
}
  • 就跟 y = f(x) 一樣
  • 語法: 名稱(參數);
TYPE FUNCTION(TYPE PARAMETER1, TYPE PARAMETER2, ..) {
    //do something...
    
    return SOMETHING;
}

example

int func(int a, int b) {
    int c = a+b;
    return c; 
}

int main() {
    int ans;
    ans = func(5, 6);
    cout << ans;
}

//Output: 11
  • 回傳值型態與變數 相同
  • 呼叫沒有要傳入參數的函數還是要有小括號 ();
void func() {
    cout << "HelloHa";
}

int main() {
    cout << func();
}

//Output: HelloHa

practice

  • 實作 max 函數:
    • 傳入兩個整數
    • 回傳比較大的整數
    • 到main(主函式) 印出來
#include<iostream>
using namespace std;

??? max() {
    //???
    return ??;
}

int main() {
    //???
    cout << ??;
    return 0;
}

前人栽樹,後人乘涼

Examples

use others'

  • C++ 函數庫(library)有各種現成的函數,只要 #include 就能用
  • 想知道有哪些函數?

examples

  • 一些常用好用函數
#include <iostream>
#include <algorithm> // sort, max, swap
#include <cmath> // sqrt
using namespace std;

int main(){
  int a = 9, b = 10, arr[3] = {0, 3, 2};
  sort(arr,arr+3);
  cout << arr[2] << endl; // 3
  cout << max(a, b) << endl; // 10
  swap(a, b);
  cout << a << endl; // 10
  cout << sqrt(b) << endl; //3 
}

try try see

PLAY

try try see

RECURSION

by 阿問

#class4_0325

RECURSION

以我的名字呼喚我呼...喚我呼喚我 --遞迴

Recursion...?

  • 將大問題分割成小問題
  • 在函式中呼叫自己
  • 定義終止條件,最後將小問題的解組合成大問題
int f(int n){
    if(...)...
    ...
    else f(n-1);
    ...
}

常見應用

  • 階乘
  • 河內塔
  • 費氏數列
  • 最大公因數
  • 帕斯卡三角形

階乘

N!

​fac(5)

int fac(int n){
    if(n==1)return 1;
    else return n*fac(n-1);
}

​5*fac(4)

​(n=5)

​4*fac(3)

3*fac(2)

2*fac(1)

1

cout<<fac(5);
//5*4*3*2*1
//120

費氏數列

費氏數列

0,1,1,2,3,5,8,13,21,34,55,89,144,233......

​F(5)

int F(int n){
    if(n==0)return 0;
    if(n==1)return 1;
    else return F(n-1)+F(n-2);
}

​(n=5)

cout<<F(5);
//1+0+1+1+0+1+0+1
//5

費氏數列

​F(4)

​F(3)

​F(3)

​F(2)

​F(2)

​F(1)

​F(2)

​F(1)

​F(1)

​F(0)

​F(1)

​F(0)

1

​F(0)

​F(1)

​0

​0

​0

1

1

1

1

最大公因數

(GCD)

輾轉相除法

  • 一種快速求出最大公因數的方法

34

10

3

30

4

8

2

2

2

4

0

GCD

​gcd(34,10)

int gcd(int a,int b){
    if(a%b==0)return b;
    return gcd(b,a%b);
}

​gcd(10,4)

​gcd(4,2)

2

cout<<gcd(34,10);
//2

34

10

3

30

4

8

2

2

2

4

0

GCD

​gcd(34,10)

int gcd(int a,int b){
    if(a%b==0)return b;
    return gcd(b,a%b);
}

​gcd(10,4)

2

cout<<gcd(10,34);
//2

34

10

3

30

4

8

2

2

2

4

0

數字順序前後會影響嗎?

​gcd(10,34)

​gcd(4,2)

:不會

遞迴的優缺點

  • 優點:

        程式碼簡潔

  • 缺點:

​        占用大量記憶體空間、

        執行時間長

PRACTICES

Practice

  • 用遞迴寫出計算1+2+......+n的程式
    • 輸入整數n,傳入函式後回傳並輸出1~n之總和
#include <iostream>
using namespace std;
??? sum(){
    if(???)???;
    else return ???;
}
int main(){
    ???;
    cout<<???;
    return 0;
}

Zero Judge

a024

b127

c002

進階一點

b190

POINTER

by h94usu/6

#class3,5_03180401

CONTENTS

  • Variables?
  • Pointers?
  • sizeof()?
  • Functions?

蝦? 變數??

VARIABLES

第0個櫃子

第1個櫃子

第2個櫃子

「放學校的書的櫃子」

「放食物的櫃子」

「放『每個人都會有一些不可告人的東西』的櫃子」

放了國文課本

放了「來自熊媽媽買菜網的一顆雞蛋」

放了 ??????

variables??

  • 有個表格的 fu

variables??

  • 簡化簡化
它在哪裡? 它是什麼? 它放了什麼?
第0個櫃子 (0x00) 放學校的書的櫃子 國文課本。
第1個櫃子 (0x01) 放食物的櫃子 一顆雞蛋。
第2個櫃子 (0x02) 放『每個人都會有一些不可告人的東西』的櫃子 ?????

地址

通常用十六進位表示。

變數名稱

放/存了 什麼

variables??

  • so 它背地裡到底做了甚麼
int a = 5;
  • 就這個短短的一行
  • 那麼簡單的宣告

宣告流程

  • 找一個沒人用的櫃子
  • 讓別人知道a在哪裡
  • 然後把值放進去
int a = 5;
它在哪裡? 它是什麼? 它放了什麼?
0x00000
int x 7
0x00001 空的。 沒放東西。
0x00002 空的。 沒放東西。

int a

5

指標 好難!!

POINTERS

operators??

  • 取址 符號 --> &a   (a 是變數名稱)
  • 存址 型態  --> T *  (T 是指型態)
  • 取值 符號 --> *a    (a 是變數名稱)

好 很簡單 目前都非常簡單

跟文科一樣

背起來就會有分數??????

Just use it  -> &

  • 取址 符號 --> &a   (a 是變數名稱)
  • 得變數的 記憶體位
#include<iostream>
using namespace std;

int main(){
    int a;
    cout << &a;  //(maybe)0x70fe1c a的記憶體位址
    return 0;
}

Just use it  -> T *

  • 存址 型態 --> T *    (T 是型態) 
  • 變數的 記憶體位
#include<iostream>
using namespace std;

int main(){
    int a;
    int *p;
    p = &a;
    cout << p;   //(maybe)0x70fe1c 一樣是a的記憶體位址
    return 0;
}

你就把 int * 當成一種存記憶體位址的 型態,就跟int, double 一樣

但注意,指標形態 T 要跟其要存的變數 a  一樣

Just use it  -> *

  • 取值 符號 --> *a   (a 是變數名稱)
  • 得記憶體的位址存的
#include<iostream>
using namespace std;

int main(){
    int a = 5;
    int *p = &a;
    cout << *p;   //5 
    //取記憶體P的值,也就是取a記憶體&a的值,也就是5
    return 0;
}

edit value || operate

  • 取值 -> 改值
  • 就是... 改值
#include<iostream>
using namespace std;

int main(){
    int a = 5;
    int *p = &a;
    *p = *p+1;
    cout << a;   //6 
    //改記憶體P的值,也就是取a記憶體&a的值,也就是把5改成5+1=6
    return 0;
}

attention!!

  • int* 的 * 跟 取值 符號的 * 不要搞混!!
    • int * 的 *型態 的一部分 (拿來當宣告用)
    • *p 的 * 一個是 取值符號(拿來使用、運算)
#include<iostream>
using namespace std;

int main(){
    int a = 5;
    int *p = &a;
    *p = *p+1;
    cout << a;   //6 
    //改記憶體P的值,也就是取a記憶體&a的值,也就是把5改成5+1=6
    return 0;
}

attention!!

  • 有些坑... 請重複打 *
#include<iostream>
using namespace std;

int main(){
  int* a, b;
  // 上下兩個等價
  int *a;
  int b;
  
  // 所以宣告兩個指標要
  int *a, *b;
    return 0;
}
  • 宣告 時,變數前加上 *,此變數為一個 指標
    • *不要跟著型態,會搞混。(int *a,*b;) (int* a, b;)
  • 使用 時,變數前加上 &,其值為此變數的 位址
  • 使用 時,指標前加上一個 *,其值即為位在此位址之變數

conclusion

是不是比想像中簡單呀~

大小

SIZEOF

sizeof??

  • 如果我們想知道某個東西的大小 (bytes)
  • 就會用到 sizeof()

1 Byte = 8 bits

1 bit 就是一個 0或1。

所以一個byte 就是 00000000 ~ 11111111

通常bit都會八個一組,我們稱之為byte。

size??

  • 各個型態大小?
#include <iostream>
int is_admin = 0;
int ary[160];
int main(){
  std::cout << sizeof(char) << std::endl;
  std::cout << sizeof(int) << std::endl;
  std::cout << sizeof(long long) << std::endl;
  std::cout << sizeof(float) << std::endl;
  std::cout << sizeof(int *) << std::endl;
  std::cout << sizeof(double *) << std::endl;
}
// 1 4 8 4 8 8

阿不是教過函式了ㄇ..

FUNCTIONS

pass by value

  • 傳進去的東西只是 副本
  • 不會實際改到你傳進去的值
#include<iostream>
using namespace std;

void f(int A, int B) {
	int tmp = A;
	A = B;
	B = tmp;
    cout << A << " " << B << endl;  //2 1
}

int main(){
	int a = 1, b = 2;
	f(a, b);
	cout << a << " " << b;  //1 2
	return 0;
}

pass by pointer(address)

  • 一堆取值存址取址
  • 又醜又麻煩
#include<iostream>
using namespace std;

void f(int *A, int *B) {  //A, B 是 a, b的記憶體位址 //存址型態
    int tmp = *A;  //取A記憶體位址的值,也就是a的值  //取值A
    *A = *B;  //把A記憶體位址的值(a),改成B記憶體位址的值(b)  //取值A,B
    *B = tmp;  //把B記憶體位址的值(b),改成tmp  //取值B
    cout << *A << " " << *B << endl;  //2 1  //取值A,B
}

int main(){
    int a = 1, b = 2;
    f(&a, &b);  //傳入a, b的記憶體位址  //取址a,b
    cout << a << " " << b;  //2 1
    return 0;
}

pass by reference

  • 簡單明瞭
  • &A -> 「別稱」reference
#include<iostream>
using namespace std;

void f(int &A, int &B) {
	int tmp = A;
	A = B;
	B = tmp;
	cout << A << " " << B <<endl;
}

int main(){
	int a = 1, b = 2;
	f(a, b);
	cout << a << " " << b;
	return 0;
}

STRUCT

by jumping

#class6_0408

STRUCT

by jumping

#class6_0408

那就讓我們開始ㄅ

START

CONTENTS

  • ..?
  • Pointer in struct!

DECISION

  • take classes
  • self-study

OMG pointer+struct!!

POINTER IN STRUCT

OMG pointer+struct!!

POINTER IN STRUCT

pointer

  • 還記得指標嗎🤔🤔
  • 三個新東西: *a, &a, T *  (a是變數,T是型態)
#include<iostream>
using namespace std;

int main(){
    int a = 5;
    int *p = &a;
    *p = *p+1;
    cout << a;   //6 
    //改記憶體P的值,也就是取a記憶體&a的值,也就是把5改成5+1=6
    return 0;
}

struct

  • 如果想要把你這個 "型態" 的不同變數連在一起呢🤔
  • 以家人做🌰,我想要知道a 的父母的名字
#include<iostream>
using namespace std;

struct person{
	string name;
	person father, mother, child;
};

int main(){
	person A, B, a;
	A.name = "alice";
	B.name = "kelly";
	a.father = A;
	a.mother = B;
	cout << a.father.name << " " << a.mother.name;
	return 0;
}

pointer in struct

#include<iostream>
using namespace std;

struct person{
    string name;
    person *father, *mother, *child;  //存他親人的記憶體位址
};

int main(){
    person A, B, a;
    A.name = "alice";
    B.name = "kelly";
    a.father = &A;  //存他父親A的記憶體位址
    a.mother = &B;  //存他母親B的記憶體位址
    cout << (*a.father).name << " " << (*a.mother).name;  //kelly alice
    //a的father(存A的位址)型態是person *,取值轉成person型態,再找他的name
    return 0;
}

->

  • 但是,這一長串好複雜 "(*a.father).name"
  • (*a.father).name == a.father->name
#include<iostream>
using namespace std;

struct person{
	string name;
	person *father, *mother, *child;
};

int main(){
	person A, B, a;
	A.name = "alice";
	B.name = "kelly";
	a.father = &A;
	a.mother = &B;
	cout << a.father->name << " " << a.mother->name;  //kelly alice
	return 0;
}

SORT

by 阿問

#class6_0415

SORT

Sort?

  • 將一群資料照我們想要的順序排
  • 常用技巧
  • 二分搜-前置

EXAMPLE

以由小排到大為例

Bubble Sort

  • 重複走訪陣列,若相鄰元素次序錯誤就交換,直到排序完成

Bubble Sort

#include <iostream>
using namespace std;
int main(){
    int a[10]={7,1,8,6,2,3,9,10,4,5},n=10;
    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-1-i;j++){
            if(a[j>a[j+1]]){
                int temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
}
//1 2 3 4 5 6 7 8 9 10

Selection Sort

  • 將陣列分成已排序與未排序兩部分,每次從未排序的陣列中找出最小值放到已排序的陣列後
  • (依序找出最小 第二小 第三小...)

Selection Sort

#include <iostream>
using namespace std;
int main(){
    int a[10]={7,1,8,6,2,3,9,10,4,5},n=10;
    for(int i=0;i<n-1;i++){
        int min_idx=i;
        for(int j=i+1;j<n;j++){
            if(a[j]<a[min_idx])min_idx=j;
        }
        int temp=a[i];
        a[i]=a[min_idx];
        a[min_idx]=temp;
    }
}
//1 2 3 4 5 6 7 8 9 10

Insertion Sort

  • 將陣列分成已排序與未排序兩部分,陣列第一個元素為已排序,每次取未排序的陣列中的第一個元素,找到它應該放在未排序陣列中的哪個位置

Insertion Sort

#include <iostream>
using namespace std;
int main(){
    int a[10]={7,1,8,6,2,3,9,10,4,5},n=10;
    for(int i=1;i<n;i++){
        int num=a[i];
        int j=i-1;
        while(j>=0&&a[j]>num){
            a[j+1]=a[j];
            j--;
        }
        a[j+1]=num;
    }
}
//1 2 3 4 5 6 7 8 9 10

SORT in STL

sort()

  • C++中有提供sort()函式
  • 需#include <algorithm>
  • 預設由小排到大
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int a[10]={7,1,8,6,2,3,9,10,4,5};
    sort(a,a+10); //1 2 3 4 5 6 7 8 9 10
    int b[10]={7,1,8,6,2,3,9,10,4,5};
    sort(b,b+5); //1 3 6 7 8 3 9 10 4 5
}

sort()

  • 如果想從大排到小...?
  • 可以自訂比較函式
  • 可搭配struct使用
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int a[10]={7,1,8,6,2,3,9,10,4,5};
    sort(a,a+10,cmp); //10 9 8 7 6 5 4 3 2 1
}

Practices

ZEROJUDGE

a104

a225

b964

a915

by jumping

#class8_0422

Binary Search

如果我今天有一堆編號不同(無重複)的卡牌,我要從中找到我要找的數字對應到的卡牌

我可以

int a[8]={6,22,5,14,9,65,54,87};
int number;
cin>>number;
for(int i=0;i<8;i++)
{
    if(a[i]==number)
    {
        cout<<i<<endl;
        break;
    }
}

但如果我今天有一萬張卡牌呢?

線性搜尋法

Liner Search

概念

int a[8]={0,2,5,4,9,65,54,87};
sort(a,a+8);
for(int i=0;i<8;i++) cout<<a[i]<<" ";
cout<<endl;
int number;
cin>>number;
for(int i=0;i<8;i++)
{
    if(a[i]==number)
    {
        cout<<i<<endl;
        break;
    }
}

但如果我要找的數字在最後面或根本找不到呢?

實作

實作

實作

二分搜尋法

Binary Search

概念

example

流程

實作

int binary_serch(int arr[],int length, int target) {
    int L = 0, R = length - 1;
    while(L<=R)
    {
        int M = floor((L+R)/2);
        if(arr[M]==target) return M;
        else if(arr[M]>target) R = M - 1;
        else L = M + 1;
    }
    return -1;
}
Made with Slides.com