Today, March 17, 2017, Huawei held an intern online programming test, the old look, three questions, a total of 120 minutes, took me 25 minutes to take care of, Huawei programming topic should be very simple.
First question: Enter a string, change the uppercase letters in the string to lowercase letters, the lowercase letters unchanged, the other characters are ignored, and then output the converted results, for example:
Number of cases |
Input |
Output |
Case One |
AbCdEf |
Abcdef |
Case Two |
Aa1bb2 |
Aabb |
Solution:
1. Read the input string str
2. Convert the input string str to a char array arr
3. Each character in an array of arr is judged by uppercase letters, lowercase letters, or other letters
If it is an uppercase letter, it is converted to lowercase by arr[i] + ' A '-' a '
If it is lowercase, you do not need to convert
If it is a different character, it is ignored (not added to the output string)
Import java.util.*;
public class Test1 {public
static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int delta = ' A '-' a ';
while (Sc.hasnext ()) {
String str = Sc.next ();
char[] arr = Str.tochararray ();
StringBuffer buffer = new StringBuffer (arr.length);
for (int i = 0; i < arr.length; i++) {
if (Arr[i] >= ' A ' && arr[i] <= ' z ') {
buffer.append (Arr[i]) ;
} else if (Arr[i] >= ' A ' && arr[i] <= ' z ') {
buffer.append ((char) (Arr[i] +delta));
}
}
System.out.println (Buffer.tostring ());
}
Sc.close ();
}
}
The second question: episode five, the character ' 1 ' represents the collection of the corresponding blessing, for example "10101" means collection of 1th, 3, 5 Blessings, 2nd and 4th blessings not collected. Q: How many five blessings can be formed by the combination of the blessings collected by a few people? For example:
Case |
Input |
Output |
Case One |
10101 01010 11111 00000 |
2 |
Solution: (using the shortest cask principle)
1. Enter the result set and turn it into a character array char[]
2. Set array int[] result = new Int[5], where each value represents the total number of Chenfo
3. Looping through the input result set
If it is ' 1 ', then result[i]++, indicating the corresponding blessing quantity plus one
If it is ' 0 ', then ignore
4. Finally returns the minimum value of the result array
Import Java.util.Scanner;
public class Test2 {public
static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
Int[] result = new Int[5];
while (Sc.hasnext ()) {
String str = Sc.next ();
char[] arr = Str.tochararray ();
for (int i = 0; i < arr.length; i++) {
if (arr[i] = = ' 1 ') {
result[i]++;
}}
}
int min = integer.max_value;
for (int i=0; i<5; i++) {
if (min > Result[i]) {
min = result[i];
}
}
System.out.println (min);
}
}
Third question: Calculate the post-post expression, enter the post-post expression string, and output the result of the expression calculation (where A~f represents 10~15). For example:
Case |
Input |
|
Output |
Case One |
32+5- |
3+2-5 |
0 |
Case Two |
a5-3+ |
10-5+3 |
4 |
Solution: (with Stack)
1. Read the input result string str
2. Convert the input string to a character array char[]
3. Judging each character
If it is a number, then directly into the stack: arr[i]-' 0 '
If it is an uppercase character, it is converted into the stack: arr[i]-' A '
If it is an arithmetic symbol: It pops up two value stacks, then calculates the result based on the symbol and then into the stack
Import Java.util.Scanner;
Import Java.util.Stack;
public class Test3 {public static void main (string[] args) {Scanner sc = new Scanner (system.in);
while (Sc.hasnext ()) {String line = Sc.next ();
SYSTEM.OUT.PRINTLN (Operate (line));
}} public static int operate (String str) {char[] arr = Str.tochararray ();
stack<integer> stack = new stack<integer> ();
for (int i = 0; i < str.length (); i++) {char c = str.charat (i);
Switch (c) {case ' + ': Case '-': Case ' * ': Case '/': Integer right = Stack.pop ();
Integer left = Stack.pop ();
Switch (c) {case ' + ': stack.push (left + right);
Break
Case '-': Stack.push (left-right);
Break
Case ' * ': stack.push (left * right);
Break
Case '/': Stack.push (left/right);
Break
} break;
Default:if (c >= ' a ') {Stack.push (C-' a ' + 10);
} else {Stack.push (C-' 0 ');
} break; } if (i = = Str.length ()-1) {return stack.pop ();
}} return-1; }
}