C++語法

Hello world!

#include <iostream>
using namespace std;
int main() {
	cout << "Hello world!" << endl;
}

初始程式碼

#include <iostream>

代表引入一個函式庫,裡面是別人寫好的各種函式

iostream是輸出輸入的函式庫

using namespace std;

代表引入一個命名空間

能區分不同函式庫的函式

例如原本的cout應該要寫成std::cout

使用using namespace std;後就只要cout就好

#include <iostream>
int main() {
	std::cout << "Hello world!" << std::endl;
}

int main(){     }

代表主程式

大括號內的程式碼就是會執行的部分

cout << "Hello world!" << endl;

cout代表輸出的指令

endl代表換行

大部分程式碼結尾都要記得加;

後面會仔細講到輸入and輸出

萬用函式庫

能直接引入所有你會需要的函式

#include <bits/stdc++.h>
using namespace std;
int main() {
	cout << "Hello World!";
}

變數

變數像是一個存放資料的東西,可以進行運算或處理

每個變數都需要先宣告才能使用

宣告變數

先打變數型態,空格後打變數名稱

例如: int a;

代表宣告一個型態是整數,名稱是a的變數

也能在宣告時給變數初始值

例如: int a = 10;

#include <iostream>
using namespace std;
int main() {
	int a = 10;
	cout << a;
}

宣告變數規則

  1. 不能以數字、符號(除了底線_)開頭
  2. 不能將變數命名為關鍵字(int、namespace...)

資料型態

整數

  1. int(-2^31 ~ 2^31-1)
  2. unsigned int(0 ~ 2^32-1)
  3. long long(-2^63 ~ 2^63-1)
  4. unsigned long long(0 ~ 2^64-1)
#include <iostream>
using namespace std;
int main() {
	int a = 10;
	long long b = 10;
}

浮點數(小數)

  1. float(小數點後7位)
  2. double(小數點後15位)
#include <iostream>
using namespace std;
int main() {
	float a = 0.1;
	double b = 0.1;
}

字元

  1. char(字元,以ASCII碼儲存,用' '表示)
  2. string(字串(字元陣列),用" "表示)
#include <iostream>
using namespace std;
int main() {
	char a = 'a';
	string b = "abcdef";
}

ASCII碼

布林值

  1. bool(true跟false)
#include <iostream>
using namespace std;
int main() {
	bool a = true;
}

輸入and輸出

cin

賦予變數值,用法如下

cin >> 變數 >> 變數;

有幾個變數就打幾次

#include <iostream>
using namespace std;
int main() {
	int a;
	float b;
	char c;
	cin >> a >> b >> c;
}

cout

在螢幕顯示物件,可以是變數或數值

可以在最後加endl或'\n'換行,用法如下

#include <iostream>
using namespace std;
int main() {
	int a = 10;
	cout << "a = " << a << endl;
	cout << 100 << '+' << 0.5 << '\n';
}

輸入輸出優化

增加cin、cout的效率

注意不能跟printf、scanf混用

#include <iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

運算子

可以對變數或數值進行運算

指定運算子

直接將等號右邊的數值或運算結果賦予給變數

和數學的等於不同

#include <iostream>
using namespace std;
int main() {
	int a;
	a = 10 + 5; //a是15 
}

算術運算子

加(+)

加就對了

#include <iostream>
using namespace std;
int main() {
	int a = 5 + 3;
}

算術運算子

減(-)

減就對了

#include <iostream>
using namespace std;
int main() {
	int a = 8 - 2;
}

算術運算子

乘(*)

乘就對了

#include <iostream>
using namespace std;
int main() {
	int a = 4 * 7;
}

算術運算子

除(/)

當兩邊都是整數,小數點後會被無條件捨去

當其中一邊為浮點數,結果才會是浮點數

#include <iostream>
using namespace std;
int main() {
	int a = 5 / 2; //2
	float b = 5 / 2.0; //2.5
}

算術運算子

