算法笔记_209:第六届蓝桥杯软件类决赛部分真题(Java语言B组)

时间:2022-09-09 23:25:58

目录

1 分机号

2 五星填数

3 表格计算

 

 

 前言:以下代码仅供参考,若有错误欢迎指正哦~


1 分机号

标题:分机号

X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:

751,520,321 都满足要求,而,
766,918,201 就不符合要求。

现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?

请直接提交该数字,不要填写任何多余的内容。


答案:
120

 

 1 public class Main {
2
3 public static void main(String[] args) {
4 int count = 0;
5 for(int a = 0;a < 10;a++)
6 for(int b = 0;b < 10;b++)
7 for(int c = 0;c < 10;c++)
8 if(a > b && b > c)
9 count++;
10 System.out.println(count);
11 }
12 }

 

 

 

 

 


2 五星填数

标题:五星填数

如【图1.png】的五星图案节点填上数字:
1~12,除去7和11。
要求每条直线上数字和相等。

如图就是恰当的填法。

请你利用计算机搜索所有可能的填法有多少种。
注意:旋转或镜像后相同的算同一种填法。

请提交表示方案数目的整数,不要填写任何其它内容。


答案:
12

 算法笔记_209:第六届蓝桥杯软件类决赛部分真题(Java语言B组)

                                 图1

 

 1 public class Main {
2 public static int count = 0;
3
4 public void swap(int[] A, int i, int j) {
5 int temp = A[i];
6 A[i] = A[j];
7 A[j] = temp;
8 }
9
10 public void check(int[] A) {
11 int sum1 = A[0] + A[2] + A[5] + A[8];
12 int sum2 = A[0] + A[3] + A[6] + A[9];
13 int sum3 = A[1] + A[2] + A[3] + A[4];
14 int sum4 = A[1] + A[5] + A[7] + A[9];
15 int sum5 = A[4] + A[6] + A[7] + A[8];
16 if(sum1 == sum2 && sum1 == sum3 && sum1 == sum4 && sum1 == sum5) {
17 count++;
18 } else
19 return;
20 }
21
22 public void dfs(int[] A, int step) {
23 if(step == A.length) {
24 check(A);
25 return;
26 } else {
27 for(int i = step;i < A.length;i++) {
28 swap(A, i, step);
29 dfs(A, step + 1);
30 swap(A, i, step);
31 }
32 }
33 }
34
35 public static void main(String[] args) {
36 Main test = new Main();
37 int[] A = {1,2,3,4,5,6,8,9,10,12};
38 test.dfs(A, 0);
39 System.out.println(count / 10);
40 }
41 }

 

 

 

 

 


3 表格计算

标题:表格计算

