Skip to content

Commit 3a4eb62

Browse files
华为机试有一定难度的题
1 parent f2e212c commit 3a4eb62

20 files changed

+1120
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# Build and Release Folders
22
bin-debug/
33
bin-release/
4+
out/
45
[Oo]bj/
56
[Bb]in/
67

78
# Other files and folders
89
.settings/
10+
.idea/
911

1012
# Executables
1113
*.swf
1214
*.air
1315
*.ipa
1416
*.apk
17+
*.iml
1518

1619
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
1720
# should NOT be excluded as they contain compiler settings and other important

huawei/src/main/java/code/HJ102.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main.java.code;
2+
3+
import java.io.*;
4+
5+
public class HJ102 {
6+
/**
7+
* 题目描述
8+
* 输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。
9+
* 本题含有多组样例输入
10+
* 输入描述:
11+
* 一个只包含小写英文字母和数字的字符串。
12+
* 输出描述:
13+
* 一个字符串,为不同字母出现次数的降序表示。若出现次数相同,则按ASCII码的升序输出。
14+
* 示例1
15+
* 输入
16+
* aaddccdc
17+
* 1b1bbbbbbbbb
18+
* 输出
19+
* cda
20+
* b1
21+
* 说明
22+
* 第一个样例里,c和d出现3次,a出现2次,但c的ASCII码比d小,所以先输出c,再输出d,最后输出a.
23+
*/
24+
public static class Main {
25+
public static void main(String[] args) throws IOException {
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
27+
String str;
28+
while ((str = br.readLine()) != null) {
29+
char[] chArr = str.toCharArray();
30+
int[] temp = new int[150];
31+
for (char c : chArr) {
32+
temp[c]++;
33+
}
34+
int max = 0;
35+
for (int i : temp) {
36+
if (max < i) {
37+
max = i;
38+
}
39+
}
40+
StringBuilder sbf = new StringBuilder();
41+
while (max != 0) {
42+
for (int j = 0; j < temp.length; j++) {
43+
if (temp[j] == max) {
44+
sbf.append((char) j);
45+
}
46+
}
47+
max--;
48+
}
49+
System.out.println(sbf);
50+
}
51+
}
52+
}
53+
}

huawei/src/main/java/code/HJ104.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main.java.code;
2+
3+
import java.io.*;
4+
5+
public class HJ104 {
6+
/**
7+
* HJ103 Redraiment的走法
8+
* Redraiment的走法
9+
* 题目描述
10+
* Redraiment是走梅花桩的高手。Redraiment可以选择任意一个起点,从前到后,但只能从低处往高处的桩子走。他希望走的步数最多,你能替Redraiment研究他最多走的步数吗?
11+
* 本题含有多组样例输入
12+
* 输入描述:
13+
* 输入多行,先输入数组的个数,再输入相应个数的整数
14+
* 输出描述:
15+
* 输出结果
16+
* 示例1
17+
* 输入
18+
* 6
19+
* 2 5 1 5 4 5
20+
* 3
21+
* 3 2 1
22+
* 输出
23+
* 3
24+
* 1
25+
* 说明
26+
* 6个点的高度各为 2 5 1 5 4 5
27+
* 如从第1格开始走,最多为3步, 2 4 5
28+
* 从第2格开始走,最多只有1步,5
29+
* 而从第3格开始走最多有3步,1 4 5
30+
* 从第5格开始走最多有2步,4 5
31+
* 所以这个结果是3。
32+
*/
33+
public static class Main {
34+
public static void main(String[] args) throws IOException {
35+
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
36+
String input;
37+
while ((input = read.readLine()) != null) {
38+
int num = Integer.parseInt(input);
39+
String[] strs = read.readLine().split(" ");
40+
int[] nums = new int[num];
41+
int max = 0;
42+
for (int i = 0; i < strs.length; i++) {
43+
nums[i] = Integer.parseInt(strs[i]);
44+
}
45+
int[] result = new int[num];
46+
for (int i = 0; i < nums.length; i++) {
47+
result[i] = 1;
48+
for (int j = 0; j < i; j++) {
49+
if (nums[j] < nums[i]) {
50+
result[i] = Math.max(result[i], result[j] + 1);
51+
}
52+
}
53+
}
54+
max = 1;
55+
for (int j : result) {
56+
if (j > max) {
57+
max = j;
58+
}
59+
}
60+
System.out.println(max);
61+
}
62+
}
63+
}
64+
}

