本文代码亲测AC
如有不足,请指正
A题
代码
#include <bits/stdc++.h>
using namespace std;
int cnt;
int main(){
int n;
string ans = "Yes";
cin >> n;
for (int i = 1;i <= n;i ++){
if (cnt == 2) ans = "No";
string s;
cin >> s;
if (s == "sweet") cnt ++;
else cnt = 0;
}
cout << ans;
}
解析
1 <= N <= 100,范围不大,直接记录当前连续吃甜菜数即可。
B题
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char c[55][55];
int h,w,si,sj;
string x;
int main(){
cin >> h >> w >> si >> sj;
for (int i = 1;i <= h;i ++)
for (int j = 1;j <= w;j ++)
cin >> c[i][j];
cin >> x;
int now_i = si,now_j = sj;
for (int i = 0;i < x.size();i ++){
int go_i = now_i,go_j = now_j;
if (x[i] == 'U')
go_i -= 1;
else if (x[i] == 'D')
go_i += 1;
else if (x[i] == 'L')
go_j -= 1;
else
go_j += 1;
if (go_i >= 1 and go_i <= h and go_j >= 1 and go_j <= w and c[go_i][go_j] == '.')
now_i = go_i,now_j = go_j;
}
cout << now_i << " " << now_j << endl;
}
解析
根据输入移动位置即可
C题
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y;
int A[222222],B[222222];
int main(){
cin >> n >> x >> y;
for (int i = 1;i <= n;i ++) cin >> A[i];
for (int i = 1;i <= n;i ++) cin >> B[i];
sort(A+1,A+n+1);
reverse(A+1,A+n+1);
sort(B+1,B+n+1);
reverse(B+1,B+n+1);
ll atry = 1,btry = 1;
ll sum = 0;
while (sum <= x){
sum += A[atry],atry ++;
if (atry == n + 1){
atry = n+1;
break;
}
}
sum = 0;
while (sum <= y){
sum += B[btry],btry ++;
if (btry == n + 1){
btry = n+1;
break;
}
}
cout << min(atry-1,btry-1);
return 0;
}
解析
甜度数组从大往小排,一直加到总甜度>x;
咸度数组从大往小排,一直加到总咸度<y;
输出两种取法中小的一个即可
文章最后
你的点赞是我最大的动力
创作不易
点个小赞