某次无聊中, atm 发现了一个很老的程序。这个程序的功能类似于 Excel ,它对一个表格进行操作。
不妨设表格有 n 行,每行有 m 个格子。
每个格子的内容可以是一个正整数,也可以是一个公式。
公式包括三种:
1. SUM(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的和。
2. AVG(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的平均数。
3. STD(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的标准差。

标准差即为方差的平方根。
方差就是:每个数据与平均值的差的平方的平均值,用来衡量单个数据离开平均数的程度。

公式都不会出现嵌套。

如果这个格子内是一个数,则这个格子的值等于这个数,否则这个格子的值等于格子公式求值结果。

输入这个表格后,程序会输出每个格子的值。atm 觉得这个程序很好玩,他也想实现一下这个程序。

「输入格式」
第一行两个数 n, m 。
接下来 n 行输入一个表格。每行 m 个由空格隔开的字符串,分别表示对应格子的内容。
输入保证不会出现循环依赖的情况,即不会出现两个格子 a 和 b 使得 a 的值依赖 b 的值且 b 的值依赖 a 的值。

「输出格式」
输出一个表格,共 n 行,每行 m 个保留两位小数的实数。
数据保证不会有格子的值超过 1e6 。

「样例输入」
3 2
1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(
1,1:2,1) STD(1,1:2,2)

「样例输出」
1.00 5.00
2.00 3.00
3.00 1.48

「数据范围」
对于
30% 的数据,满足: n, m <= 5
对于
100% 的数据,满足: n, m <= 50


资源约定:
峰值内存消耗(含虚拟机)
< 512M
CPU消耗
< 2000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
  1 import java.util.ArrayList;
2 import java.util.Scanner;
3
4 public class Main {
5 public static int n, m;
6 public static double[][] value;
7
8 public double getSum(int x1, int y1, int x2, int y2) {
9 double sum = 0;
10 for(int i = x1;i <= x2;i++)
11 for(int j = y1;j <= y2;j++)
12 sum = sum + value[i][j];
13 return sum;
14 }
15
16 public double getAvg(int x1, int y1, int x2, int y2) {
17 int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1));
18 double avg = getSum(x1, y1, x2, y2) / count;
19 return avg;
20 }
21
22 public double getStd(int x1, int y1, int x2, int y2) {
23 int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1));
24 double avg = getAvg(x1, y1, x2, y2);
25 double result = 0;
26 for(int i = x1;i <= x2;i++)
27 for(int j = y1;j <= y2;j++)
28 result = result + (value[i][j]-avg) * (value[i][j]-avg);
29 result = Math.sqrt(result / count);
30 return result;
31 }
32
33 public boolean check(int x1, int y1, int x2, int y2) {
34 boolean judge = true;
35 for(int i = x1;i <= x2;i++) {
36 if(!judge)
37 break;
38 for(int j = y2;j <= y2;j++) {
39 if(value[i][j] == -1) {
40 judge = false;
41 break;
42 }
43 }
44 }
45 return judge;
46 }
47
48 public String[] getOperaAndNum(String arrayA) {
49 int p = arrayA.indexOf("(");
50 int q = arrayA.indexOf(")");
51 String opera = arrayA.substring(0, p);
52 arrayA = arrayA.replace(':', ',');
53 String[] num = arrayA.substring(p+1, q).split(",");
54 String[] result = new String[5];
55 result[0] = opera;
56 for(int i = 0;i < 4;i++)
57 result[i + 1] = num[i];
58 return result;
59 }
60
61 public void getResult(String[] A) {
62 value = new double[n][m];
63 ArrayList<String> list = new ArrayList<String>();
64 for(int i = 0;i < n;i++)
65 for(int j = 0;j < m;j++)
66 value[i][j] = -1;
67 for(int i = 0;i < A.length;i++) {
68 String[] arrayA = A[i].split(" ");
69 for(int j = 0;j < arrayA.length;j++) {
70 if(arrayA[j].charAt(0) >= '0' && arrayA[j].charAt(0) <= '9') {
71 value[i][j] = Double.valueOf(arrayA[j]);
72 } else {
73 String[] r = getOperaAndNum(arrayA[j]);
74 String opera = r[0];
75 int x1 = Integer.valueOf(r[1]) - 1;
76 int y1 = Integer.valueOf(r[2]) - 1;
77 int x2 = Integer.valueOf(r[3]) - 1;
78 int y2 = Integer.valueOf(r[4]) - 1;
79 if(check(x1, y1, x2, y2) == false) {
80 list.add(""+i+" "+j+" "+arrayA[j]);
81 continue;
82 }
83 if(opera.equals("SUM"))
84 value[i][j] = getSum(x1, y1, x2, y2);
85 else if(opera.equals("AVG"))
86 value[i][j] = getAvg(x1, y1, x2, y2);
87 else if(opera.equals("STD"))
88 value[i][j] = getStd(x1, y1, x2, y2);
89 }
90 }
91 }
92
93 while(!list.isEmpty()) {
94 for(int i = list.size() - 1;i >= 0;i--) {
95 String[] temp = list.get(i).split(" ");
96 int a = Integer.valueOf(temp[0]);
97 int b = Integer.valueOf(temp[1]);
98 String[] r = getOperaAndNum(temp[2]);
99 String opera = r[0];
100 int x1 = Integer.valueOf(r[1]) - 1;
101 int y1 = Integer.valueOf(r[2]) - 1;
102 int x2 = Integer.valueOf(r[3]) - 1;
103 int y2 = Integer.valueOf(r[4]) - 1;
104 if(check(x1, y1, x2, y2) == false)
105 continue;
106 if(opera.equals("SUM"))
107 value[a][b] = getSum(x1, y1, x2, y2);
108 else if(opera.equals("AVG"))
109 value[a][b] = getAvg(x1, y1, x2, y2);
110 else if(opera.equals("STD"))
111 value[a][b] = getStd(x1, y1, x2, y2);
112 list.remove(i);
113 }
114 }
115 for(int i = 0;i < n;i++) {
116 for(int j = 0;j < m;j++) {
117 System.out.printf("%.2f", value[i][j]);
118 if(j != m - 1)
119 System.out.print(" ");
120 }
121 System.out.println();
122 }
123 }
124
125 public static void main(String[] args) {
126 Main test = new Main();
127 Scanner in = new Scanner(System.in);
128 n = in.nextInt();
129 m = in.nextInt();
130 in.nextLine();
131 String[] A = new String[n];
132 for(int i = 0;i < n;i++)
133 A[i] = in.nextLine();
134 test.getResult(A);
135 }
136 }