荣耀8.30笔试题解
第一题将nB转换为KB、MB、GB等
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double n = Integer.parseInt(br.readLine().trim());
int level = 0;
String[] Unit = new String[]{"B", "KB", "MB", "GB"};
while(n >= 1024 && level < 3){
n /= 1024;
level ++;
}
System.out.println(String.format("%.2f", n) + Unit[level]);
}
}
第二题:采花蜜的最近距离,给定五处地点的坐标,返回把五处地点都采集完毕再返回原点的总路程
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
static int[][] location;
static int n;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine().trim();
String[] params = str.split(" ");
n = 5;
location = new int[n][2];
for(int i = 0; i < n; i++){
location[i][0] = Integer.parseInt(params[i * 2]);
location[i][1] = Integer.parseInt(params[i * 2 + 1]);
}
double res = Double.MAX_VALUE;
for(int i = 0; i < n; i++){
int a = location[i][0];
int b = location[i][1];
int dis = (int)Math.sqrt(a * a + b * b);
res = Math.min(res, dfs(0, i) + dis);
}
//这里要四舍五入
System.out.printf("%.0f%n", res);
}
/**
*
* @param mask mask为掩码,第i位为1时表示第i处花丛已经采过
* @param cur 当前所在的花丛
* @return 距离和
*/
private static double dfs(int mask, int cur){
int x = location[cur][0];
int y = location[cur][1];
// 如果所有掩码位都为1,说明5处地点都采过了,返回原点到当前点的距离
if(mask == (1 << n) - 1){
return Math.sqrt(x * x + y * y);
}
double res = Double.MAX_VALUE;
for(int i = 0; i < n; i++){
if(((1 << i) & mask) != 0){
continue;
}
int a = location[i][0] - x;
int b = location[i][1] - y;
double distance = dfs(mask | (1 << i), i) + Math.sqrt(a * a + b * b);
res = Math.min(res, distance);
}
return res;
}
} 第三题 给定两行字符串,一行是标准答案,一行是学生答案,求得分,积分规则:对学生答案中的单词可以进行添加(但总分-2),删除(总分-1),修改(总分-1,且要修改的单词包含对应标准答案的单词中的字母的个数多于后者的一半),得分为在修改规则下将学生答案修改为标准答案所扣除的分数 + 标准答案的单词数import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
static List<String> answer, student;
static int m, n;
static int[][] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().trim().split(" ");
answer = new ArrayList<>();
for (String param : params) {
param = param.trim();
if ("".equals(param)) {
continue;
}
answer.add(param);
}
params = br.readLine().trim().split(" ");
student = new ArrayList<>();
for (String param : params) {
param = param.trim();
//去掉空串
if ("".equals(param)) {
continue;
}
student.add(param);
}
m = answer.size();
n = student.size();
//记忆数组
dp = new int[m][n];
int res = dfs(0, 0);
res += m;
System.out.println(res);
}
private static int dfs(int i, int j){
if(i == m || j == n){
return -1 * (n - j) - 2 * (m - i);
}
if(dp[i][j] != 0){
return dp[i][j];
}
int res = Integer.MIN_VALUE;
if(student.get(j).equals(answer.get(i))){
//正确的
res = Math.max(dfs(i + 1, j + 1), res);
}else if(canReplace(answer.get(i), student.get(j))){
//替换
res = Math.max(dfs(i + 1, j + 1) - 1, res);
}
//删除
res = Math.max(dfs(i, j + 1) - 1, res);
//增加
res = Math.max(dfs(i + 1, j) - 2, res);
dp[i][j] = res;
return res;
}
private static boolean canReplace(String ans, String word){
int n = ans.length();
int[] count = new int[255];
for(int i = 0; i < n; i++){
count[ans.charAt(i)] ++;
}
int same = 0;
for (int i = 0; i < word.length(); i++) {
int c = word.charAt(i);
if(count[c] > 0){
same ++;
count[c] --;
}
}
return same * 2 > n;
}
} 
查看16道真题和解析