huawei/src/main/java/code/HJ107.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main.java.code;
2+
3+
import java.io.*;
4+
5+
public class HJ107 {
6+
/**
7+
* HJ107 求解立方根
8+
* 求解立方根
9+
* 题目描述
10+
* 计算一个数字的立方根,不使用库函数。
11+
* 保留一位小数。
12+
* 输入描述:
13+
* 待求解参数,为double类型(一个实数)
14+
* 输出描述:
15+
* 输入参数的立方根。保留一位小数。
16+
* 示例1
17+
* 输入
18+
* 216
19+
* 输出
20+
* 6.0
21+
*/
22+
23+
public static class Main {
24+
public static void main(String[] args) throws Exception {
25+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
String str;
27+
while ((str = br.readLine()) != null) {
28+
double d = Double.parseDouble(str);
29+
System.out.println(getCubeRoot(d));
30+
}
31+
}
32+
33+
public static double getCubeRoot(double num) {
34+
boolean f = false;
35+
boolean l = false;
36+
if (num < 0) {
37+
f = true;
38+
num = num * (-1);
39+
}
40+
if (num > 0 && num < 1) {
41+
l = true;
42+
num = 1 / num;
43+
}
44+
double start = 0.0;
45+
double end = num;
46+
double middel = start;
47+
while (start < end) {
48+
middel = (start + end) / 2;
49+
double r = middel * middel * middel;
50+
double diff = Math.abs(r - num);
51+
if (diff < 0.0001) {
52+
break;
53+
} else if (r > num) {
54+
end = middel;
55+
} else {
56+
start = middel;
57+
}
58+
}
59+
if (f) {
60+
middel = middel * -1;
61+
}
62+
if (l) {
63+
middel = 1 / middel;
64+
}
65+
//Math.round对正数四舍五入
66+
return Math.round(middel * 10.0) / 10.0;
67+
}
68+
}
69+
}

