科大讯飞笔试

算法1

图像的卷积是对图像处理的最常用的方法,当前深度神经网络中的卷积神经网络当中也用到了卷积的操作来提取特征,卷积一般包含以下几个概念:

卷积核中心:卷积数组的中心位置。

卷积操作:将卷积核中心对准输入的某个元素,两个矩阵会有重合区域,将两个矩阵的重合区域中的对应重合元素相乘求和。再将卷积核分别从左到右,从上到下进行滑动。

这里设置步长为1.

img

代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt(); //行
int n = scanner.nextInt(); //列
int k = scanner.nextInt();//行
int l = scanner.nextInt();//列
int arr[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = scanner.nextInt();
}
}
int p[][] = new int[k][l];
for (int i = 0;i<k;i++){
for (int j = 0;j<l;j++){
p[i][j] = scanner.nextInt();
}
}

int res[][] = new int[m][n];//结果数组
for (int i = 0;i<m;i++){
for (int j = 0;j<n;j++){
int sum = 0;
for (int r = -(k/2);r<=k/2;r++){
for (int c = -(l/2);c<=l/2;c++){
int x = i+r;
int y = j+c;
if (x>=0 &&x<m && y>=0 && y<n){
sum+=arr[x][y] * p[r+k/2][c+l/2];
}
}
}
if (sum> 255){
sum = 255;
}
if (sum<0){
sum = 0;
}
res[i][j] = sum;
}
}
for (int i = 0;i<res.length;i++){
for (int j = 0;j<res[0].length;j++){
System.out.print(res[i][j]+" ");
}
System.out.println();
}
}
}

算法2

力扣岛屿的最大面积,变形题:

代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Scanner;

public class Main2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
int N = scanner.nextInt();
int m = M;
int n = N;
int area[][] = new int[m][n];
while (m-->0){
while (n-->0){
area[m][n] = scanner.nextInt();
}
n = N;
}
m= M;
n = N;
int t;
while (m-->0){
while (n-->0){
t = scanner.nextInt();
area[m][n] = area[m][n]==t?1:0;
}
n = N;
}
int ans = 0;
for (int i = 0;i!=M;++i){
for (int j = 0;j!=N;++j){
ans = Math.max(ans,dfs(area,i,j));
}
}
System.out.println(ans);
}
public static int dfs(int [][]area,int cur_i,int cur_j){
if (cur_i<0||cur_j<0||cur_i==area.length||cur_j==area[0].length||area[cur_i][cur_j]!=1){
return 0;
}
area[cur_i][cur_j] = 0;
int []di = {0,0,1,-1};
int dj[] = {1,-1,0,0};
int ans = 1;
for (int index = 0;index!=4;++index){
int next_i = cur_i+di[index],next_j = cur_j+dj[index];
ans+=dfs(area,next_i,next_j);
}
return ans;
}

}

算法3


科大讯飞笔试
http://example.com/2022/09/18/科大讯飞笔试/
作者
zlw
发布于
2022年9月18日
许可协议