簡易改值(?

在指定運算子前方加上算術運算子

省略一次變數的名字

#include <iostream>
using namespace std;
int main() {
	int a = 5, b = 5;
	a = a + 2;
	cout << a << '\n';
	b += 2;
	cout << b << '\n';
}

算術運算子

遞增(++)

將變數放在運算子前後會有不同效果

放前面的值為運算前的值

後後面為運算後的值

#include <iostream>
using namespace std;
int main() {
	int a = 1;
	cout << a++ << '\n'; //輸出1,a變為2
	int b = 1;
	cout << ++b << '\n'; //輸出2,b變為2 
	int c = 1;
	c++; //c變為2 
}

可單獨使用

算術運算子

遞減(--)

和遞增規則相同

#include <iostream>
using namespace std;
int main() {
	int a = 1;
	cout << a-- << '\n'; //輸出1,a變為0
	int b = 1;
	cout << --b << '\n'; //輸出0,b變為0
	int c = 1;
	c--; //c變為0
}

算術運算子

括號( () )

c++的計算皆符合四則運算

不分大,中,小括號

一律都使用小括號

#include <iostream>
using namespace std;
int main() {
	int a = 20 / (2 * (4 + 1));
}

算術運算子

模、取餘數(%)

取就對了

#include <iostream>
using namespace std;
int main() {
	int a = 8 % 3; //a為2
}

比較運算子

可以對數值或變數進行比較運算,會回傳布林值

  1. >(大於)
  2. <(小於)
  3. ==(等於)
  4. !=(不等於)
  5. >=(大於等於)
  6. <=(小於等於)

注意==和=不要搞混

一個是相等,一個是指定運算子

邏輯運算子

可以對布林值進行邏輯運算,會回傳布林值

  1. &&(and)
  2. ||(or)
  3. !(not)

&&(and)

當兩邊的布林值都是true(真)

結果才會是true

換句話說

其它結果都是false(假)

*小提醒

非0即為真

||(or)

當兩邊的布林值其中一個是true

結果便會是true

換句話說

當兩邊都為false

結果才會是false

!(not)

只會對一個值作用

將布林值相反

true -> false

false -> true

邏輯運算

例題

zerojudge a002

zerojudge d287

zerojudge d068

if else if-else

用條件讓電腦判斷要執行的程式碼

if

會有一個判斷條件,條件達成會執行大括號內的程式

if的格式如下

#include <iostream>
using namespace std;
int main() {
//	if(判斷條件){
//		條件達成時會執行的程式 
//	}
	int a = 5;
	if(a > 1){
		cout << "a大於1";
	}
}

else

如果if的條件都沒達成,就會執行大括號內的程式

else的格式如下

#include <iostream>
using namespace std;
int main() {
//	if(判斷條件){
//		條件達成時會執行的程式 
//	}
//	else{
//		條件都不達成會執行的程式 
//	}
	int a = 5;
	if(a < 1){
		cout << "a小於1";
	}
	else{
		cout << "a不小於1";
	}
}

else if

如果有多個條件要判斷時使用

else if的格式如下

#include <iostream>
using namespace std;
int main() {
//	if(判斷條件){
//		條件達成時會執行的程式 
//	}
//	else if(判斷條件){
//		條件達成時會執行的程式
//	}
//	else{
//		條件都不達成會執行的程式 
//	}
	int a = 5;
	if(a < 1){
		cout << "a小於1";
	}
	else if(a > 10){
		cout << "a大於10";
	}
	else{
		cout << "a不小於1也不大於10";
	}
}

小提醒

放較前面的if或else if如果達成了
後面的其他條件會被直接無視

例題:成績等第

在考完數學小考後,你想知道你的成績位於哪個等第

90分以上為A等第

80分以上為B等第

70分以上為C等第

其餘為D等第

#include <iostream>
using namespace std;
int main() {
	int score;
	cin >> score;
	if(score >= 70)
		cout << "C";
	else if(score >= 80)
		cout << "B";
	else if(score >= 90)
		cout << "A";
	else
		cout << "D";
}

由於70分以上的判斷放在最上面

因此無論是100、90分都會被歸類到C等第

解法1

將判斷順序調換

#include <iostream>
using namespace std;
int main() {
	int score;
	cin >> score;
	if(score >= 90)
		cout << "A";
	else if(score >= 80)
		cout << "B";
	else if(score >= 70)
		cout << "C";
	else
		cout << "D";
}

解法2

條件改嚴格一點

#include <iostream>
using namespace std;
int main() {
	int score;
	cin >> score;
	if(score >= 70 && score < 80)
		cout << "C";
	else if(score >= 80 && score < 90)
		cout << "B";
	else if(score >= 90 && score <= 100)
		cout << "A";
	else
		cout << "D";
}

例題

zerojudge a053

zerojudge d065

zerojudge a410

迴圈

讓電腦重複執行

for迴圈

通常在已知要重複幾次的狀況下使用

格式如下

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
int main() {
//	for(一開始要做的事; 執行條件; 跑完一圈後要執行的動作){
//		迴圈裡的程式;
//	} 
	
}

重複跑指定次數的寫法

一開始將i設定為0

執行條件為i < 次數

一圈結束後將i+1

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
int main() {
//	for(int i = 0; i < 執行次數; i++){
//		迴圈裡的程式;
//	}
//	以下為範例
	for(int i = 0; i < 3; i++){
		cout << i << '\n';
	} 
}

while迴圈

通常在未知要重複幾次的狀況下使用

格式如下

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
int main() {
//	while(執行條件){
//		迴圈裡的程式;
//	}
	int a = 0;
	while(a < 5){
		cout << a << '\n';
		a++;
	}
}

do while迴圈

和while迴圈很類似

但會無視條件先跑第一圈

也就是說

即使條件未達成,也會執行1次

格式如下

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
int main() {
//	do{
//		迴圈裡的程式;
//	}while(執行條件);
	int a = 0;
	do{
		cout << a << '\n';
		a++;
	}while(a > 10);
}

break

直接結束整個迴圈

#include <iostream>
using namespace std;
int main() {
	for(int i = 0; i <= 10; i++){
		if(i == 5)
			break;
		cout << i << " ";
	}
}

continue

直接進入下一圈

#include <iostream>
using namespace std;
int main() {
	for(int i = 0; i <= 10; i++){
		if(i == 5)
			continue;
		cout << i << " ";
	}
}

巢狀迴圈

很多層迴圈

記得for迴圈的變數名稱不要重複

#include <iostream>
using namespace std;
int main() { 
	for(int i = 1; i <= 9; i++){
		for(int j = 1; j <= 9; j++){
			cout << i << " * " << j << " = " << i * j << '\n';
		}
		cout << '\n';
	}
}

因為我沒梗了所以放九九乘法表

多筆輸入(輸入直到EOF)

#include <iostream>
using namespace std;
int main() {
	int a;
	while(cin >> a){
		cout << a * a << '\n';
	}
}

無限輸入

例題

zerojudge a005

zerojudge c013

zerojudge c295

解題系統

刷題的網站

有很多題目可以練習

只要辦一個帳號(大部分可以直接用google登入)

然後複製、上傳程式碼

就能知道你的程式是不是正確的

以下是幾個網站

zerojudge:有超多題目,缺點是難度和品質差異很大,但還是能用

TCIRC judge:台中一中的judge,包含了ap325的題目

codeforces:競賽用,每隔幾天會有線上比賽(但最近梗題越來越多)

atcoder:競賽用,特色是很重思考,而且題序都蠻清楚易懂

點這選題目

點這登入(截圖長這樣是因為我已經登入了)

使用方法(以zerojudge為例)

點這裡選擇想要的題目

點這送出你的程式碼

點這通常會有別人的提示

記得一定要選對...(cpp就是c++)

將程式碼全部複製到這裡

送出解答

最後按這個

submission

看你有沒有答對

看這裡的英文(下一頁會解釋)

名詞解釋

AC:恭喜你答對了

NA:部分測資正確(代表可能有一些bug或是假解)

WA:錯誤,系統會顯示正確答案,除非題目設定不公開

TLE:超時,通常是沒用正確的演算法(語法題應該不會遇到)

CE:編譯錯誤(建議先在電腦執行過1次,確認能執行再上傳)

其他錯誤通常較少見

所以我就沒列出了

C++語法

By patrickh

C++語法

  • 609