跳到正文

个人记录

5月21日总结

ZouXihao约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年5月21日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 7 分钟

版本

  1. v1当前

周熙皓5月21日 总结任务

第二题

U450154 寻宝

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int a[N], b[N], n;
long long ans;
bool f(int x) {
	if (x <= 1) return 0;
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0) {
			return 0;
		}
	}
	return 1;
}
//用的判断质数,应该用埃氏筛
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> b[i];
	}
	for (int i = 1; i <= n; i++) {
		if (f(a[i]) == 1) {
			ans += b[i];
		}
	}
	cout << ans;
	return 0;
}

正解

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 2e5 + 10;
ll a[1000005], b[1000005],n;
bool zhi[100000005];
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//要加这条,不然会超时
	zhi[1] = 1;
	cin >> n;
	for(int i=1; i<=n; i++) cin>>a[i];
	for(int i=1; i<=n; i++) cin>>b[i];
	for(ll i=2; i<=10000000; i++){//埃氏筛
		if(zhi[i]==0){
			for(ll j=i*i; j<=10000000; j+=i) zhi[j]=1;
		}
	}
	ll sum = 0;
	for(ll i=1; i<=n; i++){
		if(!zhi[a[i]]) sum+=b[i];//判断有没有出现过,有的话sum加上b[i]
	}
	cout << sum;//输出
	return 0;
}

第三题

U453451 花坛

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int n, a, b, s[N], minn = 1e9;
int main() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> s[i];
	}
    cout<<1;
	return 0;
}

正解

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e8 + 10;
int n, A, B, a[N];
int b[3], ans = INT_MAX;
int main() {
	cin >> n >> A >> B;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int l = 1, r = 1;//定义l,r两个指针
	b[a[1]]++;
	while (r <= n) {
		if (b[1] < A || b[2] < B) {//如果不满足条件,则移动r,将a[r]代表的颜色加一
			r++;
			b[a[r]]++;
		} else {//如果满足条件,则判断白花的最小值,移动指针l,将a[r]表示的颜色减一
			ans = min(b[0], ans);
			b[a[l]]--;
			l++;
		}
	}
	cout << ans;//输出
	return 0;
}

第四题

U452661 宝藏迷宫

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
int ans, n, m;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
char g[10][10];
bool vis[10][10], f;
//没有回溯,没有比大小
void dfs(int x, int y) {
	if (x == n && y == m) {
		cout << ans << endl;
		f = 1;
		return;
	}
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx > 0 && nx <= n && ny > 0 && ny <= m && vis[nx][ny] == 0 && g[nx][ny] != '#') {
			vis[nx][ny] = 1;
			if (g[nx][ny] >= '0' && g[nx][ny] <= '9') {
				int k = g[nx][ny] - '0';
				ans += k;
			}
			dfs(nx, ny);
		}
	}
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> g[i][j];
		}
	}
	vis[1][1] = 1;
	if (g[1][1] >= '0' && g[1][1] <= '9') {
		ans += (g[1][1] - '0');
	}
	dfs(1, 1);
	if (f == 0) {
		cout << -1;
	}
	return 0;
}

正解

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
char a[N][N];
int n, m, sum, maxx = 0;
bool vis[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int x, int y, int sum) {//方向和总值
	if (a[x][y] >= '0' && a[x][y] <= '9')sum += a[x][y] - '0';//如果是数字就加上它
	if (x == n && y == m) {
		maxx = max(maxx, sum);//寻找最大值
		return;
	}
	for (int i = 0; i < 4; i++) {
		int sx = x + dx[i];
		int sy = y + dy[i];
		if (sx > 0 && sx <= n && sy > 0 && sy <= m && vis[sx][sy] == 0 && a[sx][sy] != '#') {//判断条件
			vis[sx][sy] = 1;//标记
			dfs(sx, sy, sum);
			vis[sx][sy] = 0;//回溯
		}
	}
}
signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	vis[1][1] = 1;
	dfs(1, 1, 0);
	cout << maxx ;//输出最大值
	return 0;
}
原文