吳若喬
AP325
下載連結(最新的是v1.3):
目錄
模板優化
1
基礎程式優化及提醒&簡單複習
2
時間複雜度
3
模板優化
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}模板優化
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}模板優化
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}萬用函式庫,基本各大judge系統都可使用,APCS也可以
模板優化
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout<<"Hello World";
return 0;
}提升輸入輸出的速度
程式優化
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}程式優化
#include <iostream>
using namespace std;
int main()
{
printf("Hello World!");
return 0;
}程式優化
#include <iostream>
using namespace std;
int main()
{
printf("Hello World!");
return 0;
}程式優化
#include <iostream>
using namespace std;
int main()
{
printf("Hello World!");
return 0;
}複習
- if-else判斷式
- while迴圈
- 函式和遞迴
- 字元、字串
- for迴圈設定範圍
if-else
#include <iostream>
using namespace std;
int main()
{
int score=70;
if(score==100){
cout << "A";
}else if(score>=80){
cout << "B";
}else if(score>=60){
cout << "C";
}else{
cout << "D";
}
return 0;
}while迴圈
#include <iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10){
cout << i << " ";
i++;
}
return 0;
}while迴圈
#include <iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10){
cout << i << " ";
i++;
}
return 0;
}while迴圈
#include <iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10){
cout << i << " ";
i++;
}
return 0;
}函式和遞迴
#include <iostream>
using namespace std;
int f(int n){
if(n==1) return 1;
else return n*f(n-1);
}
int main()
{
int a=5;
cout << f(a);
return 0;
}字元和字串
#include <iostream>
using namespace std;
int main()
{
char A='A',Z='Z';
cout << int(A) << " " << int(Z);
return 0;
}
字元和字串
#include <iostream>
using namespace std;
int main()
{
char A='A',Z='Z';
cout << int(A) << " " << int(Z);
return 0;
}
字元和字串
#include <iostream>
using namespace std;
int main()
{
string strr="hello";
char arr[10]={'w','o','r','l','d','\0'};
cout << strr << " " << arr;
return 0;
}for迴圈設定範圍
#include <iostream>
using namespace std;
int main()
{
string strr="hello";
int len=strr.length();
for(int i=0;i<len;i++){
cout << strr[i];
}
return 0;
}#include <iostream>
using namespace std;
int main()
{
string strr="hello";
int len=strr.length();
for(int i=0;i<len;i++){
cout << strr[i];
}
return 0;
}for迴圈設定範圍
提醒
- 溢位
- 精準度
- 找bug
溢位
int : 32bits
long long : 64bits
如果超過int可儲存的範圍,就會產生overflow(溢位)
溢位
int : 32bits
long long : 64bits
如果超過int可儲存的範圍,就會產生overflow(溢位)
#include <iostream>
typedef long long LL;
using namespace std;
int main()
{
LL a;
cin >> a ;
cout << a;
return 0;
}精準度
關於float和double
0.1+0.2=? 0.3=0.3?
#include <iostream>
using namespace std;
int main()
{
double a=0.3, b=0.1+0.2;
printf("%.20f %.20f\n", a, b);
return 0;
}找bug
- Compile Error
- 陣列溢位
- 邏輯錯誤
Compile Error&Warning
這個真的是最好抓的了......
- 分號
- 宣告型態
- 變數是否重複使用
- 括號是否兩兩配對
- 看錯誤資訊!看錯誤資訊!看錯誤資訊!看不懂去Google!看不懂去Google!看不懂去Google!
- Warning只是代表警告,不是程式出問題
陣列溢位
- 從0開始數!
- 宣告大一點
- 自訂義數值避免在主函式開太大超過限制
#include <iostream>
#define M 10000000
using namespace std;
int main()
{
int arr[M];
return 0;
}邏輯錯誤
- 把要計算的東西輸出看哪個環節出錯
#include <iostream>
using namespace std;
int f(int n){
if(n==1) return 1;
else{
cout << n << "\n";
return n*f(n-1);
}
}
int main()
{
int a=5;
cout << f(a);
return 0;
}時間複雜度

以O(T)表示,O念作Big-O,T代表時間複雜程度
沒有常數項,並以最高次代表
時間複雜度
for (int i=0; i<n; i++) {
total += a[i]*i;
}- 迴圈跑n次
- 每次執行兩個命令分別為+和*
- 每次回圈都要判斷i<n 和 i++
時間複雜度
for (int i=0; i<n; i++) {
total += a[i]*i;
}- 迴圈跑n次
- 每次執行兩個命令分別為+和*
- 每次回圈都要判斷i<n 和 i++
n(執行+的命令)+n(執行*的命令)+n(i<n)+n(i++)=4n
時間複雜度
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++)
if (a[j]<a[i])
inversion++;
}- 最外圈執行n次
- 內圈執行n-1次
- 第三行和第四行的指令重複計算,要除上2
時間複雜度
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++)
if (a[j]<a[i])
inversion++;
}- 外圈執行n次
- 內圈執行n-1次
- 第三行和第四行的指令重複計算,要除上2
n(外圈)* n-1(內圈) /2(除掉重複計算的程式碼)=(n^2-n)/2
時間複雜度
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++)
if (a[j]<a[i])
inversion++;
}- 外圈執行n次
- 內圈執行n-1次
- 第三行和第四行的指令重複計算,要除上2
n(外圈)* n-1(內圈) /2(除掉重複計算的程式碼)=(n^2-n)/2
採計最高次 該複雜度為O(n^2)
AP325
By Wu Phoebe
AP325
- 93