语法周赛 Round 1 题解
2024年12月25日大约 3 分钟
A+B=B
难度:需要掌握分支以及字符和数字之间的转换。
30 分做法:由于数据中
满分做法:由于数据比较小,只有
#include<bits/stdc++.h>
using namespace std;
int main(){
char x,y;
cin>>x>>y;
int a=x-'A';
int b=y-'A';
int z=a+b;
if(z<10) cout<<char(z+'A');
else{
int g=z%10;//个位
int s=z/10;//十位
cout<<char(s+'A')<<char(g+'A');
}
return 0;
}
ABBA
难度:直接使用循环解决。
满分做法:long long
(最后计算出来的数据量会超出 int
范围)。计算出结果后直接判断即可(用到了 else if
,或者只用 if
语句也可以)。
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
long long c=1,d=1;
for(int i=1;i<=b;i++)
{
c=c*a;
}
for(int i=1;i<=a;i++)
{
d=d*b;
}
if(c>d) cout<<"first";
else if(c<d) cout<<"second";
else cout<<"same";
}
环状字符串
难度:能正确使用字符串即可。
30 分做法:数据的开始和结束在
满分做法:循环从第
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int len=s.length();
int n,m;
cin>>n>>m;
n=n-1;
for(int i=n;i<=n+m-1;i++)
{
cout<<s[i%len];
}
}
T4 照明
难度:需要熟练掌握二维数组以及嵌套循环的使用。
满分做法:首先在二维数组中找出灯所在的位置,并在此位置进行操作,将上下左右都照亮并标记,但是根据题目要求,一旦碰到墙就停止,所以四个方向上都要进行判断,碰到墙或者到达二维数组的边缘就停下来。最后在二维数组中判断有多少点被标记了。
#include <bits/stdc++.h>
using namespace std;
char Map[31][31];
int n,cnt=0,x[901],y[901],cur=0;
bool vis[31][31]={};
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='*')
{
x[++cur]=i;
y[cur]=j;
}
}
}
for(int i=1;i<=cur;i++)
{
int h=x[i],z=y[i];
for(int j=h;j>=1;j--)
{
if(Map[j][z]=='#')
{
break;
}
else
{
vis[j][z]=1;
}
}
for(int j=h;j<=n;j++)
{
if(Map[j][z]=='#')
{
break;
}
else
{
vis[j][z]=1;
}
}
for(int j=z;j>=1;j--)
{
if(Map[h][j]=='#')
{
break;
}
else
{
vis[h][j]=1;
}
}
for(int j=z;j<=n;j++)
{
if(Map[h][j]=='#')
{
break;
}
else
{
vis[h][j]=1;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(vis[i][j]==true)
{
cnt++;
}
}
}
cout<<cnt;
return 0;
}