C++ 基本語法

第一個程式

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}
#include "bits/stdc++.h"
using namespace std;

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

pre-processor statement前面加#

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

pre-processor statement前面加#

bits/stdc++.h 是一個header file

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

pre-processor statement前面加#

bits/stdc++.h 是一個header file

header file宣告程式的存在

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

pre-processor statement前面加#

bits/stdc++.h 是一個header file

header file宣告程式的存在

<> include path, "" relative files || include path

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

pre-processor statement前面加#

bits/stdc++.h 是一個header file

header file宣告程式的存在

<> include path, "" relative files || include path

namespace = 有名字的容器(裝輸入, 輸出之類的)

std

罐子

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}
#include "bits/stdc++.h"
using namespace std;

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

main 主函式 = entry point

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

main 主函式 = entry point

int = interger, 變數n為整數

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

main 主函式 = entry point

int = interger, 變數n為整數

cin輸入, >>是overloaded operator

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

for 迴圈 (initialize ; 條件; update )

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

for 迴圈 (initialize ; 條件; update )

if (條件) true 輸出(cout) 0, else false 輸出1

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

for 迴圈 (initialize ; 條件; update )

if (條件) true 輸出(cout) 0, else false 輸出1

operator - 乘(*), 除(/), 取餘數(%)

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

int main() {

    int n;

    cin >> n;

    for (int i=1; i <= (2*n + 1); i++) {

        if ( i % 2 == 0)
            cout << 0;
        else
            cout << 1;
    }

    return 0;
}

程式成功執行

資料型態

名稱 用途 程式名稱
interger 存整數 int
double 存浮點數(有小數) double
chararacter 存字元 char

補充型態

存取整數

int / long long / short

int的範圍是 : -2147483648 ~ 2147483647 (2^31 - 1)

▢ long long 的範圍很長(2^63-1)

▢ short 的範圍 ( 2^15 - 1)

* long int = long / short int = short

▢ long 的範圍 ( 2^31 - 1)(win) 或 (2^63-1)(linux)

存取浮點數

double / float

double 是浮點數的基礎單位 ( 8 bytes)

▢ float 是存取小範圍的浮點數

如果要預設值後面要寫f

ex : float a = 3.14159f

* 存取方式  : 

1 bit - 正/負 52 bit - 尾數 11 bit - 指數

題目

Made with Slides.com