蓝桥模拟二 题解
第一题 矩阵乘法
简单模拟矩阵的乘法
答案:
121824 149688 177552
275886 338985 402084
429948 528282 626616
代码:
import java.util.Scanner;
public class A {
public static void main(String[] args) {
int matrix[][] = new int[3][3];
int base[][] = new int[3][3];
int last[][] = new int[3][3];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = scanner.nextInt();
base[i][j] = matrix[i][j];
last[i][j] = base[i][j];
}
}
for (int m = 0; m < 4; m++) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int temp = 0;
for (int k = 0; k < 3; k++) {
temp += last[i][k] * base[k][j];
}
matrix[i][j] = temp;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
last[i][j] = matrix[i][j];
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
第二题 生日礼物
经典的01背包问题,背包问题有许多变种,诸如完全背包,多重背包等,需要重点掌握,不理解的可以看一下背包九讲的前半部分即可。
答案:58
代码
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int values[] = new int[20];
int weight[] = new int[20];
for (int i = 0; i < 20; i++) {
weight[i] = scanner.nextInt();
values[i] = scanner.nextInt();
}
int dp[] = new int[501];
for (int i = 0; i < 20; i++) {
for (int v = 500; v >= weight[i]; v--) {
dp[v] = Math.max(dp[v], dp[v - weight[i]] + values[i]);
}
}
System.out.println(dp[500]);
}
}
第三题 死神
两种方法,一种直接用excel表格,另一种进行模拟
答案:4100854
c++较繁琐,java可以直接使用Calendar类。
第四题 进制转换
简单题
java答案:s = String.valueOf(tmp) + s;
c/c++答案:s = char(tmp+48)+s;
第五题 表达式求值
c++答案:op.empty()||prority(str[i])>priority(op.top())
java答案:op.empty() || priority(String.valueOf(str[i])) > priority(op.peek())
第六题 合理分配
答案:5
有两种方法,一种是使用区间DP,另一种是用贪心。
区间DP就是在区间上dp,即把整个区间划分为一个个的小区间,在小区间内dp求出最优值,然后把这些小区间合并以后就是整个取件的最优值。
代码:
import java.util.Scanner;
public class F {
static int dp[][] = new int[1001][1001];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 20; i++) {
int l = scanner.nextInt();
int r = scanner.nextInt();
dp[l][r] = 1;//初始化
}
for (int len = 1; len < 1000; len++) {//区间长度
for (int l = 1; l + len <= 1000; l++) {//区间左端点
int r = l + len;//区间右端点
for (int m = l + 1; m <= r; m++) {//将区间从中间进行分割,看它的子区间和是否更大
dp[l][r] = Math.max(dp[l][m] + dp[m][r], dp[l][r]);
}
}
}
System.out.println(dp[1][1000]);
}
}
第七题 回文子串
一道经典的回文题目,java使用Treeset,C++只需将Treeset改为set即可,二者基本等价
java:
import java.util.Scanner;
import java.util.TreeSet;
public class G {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char c[] = scanner.nextLine().toCharArray();
TreeSet<String> res = new TreeSet<>();//以红黑树为基础,可以去重和自动排序
for (int i = 0; i < c.length; i++) {
for (int j = c.length - 1; j >= i; j--) {
StringBuilder left = new StringBuilder();//回文左半部分
StringBuilder right = new StringBuilder();//回文右半部分
int l = i;//左端点
int r = j;//右端点
while (l < r && c[l] == c[r]) {//注意是l<r
left.append(c[l]);//添加左半部分的回文
right.append(c[r]);//添加右半部分的回文
l++;
r--;
}
StringBuilder ans = new StringBuilder();
//补全回文
if (l > r) {//例如abba
//产生了一个偶数位的回文串
ans.append(left).append(right.reverse());//注意要将右半部分反转
res.add(ans.toString());
} else if (l == r) {//例如aba
//产生了一个奇数位的回文串
ans.append(left).append(c[l]).append(right.reverse());
res.add(ans.toString());
}
}
}
for (String s : res) {
System.out.println(s);
}
}
}
第八题 超级栈
较简单,直接对stack进行封装即可,C++基本类似
import java.util.*;
class SuperStack {
private Stack<Integer> stack = new Stack<>();
private int[] re() {
int temp[] = new int[stack.size()];
int index = 0;
while (!stack.isEmpty()) {
temp[index++] = stack.pop();
}
for (int i = index - 1; i >= 0; i--) {
stack.push(temp[i]);
}
return temp;
}
public void reverse() {
int temp[] = re();
stack.clear();
for (int aTemp : temp) {
stack.push(aTemp);
}
}
public void pop() {
stack.pop();
}
public void push(int x) {
stack.push(x);
}
public void print() {
if (stack.isEmpty()) {
System.out.println();
return;
}
int temp[] = re();
for (int i = temp.length - 1; i >= 0; i--) {
System.out.print(temp[i]);
if (i != 0) {
System.out.print(" ");
}
}
System.out.println();
}
}
public class H {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
SuperStack superStack = new SuperStack();
for (int i = 0; i < n; i++) {
int a = scanner.nextInt();
switch (a) {
case 1:
superStack.reverse();
break;
case 2:
superStack.pop();
break;
case 3:
int x = scanner.nextInt();
superStack.push(x);
break;
case 4:
superStack.print();
break;
}
}
}
}