cjtsai
失蹤的c++殭屍
Lecturer: 蔡嘉晉 aka 明世宗 aka 世宗大王 aka 小菜 aka c++殭屍 aka 失蹤 aka cjtsai aka 西追蔡
你爽就好
建北電資聯合演算法小社課[1]
我推的戰棋 FireEmblemThreeHouses
讓我們從簡單一點的東西開始
ios_base::sync_with_stdio(false);
scanf printf
囉#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
}
cin.tie(0);
#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(0);
}
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
}
反正背起來就好
為甚麼第一個是false第二個是0?我也不知道欸
反正意思是一樣的
但這樣看起來很舒服
熟悉的那份簡報
value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
93 | 0 | 1 | 0 | 1 | 1 | 1 | 0 | 1 |
255 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|---|
178↓ | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
bitwise or | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |
84 ↑ | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 0 |
英文 | 符號 | 範例 | 結果 | |
---|---|---|---|---|
位元或 | bitwise or | | | 6 | 11 | 15 |
位元和 | bitwise and | & | 6 & 11 | 2 |
位元互斥或 | bitwise xor | ^ | 6 ^ 11 | 13 |
左移運算 | left shift | << | 5 << 3 | 40 |
右移運算 | right shift | >> | 1243 >> 3 | 155 |
要開始燒雞ㄌ
名 | var_a | var_b | var_c | var_d |
型態 | int | long long | string | char |
值 | 0 | 1 | "cjtsai" | 'r' |
位址 | 0x8071 | 0x8072 | 0x8073 | 0x8074 |
int main(){
int var_a=0;
cout<<var_a; //0;
cout<<&var_a; //0x8071;
var_a=101;
cout<<var_a<<' '<<&var_a; // 101 0x8071
} //我只是想說變數位址跟他自己的值沒關係
每次執行的變數都會存在不同位址
不同電腦也都會有些差異喔
但格式都差不多
int* a = &b
T* a = &b
名 | var_a | pt_a | var_c | pt_c |
型態 | int | int* | string | string* |
值 | 0 | 0x8071 | "cjtsai" | 0x8073 |
位址 | 0x8071 | 0x8072 | 0x8073 | 0x8074 |
int main(){
int var_a=0;
int* pt_a = &var_a;
cout<<var_a; //0
cout<<pt_a; //0x8071
cout<<*pt_a; //0
}
int main(){
std::string var_c="cjtsai";
std::string* pt_c = &var_c;
cout<<var_c; //"cjtsai"
cout<<pt_a; //0x8073
cout<<*pt_a; //"cjtsai"
}
名 | var_a | pt_a | var_c | pt_c |
型態 | int | int* | string | string* |
值 | 0 | 0x8071 | "cjtsai" | 0x8073 |
位址 | 0x8071 | 0x8072 | 0x8073 | 0x8074 |
持續燒雞中
int main(){
int var_a=0;
int& ref_a=var_a;
cout<<ref_a<<' '<<var_a; //0 0
ref_a=10;
cout<<ref_a<<' '<<var_a; //10 10;
}
int& a = b
T& a = b
兩個變數var_a 跟 ref_a 的值同步了!
名 | var_a | pt_a | var_c | pt_c |
型態 | int | int* | string | string* |
值 | 0 | 0x8071 | "cjtsai" | 0x8073 |
位址 | 0x8071 | 0x8072 | 0x8073 | 0x8074 |
int main(){
int var_a=0;
int& ref_a=var_a;
}
ref_a
名 | var_a | pt_a | var_c | pt_c |
型態 | int | int* | string | string* |
值 | 0 | 0x8071 | "cjtsai" | 0x8073 |
位址 | 0x8071 | 0x8072 | 0x8073 | 0x8074 |
int main(){
int var_a=0;
int& ref_a=var_a;
cout<<ref_a; //0
cout<<&var_a<<' '<<&ref_a; //0x8071 0x8071 一樣欸
}//再次複習 & 是用來取得某個變數的記憶體位址喔
ref_a
他們的記憶體位址
輸出後是同個欸
測驗
cout << a;
cout << &a;
cout << b;
cout << *b;
cout << &b;
cout << c;
cout << &c;
cout << *c;
cout << d;
cout << *d;
cout << **d;
cout << &d
cout << &*d
cout << &**d
int a = 10;
int *b = &a;
int &c = a;
int **d = &b;
參考自AaW建中店言大赦課簡報
int bigger_one;
if(a>b) bigger_one=a;
else bigger_one=b;
int get_bigger_one(int num1, int num2){
if(num1>num2){
return num1;
}else{
return num2;
}
}
return type
回傳型別
function name
函式名稱
arguments
參數(們)
return value
回傳值
int main(){
int a=3, b=4;
cout<<get_bigger_one(a, b); //4
}
在main呼叫函式:
btw int main() 也是個函式喔
return 0;就是在回傳值
只是那個回傳值會被當status code處理
return type
回傳型別
function name
函式名稱
arguments
參數(們)
return value
回傳值
就是你之後用什麼名字呼叫這個函式
這個函式最後得出的值並回傳的是什麼型別 e.g. int char string long long
比較特別的void 此代表本函式不會回傳任何值
也可以是不同的型別 每個不同的參數用逗號隔開 寫法為 型別 變數名
在這邊對每個參數賦予名字 接下來在函式中便使用此變數名進行運算
此變數名不必與呼叫時傳入之變數名相同
就是函數運算完畢時回傳一個值作為答案
呼叫return xxx; 同時結束此函數的所有運算
若回傳型別為void 亦可寫return; 此處代表結束此函數之執行
型別 變數名
void swap(int a, int b){
int c=a;
a=b;
b=c;
}
函式名稱 | swap | 因為目的是交換兩個東西 btw函式名稱請有意義的取 |
回傳型別 | void | 因為目的是交換兩個東西 而非對這兩個東西進行運算獲得特別的值 |
參數們 | int a, int b | 就是你要交換的兩個變數 |
回傳值 | 無 | 因為是void 自然就沒有回傳值 |
#include <bits/stdc++.h>
using namespace std;
void swap(int a, int b){
int c=a;
a=b;
b=c;
}
int main(){
int a=10, b=15;
cout<<"交換前: a="<<a<<", b="<<b<<'\n';
swap(a, b);
cout<<"交換後: a="<<a<<", b="<<b<<'\n';
}
???
我不是換掉了嗎
這就必須提到 變數作用域了
再次推vim
#include <bits/stdc++.h>
using namespace std;
void swap(int a, int b){
int c=a;
a=b;
b=c;
}
int main(){
int a=10, b=15;
cout<<"交換前: a="<<a<<", b="<<b<<'\n';
swap(a, b);
cout<<"交換後: a="<<a<<", b="<<b<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
void swap(int a, int b){
int c=a;
a=b;
b=c;
}
int main(){
int a=10, b=15;
cout<<"交換前: a="<<a<<", b="<<b<<'\n';
swap(a, b);
cout<<"交換後: a="<<a<<", b="<<b<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b){
int c=a;
a=b;
b=c;
}
int main(){
int a=10, b=15;
cout<<"交換前: a="<<a<<", b="<<b<<'\n';
swap(a, b);
cout<<"交換後: a="<<a<<", b="<<b<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
void swap(int* a, int* b){
int c=*a;
*a=*b;
*b=c;
}
int main(){
int a=10, b=15;
cout<<"交換前: a="<<a<<", b="<<b<<'\n';
swap(a, b);
cout<<"交換後: a="<<a<<", b="<<b<<'\n';
}
指標式
參考式
遞迴只應天上有 凡人應當用迴圈
--資訊之芽
#include <bits/stdc++.h>
using namespace std;
int fibo(int n){
if(n==1) return 1;
if(n==2) return 1;
return fibo(n-1)+fibo(n-2);
}
int main(){
cout<<fibo(5); //5
}
#include <bits/stdc++.h>
using namespace std;
int fibo(int n){
if(n==1) return 1;
if(n==2) return 1;
return fibo(n-1)+fibo(n-2);
}
int main(){
cout<<fibo(5); //5
}
這裡我們在定義fibo這個函數
但我們在定義的時候使用了fibo這個函數
這就是遞迴函式特殊的寫法
我叫 邊界條件
如果沒有邊界條件 呼叫了 fibo(1) 會發生甚麼事?
fibo(1)會去呼叫 fibo(0)跟fibo(-1) 但這完全不合理
所以我們要設定邊界 適當的邊界能使程式執行成功並且不會吃到一些錯誤
像是maximum recursion depth exceed之類的
注意函式要宣告在main函數外面
但是好像有個東西叫做lambda
你可以輕鬆寫出比這個好很多的fibo函式
但我們先別在意這個問題
#include <bits/stdc++.h>
using namespace std;
int fibo(int n){
if(n==1) return 1;
if(n==2) return 1;
return fibo(n-1)+fibo(n-2);
}
int main(){
cout<<fibo(5); //5
}
return 2;
return 3;
return 2;
return 5;
順帶一提
不覺得這個遞迴呼叫了很多次相同的東西嗎
如果可以記錄已經算過的項 紀錄狀態
是不是能快很多呢
但這就是以後的事了
好難用又好好用
struct person{
string name;
int seat_no;
int height;
int weight;
}; //<-我超重要 一定要加這個分號
struct person{
string name;
int seat_no;
int height;
int weight;
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=200;
yoyo.weight=10;
cout<<yoyo.name<<"的身高是: "<<yoyo.height;
}
struct person{
string name;
int seat_no;
int height;
int weight;
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=200;
yoyo.weight=10;
cout<<yoyo.name<<"的身高是: "<<yoyo.height;
}
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi=weight/(height*height);
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=2.0;
yoyo.weight=10;
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi;
}
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi=weight/(height*height);
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=2.0;
yoyo.weight=10;
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi;
}
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi(){
return weight/(height*height);
}
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=2.0;
yoyo.weight=10;
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi();
}
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi(){
return weight/(height*height);
}
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=2.0;
yoyo.weight=10;
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi();
}
struct
or}我沒有要講destructor
constructor中間包著一個struct欸
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi(){
return weight/(height*height);
}
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo;
yoyo.name="甲";
yoyo.seat_no=32;
yoyo.height=2.0;
yoyo.weight=10;
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi();
}
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi(){
return weight/(height*height);
}
person(string _name, int _seat_no, float _height, float _weight){
name = _name;
seat_no = _seat_no;
height = _height;
weight = _weight;
} //其實可以 string name ... name=name; 但那樣好醜
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo("甲", 32, 2.0, 10.0);
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi();
}
struct person{
string name;
int seat_no;
float height;
float weight;
float bmi(){
return weight/(height*height);
}
person(string _name, int _seat_no, float _height, float _weight):name(_name), seat_no(_seat_no), height(_height), weight(_weight){}
}; //<-我超重要 一定要加這個分號
int main(){
person yoyo("甲", 32, 2.0, 10.0);
cout<<yoyo.name<<"的bmi是: "<<yoyo.bmi();
}
往後滑➮
我發現一件很神奇的事情喔
今天講的東西每個都有一句
可以搭配以後的演算法使用
蔗糖小設課叫演算法小設喔
現在只是先在教語法的基礎
之後要趕進度ㄌ湊字數的我
根據我燒雞的物理段考 我覺得這個可能有點難達到靜力平衡
By cjtsai