review
10^11
目錄
C++概論
條件判斷
迴圈
陣列
函式&遞迴
排序
字串
指標
社網
C++概論
C++概論
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
}
//萬用函式庫 -> #include <bits/stdc++.h>//單行註解
/*
多行
註解
*/起手式(程式學這麼久,該記得ㄌ)
註解
C++概論
| int | 整數 | 2,10 |
| long long | 長整數 | 10000000000、-10000000000 |
| float | 浮點數 | 0.87 |
| double | 倍精度浮點數 | 3.1415926535 |
| char | 字元 | 'a'、'@'、'4' |
| string | 字串 | "ZSISC","吱吱不知吱" |
| bool | 布林 | True(1)、False(0) |
C++概論
還記得如何宣告嗎?
變數型態 變數名稱 = 值
string a="zsisc._.31st";
char b='@';
int c=31;C++概論
變數命名規則:
1.開頭不能為數字。
2.只能使用大小寫英文字母(A-Z,a-z)、數字(0-9)與底線(_)所組成。
3.不能使用保留字(如if、for、while)。
4.變數大小寫有差別,例如:A與a是不同的變數。
C++概論
變數命名規則:
1.開頭不能為數字。
2.只能使用大小寫英文字母(A-Z,a-z)、數字(0-9)與底線(_)所組成。
3.不能使用保留字(如if、for、while)。
4.變數大小寫有差別,例如:A與a是不同的變數。
C++概論
如何輸入輸出?
cin >> 變數;
cin >> 變數 >> 變數;int x,y;
cin>>x>>y;cout << 變數或值;
cout << 變數或值 << 變數或值;cout<<"Hello world!"<<"\n"<<100;
cout<<100<<x<<endl;C++概論
如何輸入輸出?
cin >> 變數;
cin >> 變數 >> 變數;int x,y;
cin>>x>>y;cout << 變數或值;
cout << 變數或值 << 變數或值;cout<<"Hello world!"<<"\n"<<100;
cout<<100<<x<<endl;算術運算子:
負責數值的運算,跟數學四則運算一樣,先乘除後加減
| 符號 | 意義 |
|---|---|
| + | 加 |
| - | 減 |
| * | 乘 |
| / | 除 |
| % | 取餘數 |
| ++ | 遞增1 |
| -- | 遞減1 |
C++概論
邏輯運算子:在邏輯運算中只會有true或false兩種結果
| 符號 | 意義 | 達成true條件 |
|---|---|---|
| && | 且AND | 兩個都是 1 |
| || | 或OR | 至少一個 1 |
| ^ | 異或XOR | 兩個相異 |
| ! | 否NOT | 與原本相反 |
C++概論
比較運算子:在比較運算中只會有true或false兩種結果
| 符號 | 意義 |
|---|---|
| >= | 大於等於 |
| <= | 小於等於 |
| > | 大於 |
| < | 小於 |
| == | 等於 |
| != | 不等於 |
C++概論
算術運算子的簡寫:
int a=5,b=3;
b+=a;
cout<<b;
//8a=a+1 a+=1
a=a-1 a-=1
C++概論
條件判斷
條件判斷
if(A條件){
//達成A條件時執行;
}
else if(B條件){
//未達成A條件、達成B條件時執行;
}
else{
//未達成B條件和A條件時執行;
}模板好用
但記得還是要靠理解啦
情況a? 條件b : 條件c當 a 為true則執行 b,
當 a 為false則執行 c
條件判斷
三元運算子
#include <iostream>
using namespace std;
int main()
{
int a=5;
int b=10;
int c=15;
int ans=(a>0)?(b>10?b:c):20;
cout<<ans;
}這東西會輸出什麼?
15
條件判斷
迴圈
迴圈
while迴圈
while(條件){
達成條件時執行;
}for迴圈
for(起始值; 條件式; 更新值){
達成條件時執行;
}迴圈
int chieh = 87;
while(chieh>=78){
cout << "對她是!!!" << endl;
}while迴圈
for迴圈
for(int i=0;i<5;i++){
cout<<"i like chyun"<<endl;
}迴圈
巢狀迴圈(九九乘法)
for(int i=1; i<=9; i++){
for(int j=1; j<=9; j++){
cout << i*j << ' ';
}
cout << '\n';
}陣列
一維陣列
資料型態 名稱[大小];
資料型態 名稱[大小]={內容物};宣告方式
int zsisc[7]={0,1,2,3,4,5,6}一維陣列
int a[3]={2,4,6};
cout<<a[0]<<endl; //2
int b[3]={1,2,3};
for(int i=0;i<3;i++){
cin>>b[i];
}
for(int i=0;i<3;i++){
cout<<b[i]<<endl;
} 賦值
二維陣列
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
函式&遞迴
int f(int n){
if(n==1) return 1; //1!為1
else return n*f(n-1); //n!為n*(n-1)!
}遞迴怎麼寫?
排序
泡沫排序(Bubble Sort)
1
2
3
4
5
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
4
5
3
2
1
泡沫排序(Bubble Sort)
程式碼(函式&詳細交換版本)
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// 交換arr[j]和arr[j+1]
int x = arr[j];
arr[j] = arr[j+1];
arr[j+1] = x;
}
}
}
}
int main() {
int arr[] = {5,4,3,2,1};
int n = 5;
cout << "原始數列:";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
bubbleSort(arr, n);
cout << "排序後數列:";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}選擇排序(Selection Sort)
1
2
3
4
5
1
2
3
4
5
選擇排序(Selection Sort)
1
2
3
4
5
選擇排序(Selection Sort)
1
2
3
4
5
選擇排序(Selection Sort)
1
2
3
4
5
選擇排序(Selection Sort)
1
2
3
4
5
選擇排序(Selection Sort)
1
2
3
4
5
選擇排序(Selection Sort)
程式碼(swap版本)
#include <bits/stdc++.h>
using namespace std;
int main(){
int a[5]={4,5,2,1,3};
int n=5;
for(int i=0;i<n;i++){
int min_index=i;//紀錄初始位置
for(int j=i+1;j<n;j++){ //不用管已經找完的左邊
if(a[j]<a[min_index])
min_index=j;//尋找最小值
}
swap(a[i],a[min_index]);
}
for(int i=0; i<n; i++){
cout<<a[i]<<' ';
}
}選擇排序(Selection Sort)
1
2
3
4
5
插入排序(Insertion sort)
1
2
3
4
5
插入排序(Insertion sort)
1
2
3
4
5
插入排序(Insertion sort)
1
2
3
4
5
插入排序(Insertion sort)
1
2
3
4
5
插入排序(Insertion sort)
程式碼(while版本)
#include <bits/stdc++.h>
using namespace std;
int main(){
int a[5] = {3,2,5,1,4};
int n=5;
for(int i=1;i<n;i++){//第二個元素開始比較
int insert_num=a[i];
int j=i-1; //往左比大小(已排序資料)
while(j>=0&&a[j]>insert_num){
swap(a[j+1],a[j]);
j--;
}
}
for(int i=0;i<n;i++){
cout<<a[i]<<' ';
}
}插入排序(Insertion sort)
字串
字元
如何宣告字元?
char ch = 'A'; #include <iostream>
using namespace std;
int main()
{
char ch;
while(cin>>ch){
if (isupper(ch)) cout << "是大寫字母";
else if (islower(ch)) cout << "是小寫字母";
else if (isdigit(ch)) cout << "是數字";
else cout << "是其他符號";
}
return 0;
}如何判斷
字元類型?
字串
如何宣告字串?
string zsisc="31st"string str1, str2;
cin >> str1 >> str2;
//輸入 : Hello World
//輸入 :
Hello
World
//輸出 :
str1 = Hello
str2 = World輸入時遇到空白或換行即停止
字串
如何處理字串?
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
//變數名稱.empty() -> 字串是否有賦值
cout << str1.size() <<endl; //4
//變數名稱.size()/.length() -> 字串長度字串
getline:輸入整行字串
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Please, enter your full name: ";
getline (cin,name);
cout << "Hello, " << name << "!\n";
return 0;
}指標
指標
int a = 5;
int *p = &a;
cout << *p; //印出 a 的值
//&a 取址
//int *p 宣告指標
//cout << *p; 取值&->位址
*->值
指標
int arr[3] = {1, 2, 3};
cout << arr<<endl;
// 輸出 arr 的記憶點址
cout << *arr<<endl;
// 輸出 arr[0] 的值 -> 1陣列中的指標
指標
int arr[3] = {10, 20, 30};
int *p = arr; // 等同於 int *p = &arr[0];
cout << *p; // 10
cout << *(p+1); // 20
cout << *(p+2); // 30陣列中的指標
指標
#include <iostream>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5;
int y = 10;
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
函式中的指標
KAHOOT!
review
By 愛錢成癡,嗜賭成癮
review
- 59