台大資管 莊貴淳
基本C++介紹
環境與IDE安裝
輸入輸出語法介紹
基本邏輯與迴圈介紹
邏輯與迴圈題目練習
進階演算法介紹
更多題目練習
演算法與時間複雜度
介紹與題目練習
更多題目
APCS
# Prerequisite knowledge
# Prerequisite knowledge
數據分析
操作資料庫
應用軟體開發
網頁互動
# Prerequisite knowledge
數據分析
操作資料庫
應用軟體開發
網頁互動
R語言
SQL
C語言
JavaScript
# Prerequisite knowledge
# Prerequisite knowledge
# Prerequisite knowledge
# Prerequisite knowledge
Assembly
# Prerequisite knowledge
Assembly
# Prerequisite knowledge
# Prerequisite knowledge
# Prerequisite knowledge
compile
interpret
# Prerequisite knowledge
執行效率快
較不易理解
執行效率差
適合初學者
# Prerequisite knowledge
#include <iostream>
using namespace std;
int main() {
cout << "Hello! World!\n";
return 0;
}print("Hello World!")以 Windows 為例
寫程式的地方
文字編輯器
Word
整合不同的開發工具
整合開發環境(IDE)
Visual
Studio
Code
V
S
Code
在桌面新增資料夾
打開 VS code
下載協助開發的工具
Hello World
#include<iostream>
using namespace std;
int main() {
cout << "hello world";
return 0;
}輸出「first C++」
練習
Variable
10
10
-5
0
a
b
c
integer
int
10, -5, 0
數字
類型
英文名字
C++語法
可以放什麼
int a = 10;
int b = -5;
int c = 0;賦值運算子
賦值運算子
int a = 10;
int b = -5;
int c = 0;int a = 10, b = -5, c = 0;int a = 10, b = -5, c = 0;10
a
8
a = 8;10
a
8
a = 8;int 去哪了?
int a;int a = 10;10
int a;int a = 10;10
int a = 10;初始化
int a = 10;初始化
賦值運算子與初始化
賦值運算子與初始化
難道我們只能放整數嗎
2.1
int d = 2.1;cout << d;
integer
int
10, -5, 0
數字
類型
英文名字
C++語法
可以放什麼
int a = 10;int
a
10
數字
小數點
是非
字元
integer
float
boolean
character
類型
英文名字
C++語法
10, -5, 0
1.3, -2.1
true, false
'A', '7'
可以放什麼
int
float
bool
char
字串
string
'hello'
string
初始化幾個整數、浮點數、布林值、字元、字串
並輸出到終端機
int
a
10
練習
Input
什麼是輸入
2
int a = 0;int
a
0
cin >> a;2
int a = 0;int
a
cin >> a;練習
a001
Operation
a + b;a - b;a * b;a / b;a % b;10 / 3
3 ... 1
c = a % b;a = a % b;10
-5
a
b
a = a + b;10
-5
a
b
a = a + b;5
10
-5
a
b
a = a + b;5
10
-5
a
b
a = a + b;5
10
-5
a
b
a = a + b;5
-5
10
-5
a
a = a + 1;5
1
a
a = a + 1;5
1
a
a = a + 1;5
1
6
a
a = a + 1;5
1
6
a = a + 1;a++;a = a + 1;a++;a+=1;練習
a002
If / Else
if(){
}
else if(){
}
else {
}07:55以前
07:55-08:00
08:00之後
走到後門
跑到後門
走去前門
if(07:55以前){
}
else if(){
}
else {
}07:55以前
07:55-08:00
08:00之後
走到後門
跑到後門
走去前門
if(07:55以前){
走到後門;
}
else if(){
}
else {
}07:55以前
07:55-08:00
08:00之後
走到後門
跑到後門
走去前門
if(07:55以前){
走到後門;
}
else if(07:55-08:00){
}
else {
}07:55以前
07:55-08:00
08:00之後
走到後門
跑到後門
走去前門
if(07:55以前){
走到後門;
}
else if(07:55-08:00){
跑到後門;
}
else {
}07:55以前
07:55-08:00
08:00之後
走到後門
跑到後門
走去前門
if(07:55以前){
走到後門;
}
else if(07:55-08:00){
跑到後門;
}
else {
走到前門;
}07:55以前
07:55-08:00
08:00之後
走到後門
跑到後門
走去前門
練習
a003
練習
a053
While
For
For
(
初始值;
條件;
更新
)
int i=0;
For
(
初始值;
條件;
更新
)
int i=0;
i<n;
For
(
初始值;
條件;
更新
)
int i=0;
i<n;
i++
i--
i+=2
For
(
初始值;
條件;
更新
)
int i=0;
i<n;
i++
i--
i+=2
For
(
初始值;
條件;
更新
)
int i=0;
i<n;
i++
i--
i+=2
For
(
)
int i=0;
i<n;
i--
i+=2
For
(
)
int i=0;
i<n;
i+=2
{
}
程式執行內容
練習
a058
練習
a010
24
2^3 * 3
2
3
5
7
11
24
2^3 * 3
2
3
5
7
11
24
2^3 * 3
2
3
5
7
11
是不是質因數?
是
12
1
指數
2^3 * 3
2
3
5
7
11
12
還能不能整除?
能
6
2
1
指數
2^3 * 3
2
3
5
7
11
還能不能整除?
能
6
3
3
2
指數
2^3 * 3
2
3
5
7
11
還能不能整除?
不能
3
3
指數
int n;
cin >> n;
bool first = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (!first) cout << " * ";
first = false;
cout << i;
if (cnt > 1) cout << "^" << cnt;
}
}
if (n > 1) {
if (!first) cout << " * ";
cout << n;
}
cout << endl;int n;
cin >> n;
bool first = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (!first) cout << " * ";
first = false;
cout << i;
if (cnt > 1) cout << "^" << cnt;
}
}
if (n > 1) {
if (!first) cout << " * ";
cout << n;
}
cout << endl;是否在項目前加上 *
int n;
cin >> n;
bool first = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (!first) cout << " * ";
first = false;
cout << i;
if (cnt > 1) cout << "^" << cnt;
}
}
if (n > 1) {
if (!first) cout << " * ";
cout << n;
}
cout << endl;從 2 開始試除每個質因數
int n;
cin >> n;
bool first = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (!first) cout << " * ";
first = false;
cout << i;
if (cnt > 1) cout << "^" << cnt;
}
}
if (n > 1) {
if (!first) cout << " * ";
cout << n;
}
cout << endl;如果是質因數的話
int n;
cin >> n;
bool first = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (!first) cout << " * ";
first = false;
cout << i;
if (cnt > 1) cout << "^" << cnt;
}
}
if (n > 1) {
if (!first) cout << " * ";
cout << n;
}
cout << endl;宣告一個變數紀錄指數
int n;
cin >> n;
bool first = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (!first) cout << " * ";
first = false;
cout << i;
if (cnt > 1) cout << "^" << cnt;
}
}
if (n > 1) {
if (!first) cout << " * ";
cout << n;
}
cout << endl;