7.27河西J2训练课day03-课后总结
Score
T1: 100pts, 5min
T2: 100pts, 10min
T3: 20pts, 10min
T4: 100pts, 20min
T5: 0pts, 50min
T6:0pts, 0min
analyze
T1
Problem Description
求一个数字
Approach
::::warning[注意] 如果使用 C/C++ 语言中的取模运算符(%)对负数取模,将会得到一个负数作为结果。在这个负数结果上加上模数,得到的才是正确的取模结果。 :::: 输出该数对2023取模后的结果。
Code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int x;
cin>>x;
int ys=x%2023;
if(ys<0) ys+=2023;
cout<<ys<<endl;
return 0;
}
Summary
非常简单的一道题直接缪杀好题多出。
::::warning[鸣谢]
不辞辛苦的田老师
和蔼可亲的廖老师
::::
T2
Problem Description
如图,在数字
给出一串数字,求出其中零的影子的个数。
By @一扶苏一
Approach
特判 string来存。
Code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string x;
cin>>x;
int cnt=0;
for(int i=0;i<x.size();i++)
{
int num=x[i]-'0';
if(num==2) cnt++;
if(num==6) cnt++;
if(num==8) cnt+=2;
if(num==9) cnt++;
if(num==0) cnt++;
}
cout<<cnt<<endl;
return 0;
}
Summary
非常简单的一道题直接缪杀好题多出。
T3
Problem Description
给出一个
Approach
只有四个数字不至于用双指针优化,所以直接用四个变量存储即可,模拟计时器,遇到回文时间就停止。
Code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a,b;
char mh;
cin>>a>>mh>>b;
while(1)
{
b++;
if(b>=60)
{
b=0;
a++;
}
if(a>=24)
{
a=0;
}
int hq=a/10,hh=a%10,mq=b/10,mh=b%10;
if(hq==mh&&mq==hh)
{
cout<<hq<<hh<<":"<<mq<<mh;
return 0;
}
}
return 0;
}
Summary
考试的时候没有想到用这么暴力的写法,写了一堆if,要养成习惯,不会的题目先把暴力写出来。
T4
Problem Description
随机生成的集合,问集合中有多少个可以构成
Approach
桶计数,三层循环穷举即可。
Code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,a[105],ans=0,tong[10005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++)
{
for(int x=1;x<=n;x++)
{
for(int y=1;y<=n;y++)
{
if(x==y) continue;
if(a[x]+a[y]==a[i])
{
tong[a[i]]++;
if(tong[a[i]]==1)
{
ans++;
}
}
}
}
}
cout<<ans<<endl;
return 0;
}
Summary
暴力枚举的题目很好做,特别是这个数据范围很感人。
T5(这道题着重讲)
题面
小 A 打算前往一片丛林去探险。丛林的地理环境十分复杂,为了防止迷路,他先派遣了一个机器人前去探路。
丛林的地图可以用一个
这个机器人的状态由位置和朝向两部分组成。其中位置由坐标
初始时,机器人的位置为
-
假设机器人当前处在的位置为
(x, y) ,朝向为d 。则它的方向上的下一步的位置(x^′, y^′) 定义如下:若d = 0 ,则令(x^′, y^′) = (x, y + 1) ,若d = 1 ,则令(x^′, y^′) = (x + 1, y) ,若d = 2 ,则令(x^′, y^′) = (x, y - 1) ,若d = 3 ,则令(x^′, y^′) = (x - 1, y) 。 -
接下来,机器人判断它下一步的位置是否在地图内,且是否为空地。具体地说,它判断
(x^′, y^′) 是否满足1 \leq x^′ \leq n, 1 \leq y^′ \leq m ,且(x^′, y^′) 位置上是空地。如果条件成立,则机器人会向前走一步。它新的位置变为(x^′, y^′) ,且朝向不变。如果条件不成立,则它会执行“向右转”操作。也就是说,令d^′ = (d + 1) \bmod 4 (即d + 1 除以4 的余数),且它所处的位置保持不变,但朝向由d 变为d^′ 。
小 A 想要知道,在机器人执行完
【数据范围】
对于所有测试数据,保证:
::cute-table{tuack}
| 测试点编号 | 特殊性质 | |||
|---|---|---|---|---|
| 无 | ||||
| ^ | ^ | ^ | ^ | |
| ^ | ^ | |||
| ^ | ^ | ^ | ^ | |
| 地图上所有位置均为空地 | ||||
| ^ | ^ | ^ | 无 | |
| ^ | 地图上所有位置均为空地 | |||
| ^ | ^ | ^ | 无 | |
| ^ | ^ | ^ | ^ | |
| ^ | ^ | ^ | ^ |
::::error[来源] 2024 CSP-J T2 ::::
Approach
因为在这道题中,“走”和“转”都是属于一步操作,因此我们用循环while(k--)就可以实现模拟走
Code
#include<bits/stdc++.h>
#define endl '\n'
#define qwq ios::sync_with_stdio(0)
#define qwp cin.tie(0),cout.tie(0)
#define int long long
using namespace std;
const int N=1e3+5;
char mp[N][N];
bool vis[N][N];
signed main()
{
qwq;
qwp;
int T;
cin>>T;
while(T--)
{
memset(vis,0,sizeof(vis));
int n,m,k,x,y,d,ans=0;
cin>>n>>m>>k>>x>>y>>d;
string s;
for(int i=1;i<=n;i++)
{
cin>>s;
for(int j=1;j<=m;j++)
{
mp[i][j]=s[j-1];
}
}
vis[x][y]=1;
while(k--)
{
if(d==0)
{
if(x<=n&&y+1<=m&&mp[x][y+1]=='.')
{
y++;
}
else
{
d=(d+1)%4;
}
}
else if(d==1)
{
if(x+1<=n&&y<=m&&mp[x+1][y]=='.')
{
x++;
}
else
{
d=(d+1)%4;
}
}
else if(d==2)
{
if(x<=n&&y>1&&mp[x][y-1]=='.')
{
y--;
}
else
{
d=(d+1)%4;
}
}
else if(d==3)
{
if(x>1&&y<=m&&mp[x-1][y]=='.')
{
x--;
}
else
{
d=(d+1)%4;
}
}
vis[x][y]=1;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(vis[i][j]) ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
Summary
这道题其实做过,但是考试的时候想不起来了,所以比赛题目还是得至少三刷。
T6
Problem Description
一个由
Approach
这道题很经典的DFS全排列问题,我们只需要另外创建一个check函数计算该种排序的和是否为
Code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,sum,box[15][15],tong[13000];
int check()
{
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n-i+1;j++)
{
box[i][j]=box[i-1][j]+box[i-1][j+1];
}
}
if(box[n][1]==sum) return 1;
else return 0;
}
void dfs(int step)
{
if(step>n)
{
if(check()==1)
{
for(int i=1;i<=n;i++)
{
cout<<box[1][i]<<" ";
}
exit(0);
}
else
{
return ;
}
}
for(int i=1;i<=n;i++)
{
if(tong[i]==0)
{
tong[i]=1;
box[1][step]=i;
dfs(step+1);
tong[i]=0;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>sum;
dfs(1);
return 0;
}
Summary
考试时没有分配好时间来不及想这道题了。
整场考试总结
这次考试我觉得花在T5的时间太多了,说明对于CSP-J的T2还是有一点难度,需要多刷。而基础知识需要巩固,现在的短板在于回文数,DFS全排列,排列组合等等。