科大讯飞2018春招的笔试编程题中,第一题是判断蚊子是否在蚊帐中,比较简单,这里不做说明,本文将简单讲解第二题和重点说明第三题的解法。
第二题 |
3.思路
这题思路很容易想到,最后拿到的工资可以用m * n - wrongNum * x -k * (rightNum - wrongNum)
来计算,其中rightNum是指批改正确的份数,wrongNum是指批改错误的份数。但是有一点需要注意的是,当rightNum**小于**wrongNum的时候,不能再用这个公式计算,应该使用m * n - wrongNum * x
来计算。
4.代码实现
import java.util.Scanner;
public class Inter2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] inputSs = scanner.nextLine().split(" ");
int n = Integer.parseInt(inputSs[0]);
int m = Integer.parseInt(inputSs[1]);
int x = Integer.parseInt(inputSs[2]);
int k = Integer.parseInt(inputSs[3]);
int rightNum = 0;
int wrongNum = 0;
String[] inputSs2 = scanner.nextLine().split(" ");
for(int i = 0; i < inputSs2.length; i ++) {
if(inputSs2[i].equals("0")) {
wrongNum ++;
} else {
rightNum ++;
}
}
if(rightNum >= wrongNum) {
System.out.print(m * n - wrongNum * x -k * (rightNum - wrongNum));
} else {
System.out.print(m * n - wrongNum * x);
}
}
}
第三题 |
3.思路
这道题思路不是很清晰,最容易想到的方法应该是将测试板上的字符存入二维数组中,对于每个操作分别实现一个变换方法。具体见代码。
4.代码实现
提醒:这个代码是笔试结束才完全写完的,只经过了给出的两个用例的测试,不保证完全正确。
import java.util.Scanner;
public class Inter3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
int n = Integer.parseInt(scanner.nextLine());
String[][] input = new String[n][n];
for(int i = 0; i < n; i ++) {
char[] inputChars = scanner.nextLine().toCharArray();
for(int j = 0; j < inputChars.length; j ++) {
input[i][j] = String.valueOf(inputChars[j]);
}
}
//获取指令,并执行相应操作
String[] orders = scanner.nextLine().split(" ");
for(String order : orders) {
switch(order) {
case "<":
input = turnLeft(input);
break;
case ">":
input = turnRight(input);
break;
case "|":
input = flipVertical(input);
break;
case "-":
input = flipHorizontal(input);
break;
case "\\":
input = turnDiagonalt(input);
break;
case "/":
input = turnAntiDiagonalt(input);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(input[i][j]);
}
System.out.println();
}
}
}
//向左旋转90度
public static String[][] turnLeft(String[][] input) {
int length = input.length;
String[][] temp = new String[length][length];
String oldChar = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
oldChar = input[j][length - 1 - i];
if(oldChar.equals("<")) {
temp[i][j] = "v";
} else if(oldChar.equals(">")) {
temp[i][j] = "^";
} else if(oldChar.equals("^")) {
temp[i][j] = "<";
} else if(oldChar.equals("v")) {
temp[i][j] = ">";
} else if(oldChar.equals("|")) {
temp[i][j] = "-";
} else if(oldChar.equals("-")) {
temp[i][j] = "|";
}else if(oldChar.equals("\\")) {
temp[i][j] = "/";
} else if(oldChar.equals("/")) {
temp[i][j] = "\\";
} else {
temp[i][j] = oldChar;
}
}
}
return temp;
}
//向右旋转90度
public static String[][] turnRight(String[][] input) {
int length = input.length;
String[][] temp = new String[length][length];
String oldChar = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
oldChar = input[length - 1 - j][i];
if(oldChar.equals("<")) {
temp[i][j] = "^";
} else if(oldChar.equals(">")) {
temp[i][j] = "v";
} else if(oldChar.equals("^")) {
temp[i][j] = ">";
} else if(oldChar.equals("v")) {
temp[i][j] = "<";
} else if(oldChar.equals("|")) {
temp[i][j] = "-";
} else if(oldChar.equals("-")) {
temp[i][j] = "|";
}else if(oldChar.equals("\\")) {
temp[i][j] = "/";
} else if(oldChar.equals("/")) {
temp[i][j] = "\\";
} else {
temp[i][j] = oldChar;
}
}
}
return temp;
}
//沿水平方向翻转
public static String[][] flipHorizontal(String[][] input) {
int length = input.length;
String[][] temp = new String[length][length];
String oldChar = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
oldChar = input[length - 1 - i][j];
if(oldChar.equals("^")) {
temp[i][j] = "v";
} else if(oldChar.equals("v")) {
temp[i][j] = "^";
}else if(oldChar.equals("\\")) {
temp[i][j] = "/";
} else if(oldChar.equals("/")) {
temp[i][j] = "\\";
} else {
temp[i][j] = oldChar;
}
}
}
return temp;
}
//沿垂直方向翻转
public static String[][] flipVertical(String[][] input) {
int length = input.length;
String[][] temp = new String[length][length];
String oldChar = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
oldChar = input[i][length - 1 - j];
if(oldChar.equals(">")) {
temp[i][j] = "<";
} else if(oldChar.equals("<")) {
temp[i][j] = ">";
} else if(oldChar.equals("\\")) {
temp[i][j] = "/";
} else if(oldChar.equals("/")) {
temp[i][j] = "\\";
} else {
temp[i][j] = oldChar;
}
}
}
return temp;
}
//沿对角线翻转
public static String[][] turnDiagonalt(String[][] input) {
int length = input.length;
String[][] temp = new String[length][length];
String oldChar = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
oldChar = input[j][i];
if(oldChar.equals("<")) {
temp[i][j] = "^";
} else if(oldChar.equals(">")) {
temp[i][j] = "v";
} else if(oldChar.equals("^")) {
temp[i][j] = "<";
} else if(oldChar.equals("v")) {
temp[i][j] = ">";
} else if(oldChar.equals("|")) {
temp[i][j] = "-";
} else if(oldChar.equals("-")) {
temp[i][j] = "|";
} else {
temp[i][j] = oldChar;
}
}
}
return temp;
}
//沿反对角线翻转
public static String[][] turnAntiDiagonalt(String[][] input) {
int length = input.length;
String[][] temp = new String[length][length];
String oldChar = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
oldChar = input[length - 1 - j][length - 1 - i];
if(oldChar.equals("<")) {
temp[i][j] = "v";
} else if(oldChar.equals(">")) {
temp[i][j] = "^";
} else if(oldChar.equals("^")) {
temp[i][j] = ">";
} else if(oldChar.equals("v")) {
temp[i][j] = "<";
} else if(oldChar.equals("|")) {
temp[i][j] = "-";
} else if(oldChar.equals("-")) {
temp[i][j] = "|";
} else {
temp[i][j] = oldChar;
}
}
}
return temp;
}
}
小结 |
本人算法方面薄弱,这里给出的解法可能不是最佳,特别是第三道题,这样实现太过麻烦,而且很冗长。还望有更好解法的大神指点,不胜感激!