22527 鄭竣陽
Brine
BrineTW#7355
今天和以後的演算法小社課都會以 C++ 來教學。
Brine 今天會講很多東西,你們可能沒有辦法一次聽懂。
上課的時候有問題就提出來,會有人幫你解決。
回家之後可以自己再看幾次簡報複習。
下次演算法小社課會複習一些部份,不用擔心。
Editor
Compiler
電腦程式
高階語言 \(\rightarrow\) 低階語言(執行檔)
直譯器
dian() -> 01000011 01001011 01000101 01000110 01000111 01001001 01010011 01000011
今天沒時間用,就先用其他的編輯器吧
先用 Code::Blocks 和 Dev-C++
g++ fileName.cpp
./a
g++ fileName.cpp -o outputName
Online Judges
Structure
#include <iostream>
#include <string>
using namespace std;
bool check(string x) {
for (int i = 0; i < x.size(); i++) {
if (x[i] == '8') return 0;
}
return 1;
}
int main() {
string x;
while (cin >> x) {
if (check(x)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
Variables
#include <iostream>
using namespace std;
int main() {
int number = 27;
string name = "Brine";
bool flag = true;
float decimalNumber;
decimalNumber = 3.14;
}
中文 | 宣告時名稱 | 值域 |
---|---|---|
整數 | int | |
浮動點 |
float | |
雙精度浮動點 |
double | |
字元 | char | |
字串 | string | |
布林值 | bool |
名稱 | 效果 |
---|---|
short | 長度砍半 |
long | 無 |
long long | 長度加倍 |
名稱 | 效果 |
---|---|
unsigned | 沒有負數 |
const | 不可修改 |
#include <iostream>
using namespace std;
int main() {
bool brineIsWeak = true;
int a = 5;
int b;
b = a;
float c = 6.4;
double d;
unsigned long long e = 35;
const int M = 1000000007;
}
#include <iostream>
using namespace std;
int main() {
string name = "CKEFGISC27th";
string a = name;
string b = "name";
char c = 'x';
char d = '\n';
char e = name[5];
string student1, student2;
}
I/O
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world\n";
}
#include <iostream>
using namespace std;
int main() {
int a, b;
char c;
cin >> a;
cin >> b >> c;
}
cin >> a >> b;
cin >> a;
cin >> b;
225 27
225
27
#include <iostream>
using namespace std;
int main() {
string fullEnglishName;
cin >> fullEnglishName;
cout << "Hello, " << fullEnglishName << ".\n";
}
#include <iostream>
using namespace std;
int main() {
string fullName;
getline(cin, fullName);
cout << "Morning, " << fullName << ".\n";
}
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// do something here
}
Operator
#include <iostream>
using namespace std;
int main() {
int a;
a = 0;
int b = 4;
int b = a;
a = 3;
cout << a << b;
}
#include <iostream>
using namespace std;
int main() {
int a = 7;
int b = 3;
cout << a + b << '\n';
cout << a - b << '\n';
cout << a * b << '\n';
cout << a / b << '\n';
cout << b / a << '\n';
cout << a % b << '\n';
cout << b % a << '\n';
}
#include <iostream>
using namespace std;
int main() {
int a, b, c, d;
a = b = c = d = 4;
cout << a++ << ' ' << a << '\n';
cout << ++b << ' ' << b << '\n';
cout << --c-- << ' ' << c << '\n';
cout << d-- << ' ' << --d << '\n';
}
#include <iostream>
using namespace std;
int main() {
int a = 15 * 15;
cout << -a << ' ' << a << '\n';
a = -a;
cout << -a << ' ' << a << '\n';
a = a * -1;
cout << a << '\n';
}
#include <iostream>
using namespace std;
int main() {
int a = 18;
const int b = 5;
a += b;
cout << a << '\n';
a -= b;
cout << a << '\n';
a /= b;
cout << a << '\n';
a %= b;
cout << a << '\n';
a *= b;
cout << a << '\n';
}
#include <iostream>
using namespace std;
int main() {
int a = 6;
int b = 9;
int c = 6;
cout << (a == b) << '\n';
cout << (a != b) << '\n';
cout << (a == c) << '\n';
cout << (a < b) << '\n';
cout << (a > b) << '\n';
cout << (a <= b) << '\n';
cout << (a >= b) << '\n';
}
#include <iostream>
using namespace std;
int main() {
cout << ( (6 == 9) && (4 > 2) ) << '\n';
cout << !true << '\n';
cout << !(6 > 9) << '\n';
cout << ( (6 > 9) || (6 <= 9) ) << '\n';
}
#include <iostream>
using namespace std;
int main() {
short a = 3; // 00000000 00000011
short b = 5; // 00000000 00000101
cout << (a & b) << '\n';
cout << (a | b) << '\n';
cout << (a ^ b) << '\n';
cout << ~a << '\n';
a = (a << 2);
cout << a << '\n';
a >>= 1;
cout << a << '\n';
}
If-Else
#include <iostream>
using namespace std;
int main() {
bool asleep = 1;
if (asleep) {
cout << "This is just a dream.\n";
}
}
#include <iostream>
using namespace std;
int main() {
unsigned int chineseFinal;
cin >> chineseFinal;
if (chineseFinal >= 60) {
cout << "You won!\n" << '\n';
} else {
cout << "You lost!\n" << '\n';
}
}
#include <iostream>
using namespace std;
int main() {
int grade;
cin >> grade;
if (grade <= 25) {
cout << "左轉國語實小\n";
} else {
if (grade >= 60) {
cout << "贏光光\n";
} else {
cout << "哭了\n";
}
}
}
#include <iostream>
using namespace std;
int main() {
int possibility;
cin >> possibility;
if (possibility % 2 == 1) cout << "I like the odds.\n";
}
#include <iostream>
using namespace std;
int main() {
int chemGrade;
cin >> chemGrade;
if (chemGrade < 0) {
cout << "投胎 gogo!\n";
} else if (chemGrade < 60) {
cout << "非常好重補修!\n";
} else if (chemGrade > 90 && chemGrade < 95) {
cout << "化學 C 級免修!\n";
} else {
cout << "化學 B 級免修!\n";
}
}
Loops
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (n > 0) {
cout << n << '\n';
n--;
}
}
#include <iostream>
using namespace std;
int main() {
while (true) {
cout << "學弟加建電,學妹加北資!\n"
}
}
#include <iostream>
using namespace std;
int main() {
int counter = 0;
do {
cout << counter << '\n';
} while (counter > 0);
}
#include <iostream>
using namespace std;
int main() {
bool flag = 1;
while (flag) {
cout << "hello\n";
bool copyOfFlag = flag;
flag = 0;
}
cout << copyOfFlag << '\n';
// CE
}
#include <iostream>
using namespace std;
int main() {
for (int i = 2; i < 5; i++) {
cout << i << '\n';
}
int i = 2;
while (i < 5) {
cout << i << '\n';
i++;
}
}
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i > 5) continue;
cout << i << '\n';
}
int counter = 0;
while (counter >= 0) {
if (counter > 10) break;
cout << ++counter << '\n';
}
}
#include <iostream>
using namespace std;
int main() {
int l, int r;
cin >> l >> r;
for (int i = l; i <= r; i++) {
for (int j = i - 1; j > 0; j--) {
if (i % j == 0) {
cout << j << '\n';
break;
}
}
}
}
#include <iostream>
using namespace std;
int main() {
int number;
while (cin >> number) {
cout << number * number << '\n';
}
}
#include <iostream>
using namespace std;
int main() {
int number;
while (cin >> number && number != 0) {
cout << number * number << '\n';
}
}
#include <iostream>
using namespace std;
const int answer = 623;
bool check(int m);
int main() {
int maximum = 1000;
int l, m, r;
for (l = 0, r = maximum + 1; r - l > 1; (check(m) ? l : r) = m) m = l + r >> 1;
}
bool check(int m) {
return m <= answer;
}
Arrays(?)
#include <iostream>
#include <vector>
using namespace std;
int main() {
int length;
cin >> length;
vector<int> arr(length);
for (int i = 0; i < length; i++) {
cin >> arr[i];
}
cout << arr[0] << '\n';
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
int length;
vector<string> name;
for (string& n: name) cin >> n;
}
#include <iostream>
using namespace std;
int main() {
vector<int> v(3, 5); // v = {5, 5, 5};
cout << v.size() << '\n';
v.push_back(7); // v = {5, 5, 5, 7};
v.resize(6); // v = {5, 5, 5, 7, 0, 0};
v.resize(7, 1); // v = {5, 5, 5, 7, 0, 0, 1};
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< vector<int> > graph;
}
#include <iostream>
#incldue <vector>
using namespace std;
int main() {
vector< vector<int> > graph(4, vector<int>(20));
for (vector<int>& v: graph) {
for (int& n: v) {
cin >> n;
}
}
cout << graph[3][13] << '\n';
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< vector<int> > graph(5, vector<int>(7));
for (auto& v: graph) {
for (auto& n: v) {
cin >> n;
}
}
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< vector<int> > graph(5, vector<int>(7));
auto copyOfGraph = graph;
}
#include <iostream>
using namespace std;
int main() {
int length;
cin >> length;
int arr[length];
for (int i = 0; i < length; i++) {
cin >> arr[i];
}
}
Coding Style
#include <iostream>
using namespace std;
int main() {
cout << "lmao\n" << '\n';
}
#include <iostream>
using namespace std;int main(){cout<<"lmao\n"<<'\n';}
Problems
感謝聆聽