跳到正文

个人记录

6-14luokc考试

_YU_MI_约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟

版本

  1. v1当前

质数筛:

赛时代码:

细节问题:质数筛写错了

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
bool isp(ll a){
	if(a==1)return true;
	for(int i=2;i<=sqrt(n);i++){
		if(a%i!=0)return false;
	}
	return true;
}
int main(){
	cin>>n;
	vector<ll>a;
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		if(isp(x))a.push_back(x);
	}
	for(int i=0;i<a.size();i++){
		cout<<a[i]<<' ';
	}
	return 0;
}

AC代码:

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
bool isp(ll a){
	if(a==1)return true;
	for(int i=2;i<=sqrt(a);i++){
		if(a%i!=0)return false;
	}
	return true;
}
int main(){
	cin>>n;
	vector<ll>a;
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		if(isp(x))a.push_back(x);
	}
	for(int i=0;i<a.size();i++){
		cout<<a[i]<<' ';
	}
	return 0;
}

陶陶摘苹果:

AC:

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct p{
	ll x,y;
};
bool cmp(p x, p y){
	return x.y<y.y;
}
int main(){
	ll n,s,a,b,sum=0;
	cin>>n>>s>>a>>b;
	vector<p> x(n);
	for(int i=0;i<n;i++){
		cin>>x[i].x>>x[i].y;
	}
	sort(x.begin(),x.end(),cmp);
	for(int i=0;i<n;i++){
		if(a+b>=x[i].x&&s>=x[i].y){
			sum++;
			s-=x[i].y;
		}
	}
	cout<<sum;
	return 0;
}

单词的长度

AC

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	string a;
	cin>>a;
	cout<<a.size();
	while(cin>>a){
		cout<<","<<a.size();
	}
	return 0;
}

迷宫出口

赛事: 细节错误:加了回溯,起始地没写走过

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct p {
	ll x,y;
};
ll n,x1,y3,x2,y2;
ll a[1000][1000];
bool v[1000][1000],flag=false;
ll dx[]= {-1,1,0,0};
ll dy[]= {0,0,-1,1};
void dfs(ll x,ll y) {
	if(x==x2&&y==y2) {
		flag=1;
		return ;
	}
	for(int i=0; i<4; i++) {
		if((x+dx[i]<=n&&x+dx[i]>=1&&y+dy[i]<=n&&y+dy[i]>=1)&&a[x+dx[i]][y+dy[i]]==0&&!v[x+dx[i]][y+dy[i]]) {
			v[x+dx[i]][y+dy[i]]=1;
			dfs(x+dx[i],y+dy[i]);
			v[x+dx[i]][y+dy[i]]=0;
		}
	}

}
int main() {
	cin>>n;
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			cin>>a[i][j];
		}
	}
	cin>>x1>>y3>>x2>>y2;
	if(a[x1][y3]==1){
		cout<<"NO";
		return 0;
	}
	dfs(x1,y3);
	if(flag)cout<<"YES";
	else cout<<"NO";
	return 0;
}

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 2e5 + 10;
/*
n*n的地图 0表示可以走,1表示不能走
四个方向,从起点走到终点

搜索:能去就去,不能去就返回。
去过的地方不能再去了。

vis[][]标记写在所有dfs的上面
*/
int n, a[110][110]; // 地图
int sx, sy, ex, ey; // 起点和终点位置
bool vis[110][110]; //vis[i][j]=1:标记(i,j)已经走过了
bool flag = 0; // 标记是否找到了终点
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
// 当前走到了第x行y列的位置
void dfs(int x, int y)
{
	//cout << x << " " << y << endl;
	// 判断当前位置是不是终点
	if(x == ex && y == ey)
	{
		flag = 1;
		return; // 结束当前的递归函数
	}
	// 如果不是终点,去周围的四个位置(上下左右)
	//	xx = x + -1, yy = y +  0; // 上面位置的行列号
	//	xx = x +  1, yy = y +  0; // 下
	//	xx = x +  0, yy = y + -1; // 左
	//	xx = x +  0, yy = y +  1; // 右
	//	int dx[] = {-1, 1, 0, 0};
	//	int dy[] = {0, 0, -1, 1}; // 写在上面
	for (int i = 0; i < 4; i++) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		// 必须[1.不出界] [2.可以走] [3.没走过]
		if (xx>=1&&xx<=n&&yy>=1&&yy<=n&&a[xx][yy]!=1&&vis[xx][yy]==0)
		{
			vis[xx][yy] = 1;
			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];
		}
	}
	cin>>sx>>sy>>ex>>ey;
	// 特判起点
	if (a[sx][sy] == 1)
	{
		cout << "NO";
		return 0;
	}
	vis[sx][sy] = 1;
	dfs(sx, sy); // 从起点出发

	if(flag) cout<<"YES";
	else cout<<"NO";
	return 0;
}

选数

赛事:无 思路:想法bfs遍历所有可能的组合,再累加到一起(直接写在bfs传的函数内方便点)再判断是不是质数%%% 赛后重写:

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define bo bool
#define str string
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
bool isp(ll num) {//质数筛
	if (num < 2) return false;
	for (int i = 2; i <= sqrt(num); i++) {
		if (num % i == 0) return false;
	}
	return true;
}
void bfs(vector<ll>& a, ll k, ll s, ll sum, ll& ans){
	if (k == 0) {//跑完了
		if (isp(sum)) ans++;
		return;
	}
	for (int i = s; i < a.size(); i++) {//搜索
		bfs(a, k - 1, i + 1, sum + a[i], ans);
	}
}
int main(){
	ll n,m,ans=0;
	n=read(),m=read();
	vector<ll>a(n,0);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	bfs(a, m, 0, 0, ans);
	cout<<ans;
	return 0;
}

AC:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 25;

int n, k, ans;
int a[25], vis[25];

bool isprime(int x) {
	if (x <= 1) return 0;
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0) return 0;
	}
	return 1;
}

void dfs(int u) // 当前选择到了第u个数字,已经选中了num个
{
	if (u > n) {
        int sum = 0, cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (vis[i]) sum += a[i], cnt++;
        }
        if (cnt == k && isprime(sum)) ans++;
		return;
	}

	// 选或不选
	vis[u] = 1;
	dfs(u + 1);
	vis[u] = 0;
	dfs(u + 1);
}

int main()
{
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(1);
	cout << ans;
	return 0;
}

全排列问题

赛事: 空格减1就AC

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
vector<ll>a(10);
bool v[10];
void dfs(ll p){
	if(p==n+1){
		for(int i=1;i<=n;i++){
			cout<<"     "<<a[i];
		}
		cout<<"\n";
		return ;
	}
	for(int i=1;i<=n;i++){
		if(v[i]==1){
			continue;
		}
		a[p]=i;
		v[i]=1;
		dfs(p+1);
		v[i]=0;
	}
}
int main(){
	cin>>n;
	dfs(1);
	return 0;
}
原文