Skip to content

Commit c9a6723

Browse files
Default Changelist
1 parent d4b04bb commit c9a6723

8 files changed

+2975
-3230
lines changed

huawei/src/main/java/niuke_huawei_jishi/markdown/牛客网-华为机试练习题 01.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
##### 题目描述
44

5-
计算字符串最后一个单词的长度,单词以空格隔开。
5+
计算字符串最后一个单词的长度,单词以空格隔开。
66

77
##### 输入描述:
88

@@ -29,6 +29,7 @@ hello world
2929
```
3030
5
3131
```
32+
3233
#### 思路:
3334

3435
字符串切片就好
@@ -37,17 +38,17 @@ hello world
3738

3839
#### 解决代码
3940

40-
```
41+
```java
4142
import java.util.Scanner;
43+
4244
public class Main {
43-
4445
public static void main(String[] args) {
4546
Scanner input = new Scanner(System.in);
46-
String s="";
47-
while(input.hasNextLine()){
48-
s=input.nextLine();
49-
System.out.println(s.length()-1-s.lastIndexOf(" "));
50-
}
47+
String s = "";
48+
while (input.hasNextLine()) {
49+
s = input.nextLine();
50+
System.out.println(s.length() - 1 - s.lastIndexOf(" "));
51+
}
5152
}
5253
}
5354
```

huawei/src/main/java/niuke_huawei_jishi/markdown/牛客网-华为机试练习题 02.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#### 牛客网-华为机试练习题 02 计算字符个数
22

3-
#### 题目描述
3+
#### 题目描述
44

55
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
66

@@ -39,38 +39,31 @@ A
3939

4040
```java
4141
import java.util.Scanner;
42-
public class Main{
43-
public static void main(String[] args)
44-
{
45-
int count = 0;
46-
Scanner sc=new Scanner(System.in);
47-
String str = sc.nextLine();
48-
String br = sc.nextLine();
49-
50-
String string1=str.toUpperCase();
51-
String br1=br.toUpperCase();
52-
53-
char a = br1.charAt(0);
54-
55-
for(int i=0;i<string1.length();i++)
56-
{
57-
if(string1.charAt(i)==a)
58-
count++;
59-
}
60-
System.out.println(count);
61-
42+
43+
public class Main {
44+
public static void main(String[] args) {
45+
int count = 0;
46+
Scanner sc = new Scanner(System.in);
47+
String str = sc.nextLine();
48+
String br = sc.nextLine();
49+
50+
String string1 = str.toUpperCase();
51+
String br1 = br.toUpperCase();
52+
char a = br1.charAt(0);
53+
for (int i = 0; i < string1.length(); i++) {
54+
if (string1.charAt(i) == a)
55+
count++;
6256
}
57+
System.out.println(count);
58+
}
6359
}
6460
```
6561

66-
67-
6862
#### 总结:
6963

7064
* 如果需要导入,那么首先导入java.util.Scanner库,然后实例化,Scanner sc = new Scanner(System.in);
7165
* public class Main后面不需要加小括号,
7266
* 字符串转大写使用toUpperCase()方法
7367
* 字符串按照序号寻找字符使用charAt()方法
7468
* 如果报下列错误,检测main函数是否出现问题,main全部小写
75-
* 请检查是否存在数组越界等非法访问情况
76-
case通过率为0.00%
69+
* 请检查是否存在数组越界等非法访问情况 case通过率为0.00%

huawei/src/main/java/niuke_huawei_jishi/markdown/牛客网-华为机试练习题 03.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ OutputArray 输出处理后的随机整数
5656
import java.util.Iterator;
5757
import java.util.Scanner;
5858
import java.util.TreeSet;
59-
59+
6060
public class Main {
61-
public static void main(String[] args) {
62-
Scanner str = new Scanner(System.in);
63-
while(str.hasNextInt()){
64-
int cn = str.nextInt();
65-
TreeSet<Integer> ts = new TreeSet<Integer>();
66-
while(cn>0 && str.hasNextInt()){
67-
ts.add(str.nextInt());
68-
}
69-
Iterator<Integer> it = ts.iterator();
70-
while(it.hasNext()){
71-
System.out.println(it.next());
72-
}
73-
}
61+
public static void main(String[] args) {
62+
Scanner str = new Scanner(System.in);
63+
while (str.hasNextInt()) {
64+
int cn = str.nextInt();
65+
TreeSet<Integer> ts = new TreeSet<>();
66+
while (cn > 0 && str.hasNextInt()) {
67+
ts.add(str.nextInt());
68+
}
69+
Iterator<Integer> it = ts.iterator();
70+
while (it.hasNext()) {
71+
System.out.println(it.next());
72+
}
7473
}
74+
}
7575
}
7676
```
7777
#### 总结:

huawei/src/main/java/niuke_huawei_jishi/markdown/牛客网-华为机试练习题 04.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,31 @@ abc00000
3838

3939
#### 解决代码
4040

41-
```
41+
```java
4242
import java.util.Scanner;
43-
43+
4444
public class Main {
45-
45+
4646
public static void main(String[] args) {
47-
Scanner scanner=new Scanner(System.in);
48-
while(scanner.hasNextLine()){
49-
String s=scanner.nextLine();
47+
Scanner scanner = new Scanner(System.in);
48+
while (scanner.hasNextLine()) {
49+
String s = scanner.nextLine();
5050
split(s);
5151
}
5252
}
53-
54-
public static void split(String s){
55-
while(s.length()>=8){
53+
54+
public static void split(String s) {
55+
while (s.length() >= 8) {
5656
System.out.println(s.substring(0, 8));
57-
s=s.substring(8);
57+
s = s.substring(8);
5858
}
59-
if(s.length()<8&&s.length()>0){
60-
s=s+"00000000";
59+
if (s.length() > 0) {
60+
s = s + "00000000";
6161
System.out.println(s.substring(0, 8));
6262
}
6363
}
64-
65-
64+
65+
6666
}
6767
```
6868

huawei/src/main/java/niuke_huawei_jishi/markdown/牛客网-华为机试练习题 05.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
10
3131
3232
```
33+
3334
#### 思路
3435

3536
* 从后往前遍历
@@ -41,38 +42,38 @@
4142
```java
4243
import java.lang.Math;
4344
import java.util.Scanner;
44-
public class Main{
45-
public static void main(String[] args){
45+
46+
public class Main {
47+
public static void main(String[] args) {
4648
Scanner sc = new Scanner(System.in);
47-
48-
while(sc.hasNext()){
49+
50+
while (sc.hasNext()) {
4951
String str = sc.nextLine();
5052
System.out.println(fun(str.substring(2)));
5153
}
5254
}
53-
54-
public static int fun(String s){
55-
int n=0;
56-
int count= 0;
55+
56+
public static int fun(String s) {
57+
int n = 0;
58+
int count = 0;
5759
int temp = 0;
5860
char ch;
59-
60-
while(count<s.length())
61-
{
62-
ch = s.charAt(s.length()-count-1);
63-
if(ch>='0'&&ch<='9'){
64-
temp = ch-'0';
65-
}else if(ch>='A'&&ch<='Z'){
66-
temp = ch-'A'+10;
67-
}else if(ch>='a'&&ch<='z'){
68-
temp = ch-'a'+10;
69-
}else{
61+
62+
while (count < s.length()) {
63+
ch = s.charAt(s.length() - count - 1);
64+
if (ch >= '0' && ch <= '9') {
65+
temp = ch - '0';
66+
} else if (ch >= 'A' && ch <= 'Z') {
67+
temp = ch - 'A' + 10;
68+
} else if (ch >= 'a' && ch <= 'z') {
69+
temp = ch - 'a' + 10;
70+
} else {
7071
break;
7172
}
72-
n += temp*Math.pow(16,count);
73+
n += temp * Math.pow(16, count);
7374
count++;
7475
}
75-
76+
7677
return n;
7778
}
7879
}

huawei/src/main/java/niuke_huawei_jishi/markdown/牛客网-华为机试练习题 06.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
#### 题目描述
44

5-
功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
6-
最后一个数后面也要有空格
5+
功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 ) 最后一个数后面也要有空格
76

87
```
98
详细描述:
@@ -20,6 +19,7 @@ long ulDataInput:输入的正整数
2019
2120
String
2221
```
22+
2323
#### 输入描述:
2424

2525
```
@@ -41,6 +41,7 @@ String
4141
4242
2 2 3 3 5
4343
```
44+
4445
#### 思路:
4546

4647
使用一个双重循环解决。
@@ -58,26 +59,27 @@ pnum从2开始不断递增
5859
```java
5960

6061
import java.util.*;
62+
6163
public class Main {
6264
public static void main(String[] args) {
6365
Scanner str = new Scanner(System.in);
6466
long num = str.nextLong();
6567
String result = getResult(num);
6668
System.out.println(result);
6769
}
68-
69-
public static String getResult(long num){
70-
int pum = 2;
71-
String result = "";
72-
while(num!=1){
73-
while(num%pum==0){
74-
num=num/pum;
75-
result = result + pum+" ";
70+
71+
public static String getResult(long num) {
72+
int pum = 2;
73+
String result = "";
74+
while (num != 1) {
75+
while (num % pum == 0) {
76+
num = num / pum;
77+
result = result + pum + " ";
78+
}
79+
pum++;
7680
}
77-
pum++;
81+
return result;
7882
}
79-
return result;
80-
}
8183
}
8284

8385
```

0 commit comments

Comments
 (0)