PJ高一資訊上機考講義
(by林修民&賴竑斌)
1. 基本程式碼
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << "字串巴拉巴拉" << endl;
cout << '\n';
cout << flush;
return 0;
}
2. 進階輸出
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(n) << ;
cout << fixed << setprecision(n) << ;
(char)()
'字元'
3. 變數與運算子
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
int m=3,n=4;
double d;
long long b;
int a[t];
int c[m][n]={{1,2,3,4},
{5,6,7,8},
{9,10,11,12}};
n++;
n+=t;
sqrt(n)
abs(n)
__gcd(m,n)
floor(d)
round(d)
ceil(d)
return 0;
}
4. 條件判斷
1. if
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if(條件1)
{
}
else if(條件2)
{
}
else
{
}
return 0;
}
2. switch條件判斷
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
switch(n)
case 1:
break;
case 2:
break;
case 5:
break;
default:
break;
return 0;
}
5. 迴圈
1. while
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
while(條件判斷)
{
}
return 0;
}
2. do while
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
do
{
}while(條件判斷);
return 0;
}
3. for
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
for(;1條件判斷;)
{
}
return 0;
}
等同於
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
while(條件判斷)
{
}
return 0;
}
6. 執行n次
1. for 常見寫法
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n ; i++)
{
}
for (int i = 1; i <= n ; i++)
{
}
return 0;
}
7. while 常見寫法
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
while(n--)
{
}
return 0;
}
8. 陣列 array
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int a[n];
memset(a,0,sizeof(a));
memset(a,-1,sizeof(a));
for(int i=0;i<n;i++)
{
cin>>a[i];
}
return 0;
}
9. 字串 string
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
char c;
string s;
cin >> s;
getline(cin,s);
s.size()
s.clear();
reverse(s.begin(),s.end());
toupper(c)
tolower(c)
isalpha(c)
return 0;
}
10. 函式
只補了這一塊的協作人:賴竑斌 (ps:修民是電神)
函式,是一個聽起來跟函數很像的東西。事實上,他們概念上是相同的。只不過,函數輸入之後會跑出對應值,但函式則不一定。
事實上,C++ 中內建了許多好用的函式(在標頭檔裡),我們舉絕對值 abs()為例。
cout << abs(2) << endl;
cout << abs(-3) << endl;
2
3
我們會用到的函式有兩種,一種是 void,另一種是可傳回值的函式。
先說說可以傳回值的函式好了:
bool name (int k)
{
if(k)
return 1;
else
return 0;
}
以 bool為例,這是個會傳回布林值(true or false)的函式。什麼意思呢?事實上,bool 的函式就像函數一樣,丟入一個值(或單純執行),就可以傳出布林值。
程式碼印出了絕對值,我們看起來很簡單。
但輸入值其實經過了這個過程:
#include <bits/stdc++.h>
using namespace std;
int abs(int t)
{
if(t>=0)
return t;
else
return -t;
}
int main(){
int k=-4;
cout << abs(k);
}
那如果只是作動作,沒有要回傳呢?我們就會用 void函式。
#include <bits/stdc++.h>
using namespace std;
void name (int a, bool b)
{
if(b)
cout << a;
}
int main()
{
int j;
bool k=0;
cin >> j;
name(j,k);
}
我們可以發現,他就真的只把 a 印出來,而不會傳回值。至於函式可以幹嘛呢?遞迴是一個常見的應用。
例如要算階乘的話,我們可以寫成:
int f(int k)
{
if(k>1)
return k*f(k-1);
else
return 1;
}
輸入 3之後,會先回傳 3×f(3−1)。而f(2)會回傳 2×f(1)。f(1)回傳 1。
最後就成了:f(3)=f(2)×3=f(1)×2×3=1×2×3=6。以上是關於函式的簡介。
11. 補充資料
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int m,n;
int a[n];
INT_MAX
INT_MIN
LONG_LONG_MAX
LONG_LONG_MIN
max(m,n)
min(m,n)
swap(m,n);
sort(a,a+n);
return 0;
}
12. 友善連結
受資研社副社長威脅放上的Koying資訊講義(內容較深入)
https://hackmd.io/@Koying/B1Ocazx7K/https%3A%2F%2Fhackmd.io%2F%40Koying%2FH1hI3Ovwt
PJ高一資訊上機考講義
(by林修民&賴竑斌)
1. 基本程式碼
2. 進階輸出
3. 變數與運算子
4. 條件判斷
1. if
2. switch條件判斷
5. 迴圈
1. while
2. do while
3. for
等同於
6. 執行n次
1. for 常見寫法
7. while 常見寫法
8. 陣列 array
9. 字串 string
10. 函式
只補了這一塊的協作人:賴竑斌 (ps:修民是電神)
函式,是一個聽起來跟函數很像的東西。事實上,他們概念上是相同的。只不過,函數輸入之後會跑出對應值,但函式則不一定。
事實上,C++ 中內建了許多好用的函式(在標頭檔裡),我們舉絕對值 abs()為例。
我們會用到的函式有兩種,一種是 void,另一種是可傳回值的函式。
先說說可以傳回值的函式好了:
以 bool為例,這是個會傳回布林值(true or false)的函式。什麼意思呢?事實上,bool 的函式就像函數一樣,丟入一個值(或單純執行),就可以傳出布林值。
程式碼印出了絕對值,我們看起來很簡單。
但輸入值其實經過了這個過程:
那如果只是作動作,沒有要回傳呢?我們就會用 void函式。
我們可以發現,他就真的只把 a 印出來,而不會傳回值。至於函式可以幹嘛呢?遞迴是一個常見的應用。
例如要算階乘的話,我們可以寫成:
輸入 3之後,會先回傳 3×f(3−1)。而f(2)會回傳 2×f(1)。f(1)回傳 1。
最後就成了:f(3)=f(2)×3=f(1)×2×3=1×2×3=6。以上是關於函式的簡介。
11. 補充資料
12. 友善連結
受資研社副社長威脅放上的Koying資訊講義(內容較深入)
https://hackmd.io/@Koying/B1Ocazx7K/https%3A%2F%2Fhackmd.io%2F%40Koying%2FH1hI3Ovwt