T3
错误原因:没有写
题意:n*n的方阵,输入一个点的坐标,求与它同行同列同对角线的所有点的坐标
题解
C++#include<bits/stdc++.h>
using namespace std;
int n,i,j;
int main()
{
cin>>n>>i>>j;
for(int l=1;l<=n;l++)cout<<"("<<i<<","<<l<<") ";
cout<<endl;
//行
for(int h=1;h<=n;h++)cout<<"("<<h<<","<<j<<") ";
cout<<endl;
//列
for(int h=1;h<=n;h++)
{
for(int l=1;l<=n;l++)
{
if(i-j==h-l)
cout<<"("<<h<<","<<l<<") ";
}
}
cout<<endl;
//左上到右下
for(int h=n;h>=1;h--)
{
for(int l=1;l<=n;l++)
{
if(i+j==h+l)
cout<<"("<<h<<","<<l<<") ";
}
}
cout<<endl;
return 0;
//左下到右上
}
T4
错误原因:没看懂
题意:先输入词典条目,再输入字符串 S,然后去查找单词,是标点符号则直接输出,不在词典内则输出 UNK。
题解
C++#include<bits/stdc++.h>
using namespace std;
char x;
int f=0,n;
string tmp="";
map<string,string>a;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
string x,y;
cin>>x>>y;
a[x]=y;
}
while(cin>>x)
{
if(x>='a'&&x<='z')//是字母
{
tmp+=x;
f=1;
}
else
{
if(a[tmp]==""&&f==1)cout<<"UNK";
//不在词典内
else if(a[tmp]!=""&&f==1)cout<<a[tmp];
//在词典内
cout<<x;
tmp="";
f=0;
}
}
if(a[tmp]==""&&f==1)cout<<"UNK";
else if(a[tmp]!=""&&f==1)cout<<a[tmp];
return 0;
}
T5
错误原因:不擅长dfs
题意:被1包裹住的0,赋值为2,再将此方阵输出
题解
C++#include<bits/stdc++.h>
using namespace std;
int n,a[35][35],vis[35][35];
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
void dfs(int x,int y)
{
vis[x][y]=1;
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<0||ny<0||nx>n+1||ny>n+1||vis[nx][ny]==1)continue;
//越界或者本来就是1,结束这层循环
dfs(nx,ny);//调用
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]==1)
{
vis[i][j]=1;
}
}
}
dfs(0,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(vis[i][j]==0)//被包裹住的
cout<<2<<" ";//输出2
else cout<<a[i][j]<<" ";//否则输出本身
}
cout<<endl;
}
return 0;
}