#深信服校园招聘c/c++ 软件开发A卷
软件开发A卷目录
第一题-围棋遍历(水题,递归搜索,有小坑)
直接上题目:
函数calc计算围棋中位置(x,y)处连成一片的棋子个数。所谓连成一片,即沿着棋盘横竖线往任意方向遍历,遍历过程允许转弯,不允许走斜线,中间未出现对方棋子或空子。
enum color {
NONE, WHITE, BLACK, // 棋子颜色,NONE表示未落子
};
struct weiqi {
enum color board[19][19]; // 棋盘上每个位置的落子
};
int calc(struct weiqi *wq, int x, int y)
{
}
前台测试样例
输入描述:
第1-19行数据是棋盘上棋子的颜色数据。0表示未落子,1表示白子,2表示黑子。 第1行最左边位置的坐标是(0,0)
第1行第2列的坐标是(1,0),第2行第1列的坐标是(0,1),依此类推。 第20行数据是起始坐标(x,y)
输出描述:
与坐标(X,Y)连成一片的棋子数目
输入例子1:
0000000000000000000
0000011000000000000
0000001111000000000
0000001021000000000
0000001010100000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
5,1
输出例子1:
9
题目太简单了,直接类迷宫递归搜索就出来了。
但是有小坑:x与y是反着的,测试样例的5 1 其实是第一行第五个,不是第五行第一个
所以我调了半天都没找到问题在哪…最后才发现。
代码实现如下
void dfs(struct weiqi *wq, int x, int y,int flag,int &res)
{
if(x<0||x>19||y<0||y>19)return;
if(wq->board[y][x]!=flag) return;
res++;
wq->board[y][x]=NONE;
dfs(wq,x-1,y,flag,res);
dfs(wq,x+1,y,flag,res);
dfs(wq,x,y-1,flag,res);
dfs(wq,x,y+1,flag,res);
return;
}
int calc(struct weiqi *wq, int x, int y)
{
//TODO:
int res=0;
int flag=wq->board[y][x];
if(flag==NONE) return 0;
else
dfs(wq,x,y,flag,res);
return res;
}
第二题-单链表排序(常规题,考察链表的基本操作)
请实现list_sort,使用冒泡法将head指向的链表按val值大小排成升序
struct node {
int val;
struct node *next;
};
void list_sort(struct node *head)
{
}
输入描述:
第一行为数据个数 第二行为输入的数据,以空格进行分隔
输出描述:
输出head指向的链表数据,以空格分隔
输入例子1:
12
10 22 2 5 9 8 1 33 4 6 7 9
输出例子1:
1 2 4 5 6 7 8 9 9 10 22 33
这个题与leetcode合并k个有序链表有共通之处,也可以直接暴力把数组排序然后链表重建,也可以链表排序
//冒泡排序链表
static void list_sort(struct node *head)
{
//TODO:
node *h=head;
node *ne;
int temp=0;
while(h){
ne=h->next;
while(ne){
if(h->val>ne->val){
temp=h->val;
h->val=ne->val;
ne->val=temp;
}
ne=ne->next;
}
h=h->next;
}
}
//提出数组来排序(不太符合题意)
static void list_sort(struct node * &head)
{
int len=0;
node *temp=head;
while(temp!= nullptr) {len++;temp=temp->next;}
int *arr=new int[len];
temp=head;
int coun=0;
while(temp!= nullptr){
arr[coun++]=temp->val;
temp=temp->next;
}
std::sort(arr,arr+len);
head=NULL;
head=list_create(arr,len);
}
第三题-出栈顺序(没做过类似题,递归,有小坑,没做出来(无思路))
已知某一个字母序列,把序列中的字母按出现顺序压入一个栈,在入栈的任意过程中,允许栈中的字母出栈,求所有可能的出栈顺序
输入描述:
字符串,如:abc
输出描述:
可能的出栈顺序,每行一种顺序
输入例子1:
abc
输出例子1:
abc
acb
bac
bca
cba
这个题就把我难倒了,题意理解了之后又写不出来
关键点在于新数入栈时可以考虑先弹出栈里面的数,也可以不弹,这个我不知道怎么来写
最后看了别人的写法,这个递归就用的十分巧妙
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<string> &res,string s1,stack<char> s,int k,string r)
{
if(k==s1.size())
{
while(!s.empty())
{
r+=s.top();
s.pop();
}
if(find(res.begin(), res.end(), r)==res.end()) //去重
res.push_back(r);
}
else{
s.push(s1[k]);
dfs(res,s1,s,k+1,r);//入栈时不弹出数
s.pop();
if(!s.empty())
{
r+=s.top(); //新入栈时弹出数
s.pop();
dfs(res,s1,s,k,r);
}
}
}
int main()
{
string s1;cin>>s1;
vector<string> res;
stack<char> s;
string r="";
dfs(res,s1,s,0,r);
sort(res.begin(),res.end());
for(int c=0;c<res.size();c++)
cout<<res[c]<<endl;
return 0;
}
结语-自我评估
总体来说A卷难度不高,还是自己能力不足,特别是在递归方面还有待加强,需要更加努力提高实力