#include<iostream>
using namespace std;
int main(){
	
    return 0;
}引入函式庫
使用std命名空間
程式進入的地方
#include<iostream>
using namespace std;
int main(){
	
    cout << "hello, world";
    
    return 0;
}Hello, world!
分號是一行程式的結尾
cout << "hello" << endl << "world" << endl;可以把多個輸出用<<串起來
endl是換行
cout << (2 + 4) * 10 << endl;也可以直接輸出數字運算結果
變數
一個存放資料的空間
int
整數變數
宣告變數
初始化
int x;
int y, z;int x = 27, y = 10;int
整數變數
宣告變數
int x;
int y, z;初始化
int x = 27, y = 10;指定運算子
x = 1;
x = x + 1int
整數變數
整數變數可以把它當成數字去做計算
也可以用cout輸出
int x = 5;
int y = 10;
int z = x + y;
cout << x + y - 10 << '\n';輸入
int x;
cin >> x;int x, y;
cin >> x >> y;將輸入存入變數中
也可以連續輸入
int
可儲存整數
範圍:\(-2^{31}\) ~ \(2^{31}-1\)
long long int
可儲存更大的整數
範圍:\(-2^{63}\) ~ \(2^{63}-1\)
float
可儲存有小數點的數字
精度:6~7位數
double
可儲存更多位數
精度:14~15位數
float
可儲存有小數點的數字
精度:6~7位數
double
可儲存更多位數
精度:14~15位數
char
可儲存字元
一個字元的表示方式:'a'
string
可儲存字串
字串表示方式:"abc"
bool
只儲存真假值
true or false
int x = 5, y = 7;
x = y;
y = x;int x = 5, y = 7;
int tmp = x;
x = y;
y = tmp;int x = 5, y = 7;
y = x + y;
x = y - x;
y = y - x;+-*/
int x;
x = 10 + (2 + 5) * 2;
x = x + 1;cout << 13 % 4 << endl;x % 2
++ 遞增
x = x + 1
int x = 1;
x++;
cout << x << endl;
cout << x++ << endl;
cout << ++x << endl;
-- 遞減
x = x - 1
cout << ((5 + 2) * 2 + 3) / 3 << endl;cout << (13 < 4) << endl;cout << (5 < x && x < 13) << endl;cout << 5 / 2 << endl;2
cout << 5.0 / 2 << endl;2.5
int x = 5.0 / 2;
cout << x << endl;int x = 5;
cout << (double)5 / 2 << endl;
cout << (double)x / 2 << endl;int x = 5; //我是註解
/*
我是註解
*/
if, else if, else
if(判斷條件){
	要執行的程式碼;
}int x;
cin >> x;
if(x >= 90){
    cout << x << ">= 90" << endl;
}int x;
cin >> x;
if(x >= 90 && x <= 95){
    cout << "90 <= " << x << " <= 95" << endl;
}int x;
cin >> x;
if(x >= 90){
    cout << x << " >= 90" << endl;
}else{
	cout << x << " < 90" << endl;
}不符合條件要做的事
int x = 95;
if(x >= 90){
	cout << "A+" << endl;
}
if(x >= 80){
	cout << "A" << endl;
}int x = 95;
if(x >= 90){
	cout << "A+" << endl;
}else if(x >= 80){
	cout << "A" << endl;
}int x = 95;
if(x >= 90){
	cout << "A+" << endl;
}else if(x >= 80){
	cout << "A" << endl;
}else if{x >= 70}{
	cout << "B" << endl;
}else{
	cout << "C" << endl;
}把重複的工作交給電腦
for(int i = 0; i < 執行次數; i++){
	cout << i << ' ';
}
cout << endl;for(int i = 0; i < 5; i++){
	cout << i << ' ';
}
cout << endl;0 1 2 3 4
int n;
cin >> n;
int x;
for(int i = 0; i < n; i++){
	cin >> x;
    cout << x * 2 << '\n';
}for(迴圈一開始要做的事; 每一次迴圈執行條件; 每次迴圈結束時要做的事){
	cout << i << ' ';
}
cout << endl;int n;
cin >> n;
for(int i = 1; i <= n; i = i + 2){
	cout << i << ' ';
}
cout << endl;int n;
cin >> n;
for(int i = 1; i <= n; i = i + 2){
	cout << i << ' ';
}
cout << endl;輸出1~n之間的奇數
int n;
cin >> n;
for(int i = 1; i * i <= n; i++){
	cout << i << ' ';
}
cout << endl;int n;
cin >> n;
for(int i = 1; i * i <= n; i++){
	cout << i << ' ';
}
cout << endl;輸出1~\(\sqrt{n}\)
if(5 > 0){
	int x = 5;
}
cout << x << '\n';Compile Error
for(int i = 0; i < n; i++){
	int x;
    cin >> x;
}
cout << i << '\n';Compile Error
輸入 \(n, m\) 兩個正整數
用*輸出一個 \(n\times m\) 的正方形
範例輸入
3 5
範例輸出
*****
*****
*****
輸入一個n,接著輸入n個整數,輸出這n個數的總和
輸入一個n,接著輸入n個整數,輸出這n個數的總和
int n, x;
cin >> n;
int sum = 0;
for(int i = 0; i < n; i++){
	cin >> x;
    sum += x;
}輸入一個n代表學生數量
接著輸入n個整數,代表每個人的成績
輸出這n個數中有幾個人不及格(小於60)
int n, x;
cin >> n;
int cnt = 0;
for(int i = 0; i < n; i++){
	cin >> x;
    if(x < 60){
    	cnt++;
    }
}
cout << cnt << '\n';輸入一個n代表學生數量
接著輸入n個整數,代表每個人的成績
輸出這n個數中有幾個人不及格(小於60)
d074
用一個變數儲存目前的最大值
d074
int x;
cin >> x;
int i = 0;
while(i < x){
	cout << i << ' ';
    i++;
}
int x;
cin >> x;
while(x > 1){
	if(x % 2 == 1){
    	x = 3 * x + 1;
    }else{
    	x = x / 2;
    }
}
cout << x << '\n';
break; 直接結束目前這層迴圈
continue; 跳回迴圈開頭繼續執行
int n;
while(true){
	cin >> n;
    if(n == 0){
    	cout << "stop the loop\n";
        break;
    }
    cout << n << '\n';
}for(int i = x; i < y; i++){
	if(i % 2)
    	continue;
    cout << i << '\n';
}int year;
while(cin >> year){
	//你的程式
}宣告陣列
int a[5];宣告陣列
int a[5];陣列初始化
int a[5] = {1, 3, 5, 2, 4};宣告陣列
int a[5];用索引值取變數
int a[5];
cin >> a[3];int n;
int a[50];
cin >> n;
for(int i = 0; i < n; i++){
	cin >> a[i];
}
int n;
int a[50];
cin >> n;
for(int i = 0; i < n; i++){
	cin >> a[i];
}
把一堆數字照順序排好
宣告二維陣列
int a[2][4];使用二維陣列
int a[2][4];
a[1][3] = 5;int a[2][4]={
{1, 4, 2, 3},
{2, 3, 4, 1}
};以反斜線開始
以反斜線開始
換行 \n
tab \t
\ \\
" \"
' \'
#include<iostream>
using namespace std;
int main(){
	string s;
    cin >> s;
   	int n = s.size();
    for(int i = 0; i < n; i++){
    	if(s[i] >= 'A' && s[i] <= 'Z'){
			s[i] = s[i] - 'A' + 'a';
        }
	}
    cout << s << '\n';
}(判斷式1) && (判斷式2)
當判斷式1的結果是false
那就不會執行判斷式2
因為 && 的結果一定是 false
(判斷式1) || (判斷式2)
當判斷式1的結果是 true
那就不會執行判斷式2
因為 || 的結果一定是 true
int a[10] = {1, 5, 2, 3, 6, 7, 1, 2, 3, 9};
cin >> x;
if(x >= 0 && a[x] < 5){
	cout << a[x] << endl;
}else{
	cout << 0 << endl;
}回傳型別 函數名稱(參數型別 參數名稱){
	//程式
}int f(int x){
	return 2 * x;
}int f(int x){
	int a = 1;
    for(int i = 1; i <= x; i++){
    	a = a * i;
    }
    return a;
}int add(int x, int y){
	return x + y;
}void hello_world(){
	cout << "Hello, world!" << endl;
}試著把判斷質數的地方寫成函式
變數型別 *指標變數名稱;
int *ptr;&變數;
int a = 10;
int *ptr = &a;*(指標);
int a = 10;
int *ptr = &a;
cout << ptr << endl;
cout << *ptr << endl;不是複製品
*(指標);
int a = 10;
int *ptr = &a;
*ptr = *ptr + 5;
cout << *ptr << endl;void add(int *x){
	*x = *x + 1;
}
int main(){
	int x;
    cin >> x;
    int *ptr = &x;
    add(ptr);
    cout << x << '\n';
}提示:傳指標進去
自己定義變數型別
struct 型別名稱{
     //想要包在裡面的變數
int x;
string s;
};
struct Student{
	string name;
    int age, score;
};struct Student{
	string name;
    int age, score;
};
int main(){
	Student a;
    a.name = "Scott";
    a.age = 20;
    a.score = 30;
}d550