C++基礎語法

陣列

一維陣列

x[0] x[1] x[2] x[3]
#include <iostream>
using namespace std;

int main(){
    int x[4];
    
    for(int i=0 ; i<4 ; i++){
        cin >> x[i];
    }
    
    for(int i=0 ; i<4 ; i++){
        cout << x[i] << " ";
    }
}

二維陣列

x[0][0] x[0][1] x[0][2]
x[1][0] x[1][1] x[1][2]
#include <iostream>
using namespace std;

int main(){
    int x[2][3];
    
    for(int i=0 ; i<2 ; i++){
        for(int j=0 ; j<3 ; j++){
            cin >> x[i][j];
        }
    }
    
    for(int i=0 ; i<2 ; i++){
        for(int j=0 ; j<3 ; j++){
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

練習看看!

ZJ a058 - MOD3

ZJ c067 - Box of Bricks

ZJ a015 - 矩陣翻轉 (二維陣列)

ZJ a693 - 吞食天地 (前綴和)

ZJ d562 - 山寨版磁力蜈蚣 (稍難)

陣列基本上是所有東西的核心

所以一定要弄熟悉喔喔喔

字串

首先要先了解字元

在C++中文字的儲存是用ASCII編碼

有英文字母、數字、空白、特殊符號...

變數型別為char,用單引號括起來,

單引號內只能放一個字元

#include <iostream>
using namespace std;

int main(){
	
    char a = 'A', b = '5';
    
    cout << a << ' ' << b; // A 5
    
    cin >> a >> b;

}

字元也能做運算

#include <iostream>
using namespace std;

int main(){
	
    char x = 'A';
    
    x += 1;
    
    cout << x << endl; // B
    
    cout << (int)x << endl; // 66

}

字元其實有他自己代表的值,

只是他呈現出來是字元而不是數字

字元陣列即字串(的簡單版)

#include <iostream>
using namespace std;

int main(){
	
    char s[10000] = "Hello OAOAO";
    char c[10000] = {'H', 'e', 'l', 'l', 'o', ' ', 'O', 'A', 'O', 'A', 'O'};
}

但它比較屬於C語言,所以暫時先不講

字串是用雙引號括起來的一堆字元

在字串尾端的下個字元會自動加上'\0',為空字元

C++的字串

#include <iostream>
using namespace std;

int main(){
	
    string a, b;
    
    cin >> a;
    cout << a << endl;
    cout << a[0] << endl; // 可以直接存取第i個字元
    
    int n = a.size(); //取得字串長度
    
    a = "123", b = "abc"; //=一樣是賦值
    string c = a + "www" + b; // 可以直接用+把字串串接起來
    cout << c << endl; // 123wwwabc
    
    cout << (a == b) << endl; //可以比較字串是否相同、字典序
}

字串是用雙引號括起來的一堆字元

型別為string

練習題!

ZJ d235 - You can say 11

ZJ a224 - 明明愛明明

ZJ a065 - 提款卡密碼

ZJ a022 - 迴文

ZJ d139 - Compressed String

函式

\(y = f(x) = \) ...

架構

 回傳型態 函式名稱(傳入的參數型態 參數名稱, ...) { 

         // blablabla

         耶回傳東西;

 }

放在全域、main的上面。

加起來拉

#include <iostream>
using namespace std;

int add(int a, int b){

    return a + b;

}

int main(){
    int x, y;
    cin >> x >> y;
    
    int z = add(x, y);
    cout << add(x, y) << endl;
}

回傳void

#include <iostream>
using namespace std;

void what(int x){
    x = 0;
    // 不寫
    return;
    return void();
}

int main(){
    int qq;
    qq = 1;
    
    what(qq);
}

遞迴! n!

#include <iostream>
using namespace std;

int f(int n){
	
    if(n == 1) return 1;
    
    return n * f(n-1);

}

int main(){
    int x;
    cin >> x;
    cout << f(x);
}

***要有遞迴

終止條件!!!!!

吠是數列 遞迴版

#include <iostream>
using namespace std;

int f(int n){
	
    if(n == 1 || n == 2) return 1;
    
    return f(n-1) + f(n-2);

}

int main(){
    int n;
    cin >> n;
    cout << f(n);
}

\(\begin{cases} f_1 = f_2 = 1 \\ f_n = f_{n-1} + f_{n-2}, \ n \geq 3 \end{cases}\)

遞迴の練習

ZJ c002 - f91

\(\begin{cases} f(x) = f(f(x+11)), \ x \leq 100 \\ f(x) = x-10, \ x > 100 \end{cases}\)

ZJ a216 - 數數愛明明

\(\begin{cases} f(1) = g(1) = 1 \\ f(x) = x + f(x-1) \\ g(x) = f(x) + g(x-1)\end{cases}\)

//其實很明顯可以化簡一些東西

Struct

結構變數

包起來

#include <iostream>
using namespace std;

struct OAO{

    int a;
    double b;
    char c;

} ;

int main(){


}

一樣放全域,要有分號!

宣告、存取

#include <iostream>
using namespace std;

struct OAO{

    int a;
    double b;
    char c;

} ;

int main(){
    OAO yee, ooo;
    
    cin >> yee.a >> yee.b >> yee.c;
    
    cout << yee.a << yee.b << yee.c;
}

也可以開成陣列、放在全域

#include <iostream>
using namespace std;

struct OAO{

    int a;
    double b;
    char c;

} arr[100];

OAO haha[1000];

int main(){
    for(int i=0 ; i<100 ; i++){
        cin >> arr[i].a >> arr[i].b >> arr[i].c;
    }
}

一點點的指標

變數的組成

型態 名稱 在記憶體中的位址
e.g. int x 0x?????? (16進位)

指標就是用來存變數的位址的OAo

宣告

#include <iostream>
using namespace std;

int main(){

    int x = 2;
    int *p;
    p = &x;
    
    cout << x << endl; // 2
    cout << &x << endl; // 0x??????
    cout << p << endl; // 0x??????
    cout << *p << endl; // 2

    *p = 5; // 等價於 x = 5;
}

指標型別   *指標名稱;

&為取位址,*為讀取位址上的值

注意的地方

#include <iostream>
using namespace std;

int main(){

    int *p, q; // (X), q仍為int型別變數
    
    int *x, *y; // (O)
    
    int *a[100]; // (O)

}

如果要一次宣告好幾個指標

動態記憶體配置

Linked List, Segment Tree, Treap...

其他重要的東西

兩數交換

#include <iostream>
using namespace std;

int main(){

    int a, b;
    cin >> a >> b;
    
    int c = a;
    a = b;
    b = c;
    
    cout << a << " " << b << endl;

}

假如要交換 \(a, \ b\) 兩數

能直接 \(a = b; \ b = a;\) 嗎?

傳參考(Reference)

#include <iostream>
using namespace std;

void swap(int &a, int &b){
    int tmp = a;
    a = b;
    b = tmp;
}
int main(){
    int a, b;
    cin >> a >> b;
    
    swap(a, b);
    
    cout << a << " " << b;
}

有些題目有多筆測資

#include <iostream>
using namespace std;

int main(){
    int t;
    cin >> t;
    
    for(int i=0 ; i<t ; i++){
    	//做題目要做的事
    }
    
    while(t--){
        //做題目要做的事
    }
}

如果有給測資數 \(T\)

有些題目有多筆測資

#include <iostream>
using namespace std;

int main(){
    int x;
    while(cin >> x){
    	//做題目要做的事
    }
}

如果以EOF (End of File) 結束

語法差不多就到這邊辣:D

這樣高中的資訊課應該都可以躺了

之後就要進到演算法ㄉ世界ㄌ

有問題一定要問學長姊(?)喔喔喔

不然問題會像滾雪球一樣

加油啊阿阿,多多刷題OAO

OAO

By youou

OAO

  • 184