8.14训练课
xiezirui1104 · · 学习·文化课
T2 数列找规律
无思路
题意:
有六个数,在样例中找规律,输出这个规律的第N个数
思路:
经过老师提醒后,在纸上列出样例的情况,发现情况是:这一个等于前两个 + (前两个 - 前四个),只要把这个条件加入代码记录就行了
错误代码:
无
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[105];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);//关闭同步流
for(int i = 1 ; i <= 6 ; i++){
cin >> a[i];
}
int n;
cin >> n;
for(int i = 7 ; i <= n ; i++){
a[i] = a[i-2] + (a[i-2] - a[i-4]);//这一个等于前两个 + (前两个 - 前四个)
}
cout << a[n];//输出第N个
return 0;
}
错因:
没有去拆解样例的规律
备注:
建议加上
#define int long long
#define endl '\n'
T5 小田的修行3
有思路(40分)
题意:
有一个n,q,分别代表有几个可以寻找的倍数,有几个要去寻找的数
他得到了一个长度为n的数组,里面有n个数字
接下来他会得到q次询问,每次询问会输入一个数字x,如果x是任意
思路:
一开始使用的是两层循环判断的方法,但后面注意到q的数据结构为“ <= 5 *
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[1005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin >> n >> q;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
while(q--){
int t,f = 0;
cin >> t;
for(int i = 1 ; i <= n ; i++){
if(t % a[i] == 0){
f = 1;
cout << "Yes" << endl;
break;
}
}
if(f == 0){
cout << "No" << endl;
}
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int tong[1000005];
int a[1005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin >> n >> q;
for(int i = 1 ; i <= n ; i++){
int x;
cin >> x;
for(int bs = 1 ; bs * x <= 1e5 ; bs++){
tong[x*bs]=1;
}
}
while(q--){
int t;
cin >> t;
if(tong[t] == 1){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
}
return 0;
}
错因:
因为当时做的是两层循环,会TLE,应该预处理的
T6 MC
无思路
题意:
一个
火把可以照亮:
暗 暗 光 暗 暗
暗 光 光 光 暗
光 光 火把 光 光
暗 光 光 光 暗
暗 暗 光 暗 暗
萤石可以照亮:
光 光 光 光 光
光 光 光 光 光
光 光 萤石 光 光
光 光 光 光 光
光 光 光 光 光
思路:
两个循环(分开),先照着上面的示例把火把求出来,再把萤石求出来,最后来个判断即可
错误代码:
无
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[105][105];//行列表示位置,值为0则此位置未照亮,值为1则此位置被照亮
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);//关闭同步源
int n,m,k;
cin >> n >> m >> k;
while(m--){//火把
int x,y;
cin >> x >> y;
//曼哈顿距离:计算平面上两个坐标的距离
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
//距离为:abs(x - i) + abs(y - j);
if(abs(x - i) + abs(y - j) <= 2){
//枚举的是(i , j)
a[i][j] = 1;
}
}
}
}
while(k--){//萤石
int x,y;
cin >> x >> y;
//这样正常写会有越界,就会是负数
//for(int i = x-2 ; i <= x + 2 ; i++)
//我们可以用max解决问题
//但要注意,我用了#define int long long,如果直接max(1 , x - 2),会报错,得加
//LL(ll也可以)
for(int i = max(1ll , x - 2) ; i <= min(n , x + 2) ; i++){//x轴
for(int j = max(1ll , y - 2) ; j <= min(n , y + 2) ; j++){//y轴
a[i][j] = 1;
}
}
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
cnt += 1 - a[i][j];
}
}
cout << cnt;
return 0;
}
错因:
没有去认真思考这个问题,直接每个火把或萤石加都行
T7 biubiubiu
无思路
题意:
在一个
可以往正北,正南,正东,正西,东北,东南,西北,西南开枪
思路:
经过老师👩🏫讲解,可以使用一个二维数组存下标,四个桶🪣来存横向,纵向,正斜线,反斜线,再去看即可
错误代码:
无
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[105][105];//存下标
int b[1000005];//横向
int c[1000005];//纵向
int d[1000005];//正斜线
int e[1000005];//反斜线
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin >> n >> k;
for(int i = 1 ; i <= k ; i++){
int x,y;
cin >> x >> y;
a[x][y] = 1;//存入敌人位置的下标
b[x]++;
c[y]++;
d[x - y + 100]++;//会有负数,需要偏移
e[x + y]++;
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
int s = b[i] + c[j] + d[i - j + 100] + e[i + j];
if(a[i][j] == 1){
s -= 3;
}
if(s == k){
cnt++;
}
}
}
cout << cnt;
return 0;
}
错因:
没有去想,没有用桶
小P.S.
桶🪣的数组要开到1e5(k的范围)哦!因为求的是横向,可能会有1e5的范围