A.小d和答案修改
题目链接
分析:这是道简单的语法题,大写变小写,小写变大写,A与a的ASCII值差32。
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
int cnt=s.size();
for(int i=0;i<cnt;i++)
{
if(s[i]>='a') s[i]-=32;
else s[i]+=32;
}
cout<<s<<endl;
return 0;
}
B.小d和图片压缩
题目链接
分析:8说了,直接上代码。
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n,m;
int c[N][N];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) cin>>c[i][j];
}
for(int i=1;i<=n/2;i++)
{
for(int j=1;j<=m/2;j++)
{
cout<<(c[2*i-1][2*j-1]+c[2*i-1][2*j]+c[2*i][2*j-1]+c[2*i][2*j])/4<<" ";
}
puts("");
}
return 0;
}
C.小d和超级泡泡堂
题目链接
分析:这题相当于遍历从该点出发可覆盖的范围,直接拿个变量记录其中是杂草的格子即可,由于数值较小,可用dfs,但此处代码我才用bfs。
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 1010;
int n,m,sx,sy,res;
char g[N][N];
bool st[N][N];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
queue<PII> q;
void bfs(int x,int y)
{
st[x][y];
q.push({x,y});
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int xx=t.x+dx[i],yy=t.y+dy[i];
if(xx<1||xx>n||yy<1||yy>m) continue;
if(g[xx][yy]=='#'||st[xx][yy]) continue;
st[xx][yy]=true;
q.push({xx,yy});
if(g[xx][yy]=='!') res++;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>g[i][j];
if(g[i][j]=='@') sx=i,sy=j;
}
}
bfs(sx,sy);
cout<<res<<endl;
return 0;
}
D.小d和孤独的区间
题目链接
分析:找出当前1与前一个1和后一个1的长度相乘即可。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int n,cnt;
long long res;
int a[N],p[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]) p[++cnt]=i;
}
p[0]=0,p[cnt+1]=n+1;
for(int i=1;i<=cnt;i++) res+=1ll*(p[i]-p[i-1])*(p[i+1]-p[i]);
printf("%lld\n",res);
return 0;
}
E.小d的博弈
题目链接
分析:博弈论问题是真的不会,单独分析行和列可以知道,每次分割可以分成i行和x-i行,两种取最小值,即当n为偶数时,最大取n/2-1;n为奇数时,最大取n/2下取整。然后统计他们分割到最后只剩1或2时便不可再分割。如果两个分割的次数相等,也就是先手对行进行操作,后者可模仿先手对列进行操作,那么后手必胜。
#include<bits/stdc++.h>
using namespace std;
int T;
int n,m;
int f(int x)
{
int t=0;
while(x>2)
{
t++;
if(x&1) x/=2;
else x=x/2-1;
}
return t;
}
int main()
{
cin>>T;
while(T--)
{
cin>>n>>m;
int cnt1=f(n),cnt2=f(m);
if(cnt1==cnt2) puts("Bob");
else puts("Alice");
}
return 0;
}