学而思4月月赛总结
概况:
未达到满分题:
题目描述
小
输入描述
共
输出描述
共
第 "A will win."
,
如果不知道,输出 "Dont know."
。
第
样例1
输入
0 0 0
0 1 0
0 0 0
输出
Dont know.
1
样例2
输入
0 0 0
0 1 0
0 2 1
输出
A will win.
3
考场代码 :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[4][4]={},cnt=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
cin>>a[i][j];
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(a[i][j]!=0)
cnt++;
if(cnt==1||cnt==2) puts("Dont know.");
else if((a[1][1]==1&&a[3][3]==0)||(a[1][3]==1&&a[3][1]==0)||(a[1][1]==0&&a[3][3]==1)||(a[1][3]==0&&a[3][1]==1))
puts("A will win.");
else if((a[1][2]==1&&a[3][2]==0)||(a[2][1]==1&&a[2][3]==0)||(a[1][2]==0&&a[3][2]==1)||(a[2][1]==0&&a[2][3]==1))
puts("A will win.");
else if((a[1][1]==1&&a[3][3]==1)||(a[1][3]==1&&a[3][1]==1)||(a[1][2]==1&&a[3][2]==1)||(a[2][1]==1&&a[2][3]==1))
puts("A will win.");
else puts("Dont know.");
cout<<cnt;
return 0;
}
正解思路:
由题目中可知,我们能够获取到
- 在九宫格里面轮流放入
和 ,连成 个就赢了 - 小
先 - 小
的第一个子一定是在中心 表示小 , 表示可多, 表示没有落子- 保证棋子数不超过
个 - 可多最多只有
个棋子
我们可以考虑使用 if语句
判断来解决这个问题,直接且时间复杂度低。所以将所有的 Dont know
通过 if语句
排除,剩下的就是 A will win.
Dont know
情况:
- 只有
个子 - 可多下在角落
- 有一列或一行全都落子
正解代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[5][5],cnt=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
cin>>a[i][j];
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(a[i][j]!=0)
cnt++;
if(cnt==1){
puts("Dont know.");
cout<<1;
return 0;
}
if(a[1][1]==2||a[1][3]==2||a[3][1]==2||a[3][3]==2){
puts("Dont know.");
cout<<cnt;
return 0;
}
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++){
a[i][4]=a[i][1]+a[i][2]+a[i][3];
a[4][j]=a[1][j]+a[2][j]+a[3][j];
}
if(cnt==3){
for(int i=1;i<=3;i++){
if(a[i][4]==4||a[4][i]==4){
puts("Dont know.");
cout<<3;
return 0;
}
}
}
puts("A will win.");
cout<<cnt;
return 0;
}
题目描述
学校规定,进校门必须佩戴红领巾。
某一天,可多和同学们共
他们决定,先让
假设进出学校都需要花费
求让所有人都回到学校最少需要花费多少单位时间。
输入描述
输入由多组数据构成。
第一行一个正整数
对于每组数据,有一行两个正整数
输出描述
对于每组数据,输出一行一个整数表示答案。
特别地,如果无法让所有人都回到学校,输出 -1
。
样例
输入
3
6 3
10 10
10 1
输出
5
1
-1
考场代码 :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
if(m==1)
{
puts("-1");
continue;
}
if(n==m)
{
puts("1");
continue;
}
if((n-1)%(m-1)==0) cout<<n/(m-1)*2-1<<endl;
else if(n%(m-1)==0) cout<<n/(m-1)*2-1<<endl;
else cout<<n/(m-1)*2+1<<endl;
}
return 0;
}
正解思路:
什么时候会输出 -1
?只要 -1
。
接下来要考虑一般情况。可以想到,每一次进去
正解代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m;
cin>>n>>m;
if(m==1&&n!=1) puts("-1");
else
{
int t=0;
while(n-m>0)
{
n-=(m-1);
t+=2;
}
t+=1;
cout<<t<<endl;
}
}
return 0;
}
题目描述
有
你喜欢吃五彩缤纷的糖果,所以你获得的糖果不同颜色越多,就越高兴。
输出你能获得的最多的糖果颜色数
输入描述
第一行两个数字
第二行
输出描述
一行一个整数,表示你能获得的最多糖果颜色数。
样例1
输入
7 3
1 2 1 2 3 3 1
输出
3
样例2
输入
10 6
1 3 1 3 4 1 2 1 1 2
输出
4
考场代码 :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k,c[3005],f[3005]={};
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>c[i];
multiset<int> ms;
set<int> s;
for(int i=1;i<=k;i++)
{
s.insert(c[i]);
ms.insert(c[i]);
}
f[k]=s.size();
for(int i=k+1;i<=n;i++)
{
if(ms.count(c[i-k])==1) s.erase(c[i-k]);
ms.erase(ms.find(c[i-k]));
s.insert(c[i]);
ms.insert(c[i]);
f[i]=s.size();
}
int maxn=0;
for(int i=1;i<=n;i++) maxn=max(maxn,f[i]);
cout<<maxn;
return 0;
}
正解思路:
先计算前
正解代码:
#include<bits/stdc++.h>
using namespace std;
int c[300005],sm[300005];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>c[i];
int cur=0,head=1;
for(int i=1;i<=k;i++)
{
sm[c[i]]++;
if(sm[c[i]]==1) cur++;
}
int ans=cur;
for(int i=k+1;i<=n;i++)
{
sm[c[i]]++;
if(sm[c[i]]==1) cur++;
sm[c[head]]--;
if(sm[c[head]]==0) cur--;
head++;
ans=max(ans,cur);
}
cout<<ans;
return 0;
}
题目描述
卡普有
由于卡普非常聪明,只要这不是不可能的,他就能用最少次数的操作做到这一点。
请你找出他最少操作几次能消灭掉所有负数,或者告诉他这不可能。
输入描述
第一行一个正整数
第二行
输出描述
输出一行一个整数表示答案。
如果无解,则输出 -1
。
样例1
输入
5
1 -1 7 2 3
输出
2
样例2
输入
4
3 -1 0 4
输出
1
样例3
输入
1
-7
输出
-1
考场代码 :
#include<bits/stdc++.h>
using namespace std;
int main()
{
bool f0=0;
long long n,c[3005],fs=0;
cin>>n;
for(long long i=1;i<=n;i++)
{
cin>>c[i];
if(c[i]<0) fs++;
if(c[i]==0) f0=1;
}
if(fs%2==0) cout<<fs/2;
else if(f0) cout<<fs/2+1;
else if(fs!=n) cout<<fs*2;
else cout<<-1;
return 0;
}
正解思路:
在输入的同时记录是否有
消灭负数的方法 (设负数个数为
- 两两相乘 (负数个数为
的倍数时), 乘负数 (有 时),- 负数乘整数,再将两个负数相乘 (有正数时),
分类讨论输出即可。
正解代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
bool f0=0;
long long n,c[3000005],fs=0;
cin>>n;
for(long long i=1;i<=n;i++)
{
cin>>c[i];
if(c[i]<0) fs++;
if(c[i]==0) f0=1;
}
if(fs%2==0) cout<<fs/2;
else if(fs%2==1&&f0) cout<<(fs-1)/2+1;
else if(fs%2==1&&fs!=n) cout<<(fs+1)/2+1;
else cout<<-1;
return 0;
}
题目描述
给定
给出编号
已知1号点的坐标是
如果某个点的坐标无法确定,输出 undecidable
。
输入描述
第一行两个整数
接下来
输出描述
样例1
输入
3 2
1 2 2 1
1 3 -1 -2
输出
0 0
2 1
-1 -2
样例2
输入
3 2
2 1 -2 -1
2 3 -3 -3
输出
0 0
2 1
-1 -2
样例3
输入
5 7
1 2 0 0
1 2 0 0
2 3 0 0
3 1 0 0
2 1 0 0
3 2 0 0
4 5 0 0
输出
0 0
0 0
0 0
undecidable
undecidable
正解思路:
正解代码:
题目描述
兰朵在玩自己设计的新游戏,她目前陷入了一场苦战。
战斗的场地由
地板上的数字会描述地板状态。如果地板上是一个正整数,就表示这道关卡的怪物数量,如果为
兰朵可以选择任意一块没有承重柱的地板,然后进行十字切割。
十字切割的效果如下:
- 被选中的地板会拥有十字切割的覆盖范围。
- 从被选中的那块地板开始从上下左右
个方向直线延伸覆盖范围,直到遇到承重柱或者到达战斗场地边界停止。 - 十字切割覆盖范围内的所有怪物都会被击败。
兰朵的体力只允许她使用
输入描述
第一行两个正整数
接下来
输出描述
输出一行一个整数表示答案。
样例1
输入
3 3
1 1 1
1 0 1
0 1 1
输出
5
样例2
输入
5 5
1 9 1 1 9
1 0 1 0 1
0 9 9 9 0
0 1 0 1 1
1 1 1 0 1
输出
31
考场代码 :
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 50;
int n, m;
vector<vector<int> > field;
vector<vector<bool> > visited;
int max_kills = 0;
void dfs(int x, int y, int& kills) {
if (x < 0 || x >= n || y < 0 || y >= m || field[x][y] == 0 || visited[x][y]) {
return;
}
visited[x][y] = true;
kills += field[x][y];
dfs(x + 1, y, kills);
dfs(x - 1, y, kills);
dfs(x, y + 1, kills);
dfs(x, y - 1, kills);
}
int main() {
cin >> n >> m;
field.resize(n, vector<int>(m));
visited.resize(n, vector<bool>(m, false));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> field[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (field[i][j] != 0) {
int kills = 0;
dfs(i, j, kills);
max_kills = max(max_kills, kills);
}
}
}
cout << max_kills << endl;
return 0;
}
正解思路:
正解代码:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】