牛客小白月赛86(真小白)
A.水盐平衡
我的思路
两杯水浓度 a / b and c / d如果浓度a / b 小于 c / d就要加盐 反正加水
这里为什么是 a * d < c * d 是因为 a /(d * (b * d))and c / (d * (b * d))是一样的 证毕✌
Code1:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void solve() {
int a, b, c, d; cin >> a >> b >> c >> d;
if (a * 1.0 / b > c * 1.0 / d) cout << "S\n";
else cout << "Y\n";
}
int main()
{
IO;
int t; cin >> t;
while(t--) {
solve();
}
return 0;
}
Code2:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void solve() {
int a, b, c, d; cin >> a >> b >> c >> d;
if (a * d < c * b) cout << "Y\n";
else cout << "S\n";
}
int main()
{
IO;
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
B.水平考试
我的思路:
在每一次输入的时候是两个字符串,一个正确 一个错误, 小灰可以多加答案或者不加 那么我们可以遍历小蓝的字符串 并判断 是不是在 正确的字符串里 判断是不是存在这个答案,不存在就是0分存在就是10分。
Code1:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void solve() {
string a, b; cin >> a >> b;
for (auto& x : a) {
if (!count(b.begin(), b.end(), x)) {
cout << "0\n";
return ;
}
}
cout << "10\n";
}
int main()
{
IO;
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
Code2:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void solve() {
string s, f; cin >> s >> f;
map<char, int> mp;
for (auto& x : f) mp[x] ++;
int ans = 10;
for (auto& x : s) {
if (!mp[x]) ans = 0;
}
cout << ans << '\n';
}
int main()
{
IO;
int t; cin >> t;
while(t--) {
solve();
}
return 0;
}
C.数组段数
我的思路:
通过前缀和来写 区间相减,这题就是模板题
Code:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N = 2e5 + 5;
int a[N], cnt[N] = {0, 1};
void solve() {
int l, r; cin >> l >> r;
cout << cnt[r] - cnt[l] + 1 << '\n';
}
int main()
{
IO;
int n, t; cin >> n >> t;
cin >> a[1];
for (int i = 2; i <= n; i++) {
cin >> a[i];
cnt[i] = cnt[i - 1];
if (a[i] != a[i - 1]) cnt[i] ++;
}
while (t--) {
solve();
}
return 0;
}
D.剪纸游戏
我的思路:
模板题,dfs模板题 + 矩形面积判断 -- 》 区间判断的同时判断是不是点 并统计它的面积cnt 并统计它的边 如果面积与边 * 边相等, 那么就加1 最后得出答案
Code:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N = 1005;
char arr[N][N];
int n, m, cnt, maxx, maxy, minx, miny;
int dirs[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool isVild(int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '.') {
return true;
}
return false;
}
void dfs(int x, int y) {
arr[x][y] = '*';
cnt ++;
maxx = max(maxx, x), maxy = max(maxy, y);
minx = min(minx, x), miny = min(miny, y);
for (int i = 0; i < 4; i++) {
int dx = dirs[i][0] + x;
int dy = dirs[i][1] + y;
if (isVild(dx, dy)) {
dfs(dx, dy);
}
}
}
bool check(int maxx, int maxy, int minx, int miny, int cnt) {
return (maxx - minx + 1) * (maxy - miny + 1) == cnt;
}
int main()
{
IO;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> arr[i][j];
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == '.') {
cnt = 0, maxx = maxy = 0, minx = miny = N;
dfs(i, j);
if (check(maxx, maxy, minx, miny, cnt)) ans ++;
}
}
}
return cout << ans << '\n', 0;
}
E.可口蛋糕