试题B——答案:14
问题描述
小明要用二进制来表示 1 到 10000 的所有整数,要求不同的整数用不同的二进制数表示,请问,为了表示 1 到 10000 的所有整数,至少需要多少个二进制位?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
1 到 10000 的所有整数中,最大的数字为10000,十进制数字10000 转 二进制 为:10 0111 0001 0000,用了14位二进制位。
public class Main {
public static void main(String[] args) {
String temp = Integer.toBinaryString(10000);
System.out.println(temp);
}
}
试题C——答案:2002
问题描述
请问有多少个序列满足下面的条件:
1. 序列的长度为 5。
2. 序列中的每个数都是 1 到 10 之间的整数。
3. 序列中后面的数大于等于前面的数。
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int ans=0;
for(int i=1;i<=10;i++){
for(int j=i;j<=10;j++){
for(int k=j;k<=10;k++){
for(int l=k;l<=10;l++){
for(int m=l;m<=10;m++){
ans++;
}
}
}
}
}
System.out.print(ans);
}
}
试题D——答案:65
问题描述
一个无向图包含 2020 条边,如果图中没有自环和重边,请问最少包含多少个结点?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
含有n个顶点的无向完全图有n*(n-1)/2条边。
试题E——答案:162
问题描述
两个字母之间的距离定义为它们在字母表中位置的距离。例如 A 和 C 的距离为 2,L 和 Q 的距离为 5。
对于一个字符串,我们称字符串中两两字符之间的距离之和为字符串的内部距离。
例如:ZOO 的内部距离为 22,其中 Z 和 O 的距离为 11。
请问,LANQIAO 的内部距离是多少?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
public class E2 {
public static void main(String[] args) {
char arr[] = "LANQIAO".toCharArray();
int answer = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
answer += Math.abs(arr[i]-arr[j]);
}
}
System.out.println(answer);
}
}
试题H
问题描述
小蓝有一张黑白图像,由 n * m 个像素组成,其中从上到下共 n 行,每行从左到右 m 列。每个像素由一个 0 到 255 之间的灰度值表示。
现在,小蓝准备对图像进行模糊操作,操作的方法为:
对于每个像素,将以它为中心 3 * 3 区域内的所有像素(可能是 9 个像素或少于 9 个像素)求和后除以这个范围内的像素个数(取下整),得到的值就是模糊后的结果。
请注意每个像素都要用原图中的灰度值计算求和。
输入格式
输入的第一行包含两个整数 n, m。
第 2 行到第 n + 1 行每行包含 m 个整数,表示每个像素的灰度值,相邻整数之间用一个空格分隔。
输出格式
输出 n 行,每行 m 个整数,相邻整数之间用空格分隔,表示模糊后的图像。
样例输入
3 4
0 0 0 255
0 0 255 0
0 30 255 255
样例输出
0 42 85 127
5 60 116 170
7 90 132 191
数据规模和约定
对于所有评测用例,1 <= n, m <= 100。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s= new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
int[][]a =new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=s.nextInt();
}
}
int[][]b =new int[n][m];
int[] dx ={0, 1, 0, -1, -1, -1, 1, 1};
int[] dy ={1, 0, -1, 0, -1, 1, 1, -1};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int temp=a[i][j];
int num=1;
for(int k=0;k<8;k++){
int tx = i + dx[k];
int ty = j + dy[k];
if(tx>=0 && tx<n && ty>=0 && ty<m){
num += 1;
temp += a[tx][ty];
}
}
b[i][j]=temp/num; //并不是全都除以9
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
试题I
问题描述
小蓝负责花园的灌溉工作。
花园可以看成一个 n 行 m 列的方格图形。中间有一部分位置上安装有出水管。
小蓝可以控制一个按钮同时打开所有的出水管,打开时,有出水管的位置可以被认为已经灌溉好。
每经过一分钟,水就会向四面扩展一个方格,被扩展到的方格可以被认为已经灌溉好。即如果前一分钟某一个方格被灌溉好,则下一分钟它上下左右的四个方格也被灌溉好。
给定花园水管的位置,请问 k 分钟后,有多少个方格被灌溉好?
输入格式
输入的第一行包含两个整数 n, m。
第二行包含一个整数 t,表示出水管的数量。
接下来 t 行描述出水管的位置,其中第 i 行包含两个数 r, c 表示第 r 行第 c 列有一个排水管。
接下来一行包含一个整数 k。
输出格式
输出一个整数,表示答案。
样例输入
3 6
2
2 2
3 4
1
样例输出
9
样例说明
用1表示灌溉到,0表示未灌溉到。
打开水管时:
000000
010000
000100
1分钟后:
010000
111100
011110
共有9个方格被灌溉好。
数据规模和约定
对于所有评测用例,1 <= n, m <= 100, 1 <= t <= 10, 1 <= k <= 100。
广度优先搜索算法(Breadth-First-Search,缩写为 BFS),是一种利用队列实现的搜索算法。简单来说,其搜索过程和 “湖面丢进一块石头激起层层涟漪” 类似。
深度优先搜索算法(Depth-First-Search,缩写为 DFS),是一种利用递归实现的搜索算法。简单来说,其搜索过程和 “不撞南墙不回头” 类似。
BFS 的重点在于队列,而 DFS 的重点在于递归。这是它们的本质区别。
BFS和DFS的直观解释
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Node { // 这个类用来表示一个点 有row 和 col 表示坐标
int row;
int col;
public Node(int row, int col) {
this.row = row;
this.col = col;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] direction = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] garden = new int[n][m];
int tap = in.nextInt();
for (int i = 0; i < tap; i++) {
int r = in.nextInt();
int c = in.nextInt();
garden[r - 1][c - 1] = 1;
}
int time = in.nextInt();
int res = bfs(garden, time, tap, direction);
System.out.println(res);
}
public static int bfs(int[][] garden, int time, int tap, int[][] direction) { // 浇水
if (time <= 0) {
return tap;
}
int count = 0;
int n = garden.length;
int m = garden[0].length;
Queue<Node> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (garden[i][j] == 1) {
q.add(new Node(i, j));
}
}
}
while (time > 0) {
int size = q.size();
for (int i = 0; i < size; i++) {
Node temp = q.poll();
int x = temp.row;
int y = temp.col;
for (int j = 0; j < 4; j++) {
int n_x = x + direction[j][0];
int n_y = y + direction[j][1];
if (isArea(n, m, n_x, n_y)) {
garden[n_x][n_y] = 1;
q.offer(new Node(n_x, n_y));
}
}
}
time--;
}
for (int i = 0; i < n; i++) {// 浇完水之后再统计数量
for (int j = 0; j < m; j++) {
if (garden[i][j] == 1) {
count++;
}
}
}
return count;
}
public static boolean isArea(int row, int col, int x, int y) {
return 0 <= x && x < row && y >= 0 && y < col;
}
}