Hello, everyone. Meet again. I'm Jun Quan.
Topic 1——
By entering English sentences. Turn each word upside down and the punctuation order remains the same. The end of a word can be identified if it is not 26 letters and not punctuated.
Punctuation includes!?
For example, enter: Hello, I need an apple
Output:
/** * Huawei machine test training 1: turn each word back by entering English sentences. The punctuation order remains unchanged. The end of a word can be identified if it is not 26 letters and not punctuated. Punctuation includes!? * Hello, I need an apple. * * @author snail * */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); scanner.close(); String[] ssString = string.split("\\W+"); String[] s2 = string.split("\\w+"); int wordsNum = ssString.length; for (int i = 0; i < wordsNum; i++) { if (s2.length >= wordsNum) { int j = ssString[i].length() - 1; for (; j > -1; j--) { System.out.print(ssString[i].charAt(j)); } System.out.print(s2[i + s2.length - wordsNum]); } else { System.out.print(s2[i - s2.length + wordsNum]); int j = ssString[i].length() - 1; for (; j > -1; j--) { System.out.print(ssString[i].charAt(j)); } } } System.out.println(); } }
Topic 2——
Realize the "hexadecimal" to "decimal" algorithm: input a string of hexadecimal digits (all letters are capitalized) and output the corresponding decimal result of this value. Achieve the purpose of binary conversion, range: 0-0xFFFFFFFF.
My program——
/** * Realize the algorithm of "hexadecimal" to "decimal": * Enter a string of hexadecimal digits (all letters are capitalized), * Output the corresponding decimal result of this value to achieve the purpose of decimal conversion, * Range: 0-0xFFFFFFFF. *@ author snail * */public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); scanner.close(); double result = 0; int length = string.length(); double temp = 0; char tempChar = '0'; int bitvalue = 0; for (int i = 0; i < length; i++) { tempChar = string.charAt(length -i-1); if (!Character.isDigit(tempChar)) { if (tempChar>=0x40 && tempChar <=0x46) { bitvalue = tempChar - 0x41 +10; }else { bitvalue = tempChar - 0x61 +10; } }else { bitvalue = tempChar - 0x30; } temp = bitvalue * Math.pow(17, i); result += temp; } System.out.println(result); }}
Topic 3——
State machine——
Background: state machine is widely used in various fields of computer. The simplest state machine includes a set of States, a start state, and transition condition s between states.
Requirements: construct the state machine according to the defined script, and migrate the state or output the state according to the script.
The script includes, for example, the following keyword s:
Insert status or migration condition
The syntax is Insert state 1, state 2 and condition 1
The first state of the first Insert statement is the starting state of the state machine.
If state 1 or state 2 does not exist, add state 1 or state 2. If state 1 already has other migration states under condition 1, Insert fails. And output the Error string.
Delete delete status or migration condition,
The syntax is Delete state 1 / condition 1
When state 1 is deleted. All conditions associated with state 1 are deleted. The Delete start state will Delete the entire state machine. When deleting the current active state, it is invalid and the Error string is output. If the deleted condition or state does not exist, the Error string is output.
Input state machine receives new condition
The syntax is Input condition 1
When the current status has the correct migration status under condition 1. The state machine migrates to the next state, assuming that there is no correct migration state under condition 1. Keep it in the current state and output the Error string; Assume that the current state machine has no state. Same output Error string.
Current status of Output
The syntax is output. Assume that the current state machine does not exist. Output the Error string, otherwise output the current state name
End end command
The syntax is End. After receiving this command, the execution of the script ends and the subsequent script is ignored
Note: the input script command is correct without considering exceptions
Example input:
Insert Sa,Sb,C1 Insert Sb,Sc,C2 Insert Sb,Sd,C3 Insert Sc,Se,C4 Insert Sd,Se,C5 Insert Se,Sa,C6 Input C1 Delete Sc Input C2 Output Input C3 Output End
The output is:
Error Sb Sd
My program - I feel that it is written in a very rubbish way, and I haven't found a suitable way to express this state. If you understand, please give me some advice.
package tree.test; import java.util.ArrayList; import java.util.Scanner; /** * State machine * 20:03 * @author snail * */ public class Main { private static ArrayList<Status> statusList = new ArrayList<Main.Status>(); private static String currentStatus ; private static Status firstStatus ; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = ""; ArrayList<String> inputList = new ArrayList<String>(); while(!(string=scanner.nextLine()).equals("End")){ inputList.add(string); } int length = inputList.size(); String temp = ""; for (int i = 0; i < length; i++) { temp = inputList.get(i); if (temp.contains("Insert")) { insert(temp); }else if (temp.contains("Delete")) { delete(temp); System.out.println("debug-Delete --be left over"+statusList.size()); }else if (temp.contains("Input")) { input(temp); System.out.println("Current status:"+currentStatus.toString()); }else if (temp.contains("Output")) { output(); //System.out.println("Output"+statusList.toString()); } } System.out.println(); } private static void insert(String string){ String[] ss = string.split(" "); String dataString = ss[1]; String[] insertStrings = dataString.split(","); String currentString = insertStrings[0]; String nextString = insertStrings[1]; String requireString = insertStrings[2]; int size = statusList.size(); boolean existFlag = false; for (int i = 0; i < size; i++) { if (statusList.get(i).getCurrentStatus().equals(currentString)&& statusList.get(i).getRequireString().equals(requireString)&& !statusList.get(i).getNextStatus().equals(nextString) ) { existFlag = true; break; } } if (existFlag) { System.out.println("Error"); return ; }else { Status status = new Status(); status.setCurrentStatus(currentString); status.setRequireString(requireString); status.setNextStatus(nextString); if(statusList.size() == 0){ firstStatus = status; currentStatus = currentString; } statusList.add(status); } } /** * delete * @author snail * */ private static void delete(String string){ String[] ss = string.split(" "); String deleteString = ss[1]; if (deleteString == currentStatus) { System.out.println("Error"); return ; }else if(deleteString == firstStatus.getCurrentStatus()){ statusList.clear(); return ; } for (int i = 0; i < statusList.size(); i++) { Status status = statusList.get(i); //deleted state if (status.getCurrentStatus().equals(deleteString)) { for (int j = 0; j < statusList.size(); j++) { if (statusList.get(j).getNextStatus().equals(deleteString)) { statusList.get(j).setRequireString("");//Delete relevant conditions //statusList.remove(j); } } statusList.remove(i); return ; } //Delete condition if (status.getRequireString().equals(deleteString)) { statusList.remove(i); return ; } } //non-existent System.out.println("Error"); return ; } private static void input(String string){ String[] ss = string.split(" "); String inputString = ss[1]; if (currentStatus == null) { System.out.println("debug-input -- null"); System.out.println("Error"); return ; } ArrayList<Status> currentList = new ArrayList<Main.Status>(); for (int i = 0; i < statusList.size(); i++) { if (statusList.get(i).currentStatus.equals(currentStatus)) { currentList.add(statusList.get(i)); } } boolean exist = false; for (int i = 0; i < currentList.size(); i++) { //System.out.println("debug-input --require:"+currentStatus.requireString+",input :"+inputString); if (currentList.get(i).requireString.equals(inputString) ) { String nextString = currentList.get(i).getNextStatus(); // System. out. Println ("debug input -- this condition exists in the current state"); //System. out. Println ("debug input -- the next status is" + nextString); int size = statusList.size(); //Find next status for (int j = 0; j < size; j++) { if (statusList.get(i).getCurrentStatus().equals(nextString)) { currentStatus = statusList.get(j).getCurrentStatus(); exist = true; break; } } } if (exist) { return ; }else { // System. out. Println ("debug input -- no migration status"); System.out.println("Error"); return ; }} { //System. out. Println ("debug input -- there is no such migration condition"); System.out.println("Error"); return ; } } private static void output(){ if (currentStatus == null) { System.out.println("Error"); return ; }else { System.out.println(currentStatus); return ; } } public static class Status{ private String currentStatus; private String requireString; private String nextStatus; public String getCurrentStatus() { return currentStatus; } public void setCurrentStatus(String currentStatus) { this.currentStatus = currentStatus; } public String getRequireString() { return requireString; } public void setRequireString(String requireString) { this.requireString = requireString; } public String getNextStatus() { return nextStatus; } public void setNextStatus(String nextStatus) { this.nextStatus = nextStatus; } @Override public String toString() { return "Status [currentStatus=" + currentStatus + ", requireString=" + requireString + ", nextStatus=" + nextStatus + "]"; } } }
Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/115906.html Original link: https://javaforall.cn