Huawei OJ Machine Test training (I.)

Source: Internet
Author: User

Title

By entering English sentences. Each word is reversed and the punctuation order is the same. It is possible to identify the end of a word without a 26-letter and non-punctuation mark.

Punctuation contains,.!?

For example input: Hello, I need an apple.

Output:

/** * Huawei Machine Test 1: By inputting English sentences, each word is reversed. The punctuation order is the same. It is possible to identify the end of a word without a 26-letter and non-punctuation mark. Punctuation contains,.!? * Hello, I need an apple. *  * @author snail *  */public class Main {public static void main (string[] args) {Scanner Scanner = new Scanner (Sys tem.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.P Rint (Ssstring[i].charat (j));}} System.out.println ();}}

Topic 2--

Implement the "17-in" to "decimal" algorithm: Enter a 17-digit string (uppercase letters), output the corresponding decimal result of this value. To achieve the purpose of conversion, Range: 0-0xffffffff.


My program--

/** * Implementation of "17 binary" to "decimal" algorithm: * Enter a 17-digit string (all uppercase letters), * output this value corresponding decimal result, to achieve the conversion purposes, * 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 ();d ouble result = 0;int length = string.length ();d ouble temp = 0;char tem PChar = ' 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 (+, i); result + = temp;} SYSTEM.OUT.PRINTLN (result);}}


Topic 3--

State machine--

background: The state machine is widely used in various fields of computer. The simplest state machine consists of a set of state sets (states), a starting state (start ), transition conditions between States (transition Condition).

requirements: The state machine is constructed according to the defined script, and the state is migrated according to the script, or the state is output.

scripts include such as the following keyword :  

Insert Insert State or migration condition

syntax is insert Status 1, State 2, condition 1

The first state of the first INSERT statement defaults to the state machine's start.

If state 1 or state 2 does not exist, then new State 1 or State 2 is added, and if status 1 already exists in the other migration state under condition 1, insert fails. and output the error string.

Delete Delete a state or migration condition,

syntax is delete Status 1/ condition 1

When you delete state 1 o'clock. All conditions associated with state 1 are deleted, the delete start state deletes the entire state machine, deletes the current active state, invalidates and outputs the error string, removes the condition or state that does not exist, and outputs the error string.  

Input state machine receives new condition

syntax is Input conditions 1

When the current state 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. Remains in the current state and outputs an error string, assuming that the current state machine has no state. The same output error string.

Output Output current status

The syntax is output. Assume that the current state machine does not exist. Output the error string, otherwise the current state name is output.

end ending command

The syntax is end. Receive this command, script execution ends, ignoring subsequent scripts

Description: Input script command is correct, do not consider abnormal situation

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--feeling written very rubbish, did not find a suitable way to represent this state. Understand the friend, please advise.

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> ();p rivate static String Curre NtStatus; private static Status firststatus;p ublic static void Main (string[] args) {Scanner Scanner = new Scanner (system.in); String string = ""; arraylist<string> inputlist = new arraylist<string> (); 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-left" +statuslist.size ()); else if (temp.contains ("input")) {input (temp); System.out.println ("Current state:" +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 (""); Stringdeletestring = 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);//delete state if (Status.getcurrentstatus (). Equa LS (deletestring)) {for (int j = 0; J < Statuslist.size (); j + +) {if (Statuslist.get (j). Getnextstatus (). Equals (Deletestr ing) {statuslist.get (j). Setrequirestring ("");//delete the relevant condition//statuslist.remove (j);} Statuslist.remove (i); return;} Delete condition if (status.getrequirestring (). Equals (deletestring)) {statuslist.remove (i); return;}} There is no 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++) {i F (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:" +curre ntstatus.requirestring+ ", Input:" +inputstring); if (Currentlist.get (i). Requirestring.equals (inputstring)) {String nextstring = Currentlist.get (i). Getnextstatus ();//system.out.println ("debug-input-the condition in the current state");// System.out.println ("Debug-input--the next state is" +nextstring); int size = Statuslist.size ();//Find the next state 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-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;} @Overridepublic String toString () {return "Status [currentstatus=" + currentstatus+ ", requirestring=" + requirestring + " , nextstatus= "+ Nextstatus +"] ";}}}









Huawei OJ Machine Test training (I.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.