L1
L1-089
思路
直接输出即可
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Good code is its own best documentation.";
return 0;
}
L1-090
思路
按题意模拟即可
代码
#include <bits/stdc++.h>
using namespace std;
int a, b;
int main() {
cin >> a >> b;
int ans = a + b;
cout << ans - 16 << endl;
cout << ans - 3 << endl;
cout << ans - 1 << endl;
cout << ans << endl;
return 0;
}
L1-091
思路
按题意模拟
代码
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
string s;
int main() {
cin >> n >> s >> m >> k;
if (k == n) cout << "mei you mai ";
else if (k == m) cout << "kan dao le mai ";
else cout << "wang le zhao mai ";
cout << s << " de";
return 0;
}
L1-092
思路
按题意模拟即可
代码
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c;
void solve() {
cin >> a >> b >> c;
if (c == a * b) cout << "Lv Yan" << endl;
else if (c == a + b) cout << "Tu Dou" << endl;
else cout << "zhe du shi sha ya!" << endl;
}
int main() {
cin >> n;
while (n--) solve();
return 0;
}
L1-093
思路
按题意模拟即可
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, k;
int a[N], b[N];
void solve() {
for (int i = 1; i <= n; i++) cin >> b[i];
int ok1 = 0, ok2 = 0;
int n0 = 0;//表示弃权的人数
for (int i = 1; i <= n; i++) {
if (b[i] == 0) n0++;//统计弃权票数
else if (a[i] == b[i]) ok1 = 1;//有人猜对
else ok2 = 1;//有人猜错
}
if (n0 == n) {
cout << "Ai Ya" << endl;
return;
}
if (ok2 == 1) {
cout << "Ai Ya" << endl;
return;
}
if (ok1 == 1) {
cout << "Da Jiang!!!" << endl;
return;
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> k;
while (k--) solve();
return 0;
}
L1-094
思路
按照题意模拟即可
代码
#include <bits/stdc++.h>
using namespace std;
int n;
string s, a, b;
int l, r;
int main() {
cin >> s >> n;
while (n--) {
cin >> l >> r >> a >> b;
string x = s.substr(l - 1, r - l + 1);
s.erase(l - 1, r - l + 1);
string y = a + b;
if (s.find(y) == -1) {
s += x;
continue;
}
int pos = s.find(y);
s.insert(pos + a.size(), x);
}
cout << s;
return 0;
}
L1-095
思路
枚举n0,再算n1,看是不是都满足条件并更新答案
代码
#include <bits/stdc++.h>
using namespace std;
int n, n0, n1;
int main() {
cin >> n0 >> n1 >> n;
int x, y;
int a, b;
int ans = 999999;
for (int i = 2; i <= n0; i++) {//从2开始,因为不许一人一寝
if (n0 % i != 0) continue; //每间人数需相同
a = n0 / i, b = n - a; //计算n1寝室数
if (n1 % b != 0) continue; //同理每间人数相同
if (abs(a - b) < ans) { //更新答案
ans = abs(a - b);
x = a, y = b;
}
}
if (ans == 999999) cout << "No Solution";
else cout << x << ' ' << y;
return 0;
}
L1-096
思路
按题意模拟即可
代码
#include <bits/stdc++.h>
using namespace std;
int sum(int x) {
int s = 0;
while (x) s += x % 10, x /= 10;
return s;
}
int n, a, b;
int s1, s2;
void solve() {
cin >> a >> b;
s1 = sum(a), s2 = sum(b);
int is1 = 0, is2 = 0;
if (a % s2 == 0) is1 = 1;
if (b % s1 == 0) is2 = 1;
if ((is1 == 1 && is2 == 1) || (is1 == 0 && is2 == 0)) {
if (a > b) is1 = -1;
else is2 = -1;
} else if (is1 == 1) is1 = -1;
else if (is2 == 1) is2 = -1;
if (is1 == -1) cout << "A" << endl;
if (is2 == -1) cout << "B" << endl;
}
int main() {
cin >> n;
while (n--) solve();
return 0;
}
L2
L2-045
思路
中模拟,可以用栈来实现
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int ma, cnt;
stack<int> a, b;
int c;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c;
if (a.empty() || c < a.top()) {
a.push(c);
ma = max(ma, (int) a.size());
} else {
if (b.empty() || c > b.top()) {
b.push(c);
} else {
cnt++;
while (!a.empty()) a.pop();
while (!b.empty() && b.top() > c) {
a.push(b.top());
b.pop();
}
a.push(c);
ma = max(ma, (int) a.size());
}
}
}
if (!a.empty()) cnt++;
if (!b.empty()) cnt++;
cout << cnt << ' ' << ma;
return 0;
}
L2-046
思路
中模拟,可以用堆实现
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e3 + 5;
struct node {
string s;//名称
int m;//参赛人数
int y;//考场数
int id;//编号
bool friend operator<(node a, node b) {
return a.m < b.m;
}
} a[N];
int n, c;
vector<int> v;
priority_queue<node> q;
int cnt;
int main() {
cin >> n >> c;
for (int i = 1; i <= n; i++) {
cin >> a[i].s >> a[i].m;
a[i].id = i;
q.push(a[i]);
}
while (!q.empty()) {
node now = q.top();
q.pop();
if (now.m >= c) {
a[now.id].y += now.m / c;
cnt += now.m / c;
now.m %= c;
if (now.m) q.push(now);
} else {
if (v.empty()) {
cnt++;
a[now.id].y++;
v.push_back(now.m);
} else {
int is = 1;
for (int i = 0; i < v.size(); i++) {
if (v[i] + now.m <= c) {
is = 0;
a[now.id].y++;
v[i] += now.m;
break;
}
}
if (is) {
cnt++;
a[now.id].y++;
v.push_back(now.m);
}
}
}
}
for (int i = 1; i <= n; i++) cout << a[i].s << ' ' << a[i].y << endl;
cout << cnt;
return 0;
}
L2-047
思路
用满二叉树模拟
代码
#include <iostream>
using namespace std;
const int N = 1 << 19;
struct node {
int w;//胜者
int l;//败者
} t[N];
int k;
bool solve(int x) {//判断该节点是否满足
if (x >= 1 << k) return 1;
if (t[x].l > t[x].w) return 0;
t[x << 1].w = t[x].w;
t[x << 1 | 1].w = t[x].l;
if (solve(x << 1) && solve(x << 1 | 1)) return 1;
swap(t[x << 1].w, t[x << 1 | 1].w);
if (solve(x << 1) && solve(x << 1 | 1)) return 1;
return 0;
}
int main() {
cin >> k;
for (int i = k; i >= 1; i--)
for (int j = 1 << (i - 1); j < 1 << i; j++)
cin >> t[j].l;
cin >> t[1].w;
if (solve(1))
for (int j = 1 << (k - 1); j < 1 << k; j++)
cout << t[j].w << ' ' << t[j].l << " \n"[j == (1 << k) - 1];
else cout << "No Solution" << endl;
return 0;
}
L2-048
思路
bfs直接,注意可能暴空间,用vecotr
代码
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
int cnt1, cnt2;
struct point {
int x, y;
};
vector<vector<char>> mp;
void init() {
mp.resize(n + 1);
for (int i = 1; i <= n; i++) mp[i].resize(m + 1);
}
void bfs(int i, int j) {
queue<point> q;
int is = 0;
cnt1++;
q.push({i, j});
if (mp[i][j] > '1') is = 1;
mp[i][j] = '0';
while (!q.empty()) {
point now = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int tx = now.x + dx[k];
int ty = now.y + dy[k];
if (tx >= 1 && ty >= 1 && tx <= n && ty <= m) {
if (mp[tx][ty] != '0') {
q.push({tx, ty});
if (mp[tx][ty] > '1') is = 1;
mp[tx][ty] = '0';
}
}
}
}
if (is) cnt2++;
}
int main() {
cin >> n >> m;
init();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] != '0') bfs(i, j)
}
}
cout << cnt1 << ' ' << cnt2;
return 0;
}
L3