第一道题:电线上的小鸟:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1007;
int n, zero, one, two;
char dx[N];
int main() {
freopen("bird.in", "r", stdin);
freopen("bird.out", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%s", dx + i);
for (int i = 0; i < n; i++) {
if (i == 0) {
if (dx[i+1] == 'p') zero++;
else one++;
} else if (i == n-1) {
if (dx[i-1] == 'q') zero++;
else one++;
} else {
if (dx[i-1]=='q' && dx[i+1]=='p') zero++;
else if (dx[i-1]=='p' && dx[i+1]=='q') two++;
else one++;
}
}
printf("%d %d %d", zero, one, two);
return 0;
}
第二道题:报数问题
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 207;
int x, y, k, zh1, zh2, bs[N], tmp = -1;
int main() {
freopen("num.in", "r", stdin);
freopen("num.out", "w", stdout);
scanf("%d%d%d", &x, &y, &k);
zh1 = zh2 = x + y;
for (int i = 0; i < zh1; i++)
bs[i] = 1;
while (zh2 > x) {
for (int i = 0; i < k; i++) {
tmp++;
while (bs[tmp%zh1] == 0) tmp++;
}
bs[tmp%zh1] = 0;
zh2--;
}
for (int i = 0; i < zh1; i++)
if (bs[i] == 1)
printf("%d ", i+1);
return 0;
}
第三道题:最少问题
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 107;
int n, a[N], dp[N];
int main() {
freopen("min.in", "r", stdin);
freopen("min.out", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < n; i++)
dp[i] = 99;
dp[0] = 0;
for (int i = 0; i < n; i++)
for (int j = i+1; j <= i+a[i]; j++)
if (j < n)
dp[j] = min(dp[j], dp[i] + 1);
printf("%d\n", dp[n-1]);
return 0;
}
第四道题:翻卡片
#include <iostream>
#include <cstring>
using namespace std;
const int N = 57;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
char a[N][N];
int n, ans, mx;
bool vis[N][N];
void dfs(int x, int y) {
ans++;
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
int xx = x+dx[i], yy = y + dy[i];
if (xx<1 || xx>n || yy<1 || yy>n || a[xx][yy]=='B' || vis[xx][yy])
continue;
dfs(xx, yy);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n ;i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (a[i][j] == 'A') continue;
memset(vis, 0, sizeof vis);
ans = 0;
dfs(i, j);
mx = max(mx, ans);
}
printf("%d", mx);
return 0;
}
第五道题:金箍棒
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e4 + 7, M = 1007, K = 5e6 + 7;
int a[N], s[M], t[N], mn = K;
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i+k-1 <= n; i++) {
for (int j = i; j <= k+i-1; j++) ++s[a[j]];
int sum = 0;
for (int j = 1,x = 1000; j <= x; ) {
if (j == x) s[j] = 0;
if (!s[j]) { ++j; continue; }
if (!s[x]) { --x; continue; }
sum += x - j;
--s[j], --s[x];
}
mn = min(mn, sum);
}
printf("%d", mn);
return 0;
}
第五道题:路线
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 15;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; //位置
bool vis[N][N];
int ans, n, m;
// 走到(x,y)时,已经浇过的花的数量是tot朵
void dfs(int x, int y, int tot) {
if (x==1 && y==1 && tot==m*n) {
//如果走回终点,就是回到了x=1, y=1,并且花的多数为m*n时,就退出。
++ans;
return;
}
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx<1 || xx>m || yy<1 || yy>n || vis[xx][yy])
continue;
vis[xx][yy] = true; //回溯算法
dfs(xx, yy, tot+1);
vis[xx][yy] = false;
}
}
int main() {
scanf("%d%d", &n, &m);
dfs(1, 1, 0); // 位置从x=1, y=1开始,花朵数从0开始
printf("%d\n", ans);
return 0;
}
这道题以我的能力只能拿到80分,请大家笑纳。如果有AC代码,请私信与我。
最后,请各位观众baba点个赞,球球啦~~~