Review

3

6

輸入輸出、

變數、運算子

標頭檔

#include <iostream> //將函式庫<iostream>引入
using namespace std; //省去命名空間(namespace)
int main(){ //主函式(Function)

	// 主程式碼

	return 0;//讓電腦知道程式要結束
}

#include<bits/stdc++.h> 萬用函式庫

註解

//單行註解

/*
多行
註解
*/

變數型別

型別 舉例
int (整數) 8、-6
long long (長整數) 10000000000、-10000000000
float (浮點數) 3.14
double (倍精度浮點數) 3.1415926535
char (字元) 'a'、'@'、'4'
string (字串) "Hello World"
bool (布林) true(1)、false(0)

宣告

變數型態 變數名稱 = 值

變數名稱禁用❌

1.特殊字(int,true)

2.除底線外(_)特殊符號(如@, #, $

3.開頭使用數字

int sum = 0;

輸入輸出

cin >> 變數名;

cout << 變數名;

多項輸入輸出

int a, b, c;
cin >> a >> b >> c;
cout << a << "\n" << b << "\n" << c << "\n";

輸入 : 1 2 3

輸出 : 1

           2

           3

換行: endl  '\n'

比較運算子 意義
> 大於
>= 大於等於
< 小於
<= 小於等於
== 等於
!= 不等於
算術運算子 意義
+
++ 遞增1
-
-- 遞減1
*
/
% 取餘數

運算子

邏輯運算子 意義
&& AND (且)
|| OR (或)
! NOT (否
int a=10,b;
b=a+5;
cout << b; //15

a+=1 //簡寫 a=a+1
cout << a; //11

int a=3;
bool q;
q=a<5;
cout << q; //1

int a=3;
bool q;
q= a!=0 && a>10;
cout<<q; //0

例子

條件判斷

if(A條件){
  //達成A條件時執行;
}
else if(B條件){
  //未達成A條件、達成B條件時執行;
}
else{
  //未達成B條件和A條件時執行;
}

模板

if (a=5) cout << "true" << endl;
if (a==5) cout << "true" << endl;

三元運算子

 情況a? 條件b : 條件c

當 a 為true則執行 b

當 a 為false則執行 c

#include <iostream>
using namespace std;
int main(){
    int score = 100;
    string level;
    level = score>=60 ? "excellent":"try again";
    cout << level << endl;;

}

輸出 excellent

迴圈

while

while(條件){
  達成條件時執行;
}
int score = 70;
while(score>=60){
	cout << "及格了!!!" << endl;
}

⚠️無窮迴圈

 

及格了 ! ! !  及格了 ! ! ! 及格了 ! ! ! 及格了 ! ! ! 及格了 ! ! ! 及格了 ! ! !  及格了 ! ! ! 及格了 ! ! ! 及格了 ! ! ! 及格了 ! ! !及格了 ! ! !  及格了 ! ! ! 及格了 ! ! ! ...

continue

for(int i=0;i<5;i++){
    if(i==3){
        continue;
    }
    cout << i << ' ';
}
cout << "end" << endl;

break

強制結束此次迴圈循環

for(int i=0;i<5;i++){
    if(i==3){
    	break;
    }
    cout << i << ' ';
}
cout << "end" << endl;

return 0

強制跳出迴圈

for(int i=0;i<5;i++){
    if(i==3){
    	return 0;
    }
    cout << i << ' ';
}
cout << "end" << endl;

強制結束程式

0 1 2 4 end

0 1 2 end

0 1 2

for迴圈

for(起始值; 條件式; 更新值){
	達成條件時執行;
}
for(int i=1;i<=5;i++){
	cout<<i<<" ";
}

1 2 3 4 5

巢狀迴圈

for(int i=1; i<10; i++){
    for(int j=1; j<10; j++){
        cout << i*j << ' ';
    }
    cout << '\n';
}

九九乘法表

1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10
3 3 6 9 12
4 4 8 12 16
5 5 10
6 6
7 7
8 8
9 9

i

j

巢狀迴圈

for(int i=1; i<10; i++){
    for(int j=1; j<10; j++){
        cout << i*j << ' ';
    }
    cout << '\n';
}

九九乘法表

1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10
3 3 6 9 12
4 4 8 12 16
5 5 10
6 6
7 7
8 8
9 9

i

j

巢狀迴圈

for(int i=1; i<10; i++){
    for(int j=1; j<10; j++){
        cout << i*j << ' ';
    }
    cout << '\n';
}

九九乘法表

1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10
3 3 6 9 12
4 4 8 12 16
5 5 10
6 6
7 7
8 8
9 9

i

j

.

.

.

.

.

陣列

     int score[5] = {2, 4, 6, 8, 10}

陣列名稱

陣列型態

空間數

內容物(非必要)

# 一種資料型態

# 可存放多個變數

# 第一格從0開始

# 大小固定

取值 : 陣列名稱[位置]

int a[3] = {2, 4, 6};
cout << a[0] << endl; //會輸出2

賦值

int a[3];
a[3] = {1, 2, 3}
    
for(int i=0; i<3; i++){
	cin >> a[i];
}  

二維陣列

int arr[2][3]={{0,1,2}, {3,4,5}}

int a[2][3];
for(int i=0; i<2; i++)
    for(int j=0; j<3; j++)
        cin >> a[i][j];
}
0 1 2
0 0 1 2
1 3 4 5

函式&遞迴

宣 告

返回型態 函式名稱(參數列表) {
     // 函式的程式碼
     
     return 返回值;
}

#include<iostream>
using namespace std;

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

int main(){
	int x=10, y=5;
    cout << add(x, y) << endl;
}

# 不回傳值 : void

c++函式庫

#include <iostream>

                 <string>

                 <ctime>

                 <algorithm>

                 <cmath>

                 <vector>

 

萬用函式庫 -> #include <bits/stdc++.h>

遞迴

# 把大問題拆成小問題

# 在函式裡呼叫自己

int factorial(int n){
    if(n==1) return 1; //1!為1
    else return n*factorial(n-1); //n!為n*(n-1)!
}

字串

# string 變數名稱 = "[文字]"

# 函式庫#include<string>

# 使用雙引號(")

#include <iostream>
using namespace std;
int main(){
    string str1;
    str1="IZCC";
    cout << str1 << endl;
    cout << str1[0] << endl;
    return 0;
}

輸出IZCC

        I

# 字串可相加

# 字串位置指的是字元,因此要使用單引號

# 變數名稱.empty() -> 字串是否有賦值

# 變數名稱.size()/.length() -> 字串長度

#include <iostream>
using namespace std;
int main()
{
    string str1, str2;
    str1 = "IZCC";
    str2 = " = INFOR x ZSISC x CKCSC x CMIOC";
    
    cout << str1+str2 << endl; // IZCC = INFOR x ZSISC x CKCSC x CMIOC
    str1[3] = '2';
    cout << str1 << endl; //IZC2
    cout << str1.empty() << endl; //0
    cout << str1.size() <<endl; //4
    return 0;
}

KAHOOT!

Palette

By phoebe tsai

Palette

  • 246