huawei/src/main/java/code/HJ16.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main.java.code;
2+
import lombok.Data;
3+
4+
import java.io.*;
5+
public class HJ16 {
6+
public static class Main{
7+
public static int dw = 100;
8+
public static void main(String[] args)throws IOException{
9+
boolean flag = true;
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
String[] firstArr = br.readLine().split(" ");
12+
int N = Integer.parseInt(firstArr[0]);
13+
int m = Integer.parseInt(firstArr[1]);
14+
Good[] A = new Good[m+1];
15+
for(int i = 1;i<=m;i++){
16+
String[] perArr = br.readLine().split(" ");
17+
int v = Integer.parseInt(perArr[0]);
18+
int p = Integer.parseInt(perArr[1]);
19+
int q = Integer.parseInt(perArr[2]);
20+
if(flag)
21+
if(v%dw!=0){
22+
flag = false;
23+
dw = 10;
24+
for(int j = 1;j<i;j++){
25+
A[j].v = A[j].v*dw;
26+
}
27+
}
28+
v=v/dw;
29+
A[i] = new Good(v, p, q);
30+
if(q>0){
31+
if(A[q].a1 == 0){
32+
A[q].a1=i;
33+
}else{
34+
A[q].a2=i;
35+
}
36+
}
37+
}
38+
N=N/dw;
39+
check(N,A);
40+
}
41+
42+
public static void check(int N,Good[] A ){
43+
int[][] dp = new int[A.length][N+1];
44+
for(int i=1;i<A.length;i++){
45+
int v=-1,v1=-1,v2=-1,v3=-1,tmpDp=-1,tmpDp1=-1,tmpDp2=-1,tmpDp3=-1;
46+
v = A[i].v;//主件
47+
tmpDp = v*A[i].p;
48+
if(A[i].a2!=0&&A[i].a1!=0){//主件+附件1+附件2
49+
v3 = v+A[A[i].a2].v+A[A[i].a1].v;
50+
tmpDp3 = tmpDp + A[A[i].a2].v*A[A[i].a2].p+ A[A[i].a1].v*A[A[i].a1].p;
51+
}
52+
if(A[i].a1!=0){//主件+附件1
53+
v1 = v+A[A[i].a1].v;
54+
tmpDp1 = tmpDp + A[A[i].a1].v*A[A[i].a1].p;
55+
}
56+
if(A[i].a2!=0){//主件+附件2
57+
v2 = v+A[A[i].a2].v;
58+
tmpDp2 = tmpDp + A[A[i].a2].v*A[A[i].a2].p;
59+
}
60+
61+
for(int j = i;j<=N;j++){
62+
dp[i][j] = dp[i-1][j];
63+
if(A[i].q==0){//是主件
64+
if(j>=v&&v!=-1)dp[i][j] = Math.max(dp[i][j],dp[i-1][j-v]+tmpDp);
65+
if(j>=v1&&v1!=-1)dp[i][j] = Math.max(dp[i][j],dp[i-1][j-v1]+tmpDp1);
66+
if(j>=v2&&v2!=-1)dp[i][j] = Math.max(dp[i][j],dp[i-1][j-v2]+tmpDp2);
67+
if(j>=v3&&v3!=-1)dp[i][j] = Math.max(dp[i][j],dp[i-1][j-v3]+tmpDp3);
68+
}
69+
}
70+
71+
}
72+
System.out.print(dp[A.length-1][N]*dw);
73+
74+
}
75+
}
76+
@Data
77+
static class Good{
78+
public int v;//价值
79+
public int p;//重要度
80+
public int q;//是否是附件,>0附件,=0主件
81+
public int a1;//附件1编号
82+
public int a2;//附件2编号
83+
84+
public Good(int v,int p,int q){
85+
this.v = v;
86+
this.p = p;
87+
this.q = q;
88+
}
89+
90+
}
91+
}

huawei/src/main/java/code/HJ26.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main.java.code;
2+
3+
import java.io.*;
4+
5+
public class HJ26 {
6+
/**
7+
* HJ26 字符串排序
8+
* 字符串排序
9+
* 题目描述
10+
* 编写一个程序,将输入字符串中的字符按如下规则排序。
11+
* 规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
12+
* 如,输入: Type 输出: epTy
13+
* 规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
14+
* 如,输入: BabA 输出: aABb
15+
* 规则 3 :非英文字母的其它字符保持原来的位置。
16+
* 如,输入: By?e 输出: Be?y
17+
* 注意有多组测试数据,即输入有多行,每一行单独处理(换行符隔开的表示不同行)
18+
* 输入描述:
19+
* 输入字符串
20+
* 输出描述:
21+
* 输出字符串
22+
* 示例1
23+
* 输入
24+
* A Famous Saying: Much Ado About Nothing (2012/8).
25+
* 输出
26+
* A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
27+
*/
28+
29+
public static class Main {
30+
public static void main(String[] args) throws IOException {
31+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
32+
String s;
33+
while ((s = br.readLine()) != null) {
34+
char[] ch = s.toCharArray();
35+
char[] chars = new char[ch.length];
36+
int flag = 65, j = 0;
37+
while (flag <= 90) {
38+
for (char c : ch) {
39+
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
40+
if (c == flag || c == flag + 32) {
41+
chars[j] = c;
42+
j++;
43+
}
44+
}
45+
}
46+
flag++;
47+
}
48+
j = 0;
49+
for (int i = 0; i < ch.length; i++) {
50+
if ((ch[i] >= 65 && ch[i] <= 90) || (ch[i] >= 97 && ch[i] <= 122)) {
51+
ch[i] = chars[j];
52+
j++;
53+
}
54+
}
55+
System.out.println(String.valueOf(ch));
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)