@jesseliu612
2020-05-15T16:54:26.000000Z
字数 2043
阅读 1234
这题考察基本位运算。
(x >> (8*i)) & ((1<<8) - 1);
稍微排列组合一下试试即可。
virtual void Fun() {
cout << "A::Fun()" << n+1 << endl;
}
这题主要坑点在于如何输出 6 2 2
,只要设置一个条件不减就可以了。
static int counter;
void print_avaliable() {
cout << counter << " ";
}
virtual void work() {
if(counter - 4 >= 0) counter -= 4;
print_avaliable();
}
重载练习题。
friend Fraction operator * (const Fraction &x, const Fraction &y) {
Fraction ret;
ret.p = x.p * y.p;
ret.q = x.q * y.q;
int d = gcd(ret.p, ret.q);
ret.p /= d;
ret.q /= d;
return ret;
}
friend Fraction operator * (const Fraction &x, const int &y) {
Fraction ret;
ret.p = x.p * y;
ret.q = x.q;
int d = gcd(ret.p, ret.q);
ret.p /= d;
ret.q /= d;
return ret;
}
friend istream& operator >> (istream &is, Fraction &f) {
int x,y;
is >> x >> y;
f.p = x; f.q = y;
return is;
}
friend ostream& operator << (ostream &os, Fraction f) {
int d = gcd(f.p, f.q);
f.p /= d; f.q /= d;
os << f.p;
if (f.q != 1) os << "/" << f.q;
return os;
}
Fraction(int x) : p(x), q(1) {}
Fraction() {}
考察高阶 lambda
表达式的使用。
auto f = [](int d) {
return [d](int x) {
return x % d == 0;
};
};
这题难点在于是把加了之后的结果复制num次,所以每次都需要复制好。
template<typename T>
class MyFunc {
public:
int num;
T &result;
T temp;
MyFunc(int num, T &result): num(num), result(result) {temp = T();}
void operator () (T &x) {
temp += x;
result = T();
for (int i = 1;i <= num ;i += 1) result += temp;
}
};
以下解法用到了 map
用迭代器访问是按照 key
从小到大排序的。
#include <bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
string stmp;
map<int,int> all;
for (int i = 1; i <= n; i += 1) {
cin >> stmp;
if (stmp[0] == 'S') {
int x,y;
cin >> x >> y;
if (all.count(y)) all[y] += x;
else all[y] = x;
} else {
int x;
cin >> x;
int cost = 0;
vector<map<int,int> ::iterator> remove;
for (map<int,int>::iterator it = all.begin(); it != all.end() && x; ++it) {
if(it->second <= x) {
cost += (it->first) * (it->second);
x -= it->second;
it->second = 0;
remove.push_back(it);
} else {
cost += x * it->first;
it->second -= x;
x = 0;
}
}
for (auto &it: remove) {
all.erase(it);
}
if (x) cost += 40 * x;
cout << cost << endl;
}
}
return 0;
}
难点在于需要重载从未重载过的 ->
号,试一下之后发现可以这样操作。
class A {
public:
int x;
A (int x) : x(x) {}
int get_value () { return x; }
A* operator -> () { return this; }
};
A operator + (const A x, const A *y) {
return A(x.x + y->x);
}
class B : public A {
public:
B (int x) : A(x) {}
};
class C : public A {
public:
C (int x) : A(x) {}
};