更新笔试题目
This commit is contained in:
parent
3962c91939
commit
2c4bf9dc84
@ -1,10 +1,16 @@
|
||||
package com.raorao;
|
||||
|
||||
import afu.org.checkerframework.checker.igj.qual.I;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
@ -36,7 +42,10 @@ public class Test {
|
||||
ss.add(1);
|
||||
ArrayBlockingQueue<Integer> s;
|
||||
LinkedBlockingQueue<Integer> a;
|
||||
|
||||
FileInputStream fis;
|
||||
FileOutputStream fos;
|
||||
InputStreamReader isr;
|
||||
OutputStreamWriter osw;
|
||||
}
|
||||
|
||||
public int foo() {
|
||||
|
59
code/src/main/java/com/raorao/interview/huawei/Q1.java
Normal file
59
code/src/main/java/com/raorao/interview/huawei/Q1.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.raorao.interview.huawei;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 汽水瓶.
|
||||
*
|
||||
* 题目描述:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?
|
||||
*
|
||||
* 答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。
|
||||
* 然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-9:52
|
||||
*/
|
||||
public class Q1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (scanner.hasNextInt()) {
|
||||
int input = Integer.parseInt(scanner.nextLine());
|
||||
if (input != 0) {
|
||||
list.add(input);
|
||||
} else {
|
||||
process(list);
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void process(List<Integer> list) {
|
||||
if(list.size() == 0){
|
||||
System.out.println(0);
|
||||
}
|
||||
list.forEach((e) -> System.out.println(process(e)));
|
||||
}
|
||||
|
||||
public static int process(int input) {
|
||||
if (input < 3) {
|
||||
return 0;
|
||||
}
|
||||
int q = input / 3;
|
||||
int r = input % 3;
|
||||
int count = 0;
|
||||
while (q > 0) {
|
||||
count += q;
|
||||
int temp = q + r;
|
||||
q = temp / 3;
|
||||
r = temp % 3;
|
||||
}
|
||||
if (r == 2) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
48
code/src/main/java/com/raorao/interview/huawei/Q2.java
Normal file
48
code/src/main/java/com/raorao/interview/huawei/Q2.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.raorao.interview.huawei;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 数字去重.
|
||||
*
|
||||
* 题目描述:明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。
|
||||
* 请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。
|
||||
*
|
||||
* 输入描述: 输入多行,先输入随机整数的个数,再输入相应个数的整数
|
||||
*
|
||||
* 输出描述: 返回多行,处理后的结果
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-10:23
|
||||
*/
|
||||
public class Q2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (scanner.hasNext()) {
|
||||
int count = scanner.nextInt();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (scanner.hasNextInt()) {
|
||||
list.add(scanner.nextInt());
|
||||
}
|
||||
}
|
||||
process(list);
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private static void process(List<Integer> list) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
List<Integer> tempList = new ArrayList<>();
|
||||
list.forEach(set::add);
|
||||
set.forEach((e) -> tempList.add(e));
|
||||
Collections.sort(tempList);
|
||||
tempList.forEach(System.out::println);
|
||||
}
|
||||
}
|
31
code/src/main/java/com/raorao/interview/huawei/Q3.java
Normal file
31
code/src/main/java/com/raorao/interview/huawei/Q3.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.raorao.interview.huawei;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 数制转换.
|
||||
*
|
||||
* 问题描述:输入一个16进制的字符串,输出对应的10进制的字符串
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-10:44
|
||||
*/
|
||||
public class Q3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String a = "0xA";
|
||||
System.out.println(process(a));
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNextLine()) {
|
||||
System.out.println(process(scanner.nextLine()));
|
||||
}
|
||||
}
|
||||
|
||||
public static String process(String ss) {
|
||||
if (ss.startsWith("0x")) {
|
||||
ss = ss.substring(2);
|
||||
}
|
||||
int a = Integer.parseInt(ss, 16);
|
||||
return String.valueOf(a);
|
||||
}
|
||||
}
|
40
code/src/main/java/com/raorao/interview/huawei/T1/Main.java
Normal file
40
code/src/main/java/com/raorao/interview/huawei/T1/Main.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.raorao.interview.huawei.T1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 题目要求:输入任意个字符串,将其中的小写字母变为大写,大写字母变为小写,其他字符不用处理.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-19:01
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNext()) {
|
||||
String ss = scanner.nextLine();
|
||||
process(ss);
|
||||
}
|
||||
}
|
||||
|
||||
private static void process(String str) {
|
||||
char[] arr = str.toCharArray();
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
arr[i] = tranfer(arr[i]);
|
||||
}
|
||||
System.out.println(new String(arr));
|
||||
}
|
||||
|
||||
private static char tranfer(char c) {
|
||||
int t = (int) c;
|
||||
if (t >= 65 && t <= 90) {
|
||||
t = t + 32;
|
||||
}else if (t >= 97 && t <= 122) {
|
||||
t = t - 32;
|
||||
}
|
||||
return (char) t;
|
||||
}
|
||||
|
||||
|
||||
}
|
56
code/src/main/java/com/raorao/interview/huawei/T2/Main.java
Normal file
56
code/src/main/java/com/raorao/interview/huawei/T2/Main.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.raorao.interview.huawei.T2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 题目描述:小偷来到了一个神秘的王宫,突然眼前一亮,发现5个宝贝,每个宝贝的价值都不一样,且重量也不一样,但是小偷的背包携带重量 有限,所以他不得不在宝贝中做出选择,才能使偷到的财富最大,请你帮助小偷计算一下。.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-19:15
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNext()) {
|
||||
String line1 = scanner.nextLine();
|
||||
String line2 = scanner.nextLine();
|
||||
String[] s1 = line1.split(",");
|
||||
String[] s2 = line2.split(",");
|
||||
int[] weights = new int[s1.length];
|
||||
int[] values = new int[s2.length];
|
||||
for (int i = 0; i < s1.length; i++) {
|
||||
weights[i] = Integer.parseInt(s1[i]);
|
||||
}
|
||||
for (int i = 0; i < s2.length; i++) {
|
||||
values[i] = Integer.parseInt(s2[i]);
|
||||
}
|
||||
int capacity = Integer.parseInt(scanner.nextLine());
|
||||
System.out.println(process(values, weights, capacity));
|
||||
}
|
||||
}
|
||||
|
||||
public static int process(int[] weight, int[] value, int capacity) {
|
||||
int n = weight.length;
|
||||
int[][] maxvalue = new int[n + 1][capacity + 1];
|
||||
|
||||
for (int i = 0; i < capacity + 1; i++) {
|
||||
maxvalue[0][i] = 0;
|
||||
}
|
||||
for (int i = 0; i < n + 1; i++) {
|
||||
maxvalue[i][0] = 0;
|
||||
}
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= capacity; j++) {
|
||||
maxvalue[i][j] = maxvalue[i - 1][j];
|
||||
if (weight[i - 1] <= j) {
|
||||
if (maxvalue[i - 1][j - weight[i - 1]] + value[i - 1] > maxvalue[i - 1][j]) {
|
||||
maxvalue[i][j] = maxvalue[i - 1][j - weight[i - 1]] + value[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxvalue[n][capacity];
|
||||
}
|
||||
|
||||
}
|
78
code/src/main/java/com/raorao/interview/huawei/T3/Main.java
Normal file
78
code/src/main/java/com/raorao/interview/huawei/T3/Main.java
Normal file
@ -0,0 +1,78 @@
|
||||
package com.raorao.interview.huawei.T3;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-19:23
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNext()) {
|
||||
String line1 = scanner.nextLine();
|
||||
String line2 = scanner.nextLine();
|
||||
process(line1, line2);
|
||||
}
|
||||
}
|
||||
|
||||
private static void process(String string, String target) {
|
||||
String[] types = string.split(";");
|
||||
int pointNum = 0;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
Map<String, Integer> map2 = new HashMap<>();
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
String[] items = types[i].trim().split(" ");
|
||||
String pattern = "([a-zA-Z]+)\\**";
|
||||
Pattern p = Pattern.compile(pattern);
|
||||
Matcher matcher = p.matcher(items[1]);
|
||||
// 检查关键字错误
|
||||
if (!items[0].equals("typedef")) {
|
||||
System.out.println("none");
|
||||
return;
|
||||
}
|
||||
//检查重定义
|
||||
if(map.containsKey(items[2])){
|
||||
System.out.println("none");
|
||||
}
|
||||
if (matcher.find()) {
|
||||
String type = matcher.group(1);
|
||||
// 检查类型
|
||||
if (map.isEmpty() || map.containsKey(type)) {
|
||||
map.put(items[2], matcher.group(1));
|
||||
} else {
|
||||
System.out.println("none");
|
||||
return;
|
||||
}
|
||||
}
|
||||
pointNum += items[1].length() - matcher.group(1).length();
|
||||
map2.put(items[2], pointNum);
|
||||
}
|
||||
|
||||
// 判断目标类型是否存在
|
||||
if (!map.containsKey(target)) {
|
||||
System.out.println("none");
|
||||
return;
|
||||
}
|
||||
|
||||
// 星号个数
|
||||
int num = map2.get(target);
|
||||
//打印真实类型
|
||||
while (map.containsKey(target)) {
|
||||
target = map.get(target);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(target).append(" ");
|
||||
for (int i = 0; i < num; i++) {
|
||||
sb.append("*").append(" ");
|
||||
}
|
||||
System.out.println(sb.toString().trim());
|
||||
}
|
||||
}
|
76
code/src/main/java/com/raorao/interview/netease/Q1.java
Normal file
76
code/src/main/java/com/raorao/interview/netease/Q1.java
Normal file
@ -0,0 +1,76 @@
|
||||
package com.raorao.interview.netease;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Clock.
|
||||
*
|
||||
* link: https://www.nowcoder.com/question/next?pid=11647029&qid=117516&tid=17351209
|
||||
*
|
||||
* 修改不满足时钟的条件,得到最小的值
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-10:56
|
||||
*/
|
||||
public class Q1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNext()) {
|
||||
int count = scanner.nextInt();
|
||||
String[] arr = new String[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
arr[i] = scanner.next();
|
||||
}
|
||||
process(arr);
|
||||
}
|
||||
}
|
||||
|
||||
private static void process(String[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
process(arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean[] isLeagle(String str) {
|
||||
String[] items = str.split(":");
|
||||
boolean[] res = new boolean[] {true, true, true};
|
||||
if (Integer.parseInt(items[0]) > 23) {
|
||||
res[0] = false;
|
||||
}
|
||||
if (Integer.parseInt(items[1]) > 59) {
|
||||
res[1] = false;
|
||||
}
|
||||
if (Integer.parseInt(items[2]) > 59) {
|
||||
res[2] = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private static void process(String str) {
|
||||
boolean[] judges = isLeagle(str);
|
||||
String[] items = str.split(":");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (judges[0]) {
|
||||
sb.append(items[0]).append(":");
|
||||
} else {
|
||||
sb.append("0").append(items[0].substring(1)).append(":");
|
||||
}
|
||||
if (judges[1]) {
|
||||
sb.append(items[1]).append(":");
|
||||
} else {
|
||||
sb.append("0").append(items[1].substring(1)).append(":");
|
||||
}
|
||||
if (judges[2]) {
|
||||
sb.append(items[2]);
|
||||
} else {
|
||||
sb.append("0").append(items[2].substring(1));
|
||||
}
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
65
code/src/main/java/com/raorao/interview/netease/Q2.java
Normal file
65
code/src/main/java/com/raorao/interview/netease/Q2.java
Normal file
@ -0,0 +1,65 @@
|
||||
package com.raorao.interview.netease;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* 会话列表问题.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-11:55
|
||||
*/
|
||||
public class Q2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNext()) {
|
||||
int group = Integer.parseInt(scanner.nextLine());
|
||||
String[] res = new String[group];
|
||||
for (int i = 0; i < group; i++) {
|
||||
int size = Integer.parseInt(scanner.nextLine());
|
||||
String[] ss = scanner.nextLine().split(" ");
|
||||
int[] arr = new int[size];
|
||||
for (int j = 0; j < size; j++) {
|
||||
arr[j] = Integer.parseInt(ss[j]);
|
||||
}
|
||||
res[i] = process(arr);
|
||||
}
|
||||
for (int i = 0; i < group; i++) {
|
||||
System.out.println(res[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String process(int[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return null;
|
||||
}
|
||||
int length = arr.length;
|
||||
Stack<Integer> s1 = new Stack<>();
|
||||
Stack<Integer> s2 = new Stack<>();
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (!s1.isEmpty() && arr[i] == s1.peek()) {
|
||||
continue;
|
||||
}
|
||||
if (s1.contains(arr[i])) {
|
||||
Integer t;
|
||||
while ((t = s1.pop()) != arr[i]) {
|
||||
s2.push(t);
|
||||
}
|
||||
while (!s2.isEmpty()) {
|
||||
s1.push(s2.pop());
|
||||
}
|
||||
s1.push(t);
|
||||
} else {
|
||||
s1.push(arr[i]);
|
||||
}
|
||||
}
|
||||
int size = s1.size();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < size; i++) {
|
||||
sb.append(s1.pop()).append(" ");
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
}
|
90
code/src/main/java/com/raorao/interview/netease/Q3.java
Normal file
90
code/src/main/java/com/raorao/interview/netease/Q3.java
Normal file
@ -0,0 +1,90 @@
|
||||
package com.raorao.interview.netease;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 字符迷阵.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-16:29
|
||||
*/
|
||||
public class Q3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Word[] words = read();
|
||||
for (Word word : words) {
|
||||
String target = word.val;
|
||||
int sum = 0;
|
||||
char[] tc = target.toCharArray();
|
||||
for (int i = 0; i < word.row; i++) {
|
||||
for (int j = 0; j < word.col; j++) {
|
||||
if (word.matrix[i][j] == tc[0]) {
|
||||
sum += count(word, i, j, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(sum);
|
||||
}
|
||||
}
|
||||
|
||||
private static int count(Word word, int i, int j, String target) {
|
||||
int count = 0;
|
||||
int length = target.length();
|
||||
// 右边
|
||||
if (j + length <= word.col) {
|
||||
String temp = new String(word.matrix[i], j, length);
|
||||
if (temp.equals(target)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
// 下面
|
||||
if (i + length <= word.row) {
|
||||
char[] temp = new char[length];
|
||||
for (int k = 0; k < length; k++) {
|
||||
temp[k] = word.matrix[i + k][j];
|
||||
}
|
||||
if (target.equals(new String(temp))) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// 右下斜线
|
||||
if (j + length <= word.col && i + length <= word.row) {
|
||||
char[] temp = new char[length];
|
||||
for (int k = 0; k < length; k++) {
|
||||
temp[k] = word.matrix[i + k][j + k];
|
||||
}
|
||||
if (target.equals(new String(temp))) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static Word[] read() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int count = scanner.nextInt();
|
||||
Word[] res = new Word[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
int m = scanner.nextInt();
|
||||
int n = scanner.nextInt();
|
||||
res[i] = new Word();
|
||||
res[i].matrix = new char[m][n];
|
||||
res[i].row = m;
|
||||
res[i].col = n;
|
||||
for (int j = 0; j < m; j++) {
|
||||
res[i].matrix[j] = scanner.next().toCharArray();
|
||||
}
|
||||
res[i].val = scanner.next();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private static class Word {
|
||||
|
||||
int row;
|
||||
int col;
|
||||
String val;
|
||||
char[][] matrix;
|
||||
}
|
||||
}
|
14
code/src/main/java/com/raorao/leetcode/Q2.java
Normal file
14
code/src/main/java/com/raorao/leetcode/Q2.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.raorao.leetcode;
|
||||
|
||||
/**
|
||||
* 逆波兰表示法.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-08-8:29
|
||||
*/
|
||||
public class Q2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user