2021年1月29日 星期五

c199: 爬山去(Hiking)-TOI練習賽y7m5-1

 程式碼

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int n;
	while (cin >> n)
	{
		int count = 0, top, tmp;
		vector<int> m;
		cin >> tmp;
		m.push_back(tmp);
		for (int i = 1; i < n; i++)
		{
			cin >> tmp;
			if (tmp != m.back()) m.push_back(tmp);
		}
		for (int i = 1; i < m.size()-1; i++)
		{
			if (m[i - 1] < m[i] && m[i] > m[i + 1])
				count++;
		}
		cout << count << endl;
	}
	return 0;
}

2021年1月28日 星期四

f373: 週年慶 Anniversary

 程式碼:


#include <iostream>
using namespace std;

int main()
{
	int n, flag1, flag2;
	cin >> n;
	flag1 = n - (n / 2000) * 200;
	flag2 = n - (n / 1000) * 100;
	if (flag1 <= flag2) cout << flag1 << " 0";
	else cout << flag2 << " 1" << endl;
	return 0;
}

f441: 評分系統 Score

程式碼:

#include <iostream>
using namespace std;

int main()
{
	int n, points,studentN;
	cin >> n >> points;
	int* ans = new int[n];
	for (int i = 0; i < n; i++)
		cin >> ans[i];
	cin >> studentN;
	while (studentN--)
	{
		int score = 0, p;
		for (int i = 0; i < n; i++)
		{
			cin >> p;
			if (p == ans[i]) score += points;
		}
		cout << score << endl;
	}
}

2021年1月27日 星期三

2021年1月26日 星期二

e548: 11995 - I Can Guess the Data Structure!

程式碼

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

int main()
{
	int n, x, command;
	while (cin >> n)
	{
		stack<int> st;
		queue<int> q;
		priority_queue<int> pq;
		bool isStack = true, isQueue = true, isPQ = true;
		while (n--)
		{
			cin >> command >> x;
			if (!isStack && !isQueue && !isPQ) continue;
			if (command == 1)
			{
				st.push(x); q.push(x); pq.push(x);
			}
			else
			{
				if (st.empty()||st.top() != x) isStack = false;
				if (q.empty()||q.front() != x) isQueue = false;
				if (pq.empty()||pq.top() != x) isPQ = false;
				st.pop(); q.pop(); pq.pop();
			}
		}
		if (!isStack && !isQueue && !isPQ) cout << "impossible" << endl;
		else if (isStack && !isQueue && !isPQ) cout << "stack" << endl;
		else if (!isStack && isQueue && !isPQ) cout << "queue" << endl;
		else if (!isStack && !isQueue && isPQ) cout << "priority queue" << endl;
		else cout << "not sure" << endl;
	}
	return 0;
}

【Come From Away】Stop the World

pinch yourself: 掐自己一下(表示某事太好或太奇怪,簡直令人難以置信)
lookout: 觀景處
take the time: 花費力氣(做某事)


virtual base class

使用時機
當一個class繼承自不同的多個class時,會繼承到他們的資料定義。這時可能會有重複的資料被繼承到,造成運作不符合預期,例如:

#include <iostream>
using namespace std;

class CA //common base class of CB and CC
{
public:
	int x;
	CA(int a = 0) { x = a; }
};
class CB :public CA
{
public:
	int y;
	CB(int a = 0, int b = 0) :CA(a) { y = b; }
};
class CC :public CA
{
public:
	int z;
	CC(int a = 0, int b = 0) :CA(a) { z = b; }
};
class CD : public CB, public CC
{
public:
	int w;
	CD(int a = 0, int b = 0, int c = 0, int d = 0 ,int e = 0) :CB(a, b), CC(c, d) {
		w = e;
	}
	void ShowVal() {
		cout << "x = " << CB::x << " y = " << y
		<< " x = " << CC::x << " z = " << z;
		cout << " w = " << w  << endl;
	}
};
int main()
{
	CD obj(5, 4, 3, 2, 1);
	obj.ShowVal(); //what happens?
	return 0;
}

輸出為: x = 5 y = 4 x = 3 z = 2 w = 1

如果改以virtual 方式繼承,相同的資料只會用到同一塊記憶體。例如:

#include <iostream>
using namespace std;

class CA //common base class of CB and CC
{
public:
	int x;
	CA(int a = 0) { x = a; }
};
class CB : virtual public CA
{
public:
	int y;
	CB(int a = 0, int b = 0) :CA(a) { y = b; }
};
class CC : virtual public CA
{
public:
	int z;
	CC(int a = 0, int b = 0) :CA(a) { z = b; }
};
class CD : public CB, public CC
{
public:
	int w;
	CD(int a = 0, int b = 0, int c = 0, int d = 0
		,int e = 0) : CA(a), CB(a, b), CC(c, d) {
		w = e;
	}
	void ShowVal() {
		cout << "x = " << CB::x << " y = " << y
		<< " x = " << CC::x << " z = " << z;
		cout << " w = " << w << " x = " << x << endl;
	}
};
int main()
{
	CD obj(5, 4, 3, 2, 1);
	obj.ShowVal(); //what happens?
	return 0;
}

輸出為: x = 5 y = 4 x = 5 z = 2 w = 1 x = 5


呼叫順序

constructors
first, virtual base classes in declaration order
then, other base classes in declaration order

destructors
 in the reverse order of the constructors

2021年1月25日 星期一

f277: 嘿嘿想不到吧

程式碼

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Student
{
public:
	string name, sentence;
	int _class, index;
};
bool cmp(Student s1, Student s2)
{
	if (s1._class < s2._class) return true;
	if (s1._class > s2._class) return false;
	if (s1.index < s2.index) return true;
	else return false;
}
int main()
{
	int n;
	while (cin >> n)
	{
		vector<Student> school;
		for (int i = 0; i < n; i++)
		{
			Student tmp;
			cin >> tmp.name >> tmp._class >> tmp.index >> tmp.sentence;
			school.push_back(tmp);
		}
		sort(school.begin(), school.end(), cmp);
		for (int i = 0; i < school.size(); i++)
		{
			cout << school[i]._class << " " << school[i].index << " " << school[i].name << endl<< school[i].sentence;
			if (i != school.size() - 1)cout << endl;
		}
	}
	return 0;
}

【Matilda】revolting children

revolt: 反抗、反叛
Get the best of sb: 被某人(負面)影響
chokey: 禁閉室
bar: 鐵欄杆
horde: 一群人
chalk: 粉筆

2021年1月22日 星期五

【Come From Away】On The Edge

jumping at our own shadows: 易受驚嚇的
suppertime: 晚餐時間
I beg your pardon: 不好意思、能再重說一次嗎?
airspace: 領空
torn up: 磨損
debris: 碎屑
asphalt: 柏油
landfall: 登陸
crack: 身心疲憊、精神崩潰
chilli: 辣味肉豆

2021年1月21日 星期四

【Come From Away】38 Planes (Reprise) / Somewhere in the Middle of Nowhere

pick up: 增加
turbulence: 不穩定氣流、顛簸
canoodle: 親熱
pace: 步調
seat back: 座椅靠背
tray table: (可折攏的)餐桌
taxiing: 滑行