Huawei machine questions-it's time to be alert, Sao Nian, Huawei questions alert

Source: Internet
Author: User

Huawei machine questions-it's time to be alert, Sao Nian, Huawei questions alert

I tried to write something at will once, and I was motivated by the second mistake. I posted an article without any knowledge. You can choose to ignore O (∩ _ ∩) O Haha ~

 

On the afternoon of June 27, I took part in the Huawei machine test. I simply scanned the titles of several questions and selected a string question. In fact, this question is very simple. It can be said that it is simple and cannot be simpler, in addition, there are many solutions. I chose to borrow some functions provided by java directly on the machine, so that I could solve the problem quickly, but the result was stuck for a long time. I failed to pass the test on the machine.

Return to the dormitory in the evening and change the previous code. The problem solving method has not changed. It should be correct after testing.

 

The specific question is not clearly remembered. The question is to enter a string. If the string contains "*", remove "*" and output it; if the first or last character of the string is "*", "*" is retained. If the string does not contain "*" or contains other characters, it can be output directly.

(Isn't it easy? Don't try it)

The Code submitted for the first time is as follows (main part ):

 

For the above Code, the test on the machine has never passed, and the following error is reported for the following test data. The error is found in line 1 of the Code, and the error is still unknown.

 

If an error is not found for a long time, the first request is submitted.

After the code is submitted, an incorrect answer is displayed. After a brief look at the submitted code, it is found that the auxiliary output statement during encoding is not commented out, after the auxiliary output statement is commented out, It is submitted for the second time, and the answer is still displayed incorrectly. At this time, it is not easy to submit.

The asterisk (*) is a special character and cannot output the correct result after the modification. The Code is as follows:

 

It took a long time for this question to be confused, so I have to do the third question for the time being. The third question is still simple and not necessary. After I finish the third question, I will look back at this question, after a long time, I still failed to solve this problem. At this time, my head was so confused that I forgot to submit the problem only three times, and I submitted it again. The result can be imagined.

After that, I started my first question. I had some ideas, but it was estimated that it would take some time to solve the problem correctly. At this time, my friends in the IDC had already gone through a lot. It was not long before the examination was over, in addition, the mood is a little low, give up and continue to answer

 

Summary of the cause of this error:

(1) carelessness:

A. I haven't simply browsed several questions before I started to select the question for this string. Otherwise, I will definitely do the third question first, because the question is very simple, you must know that you will be confident and calm after you make a question.

B. Some auxiliary output statements for encoding are not commented out during submission, which wastes a chance of answering (only three times can be submitted for each question)

(2) solid foundations:

A. I am not familiar with some functions. I didn't notice that the asterisk (*) is A special character when I used the split () method to separate strings.

B. an error occurred while converting the string array returned by the split () method to a string. I printed and output the string array directly in the Code: System. out. print (line. split ("\ *"); I feel silly at the time. If I calm down and analyze the output information, I should know what is going on, at that time, I saw that I didn't want the output, so I was in a hurry to modify the code.

(3) Bad mentality:

A. I didn't give up the original solution when I hadn't solved the problem for A long time, even though I know how to solve it.

B. I am confused because I have not found the cause of the Error for a long time,I am busy modifying the code without carefully analyzing the unexpected results or errors reported by running the output.And the same data is not used in the pre-and post-test, so that different lines of code are located for each error (it should be the biggest mistake in this test)However, you do not know that you are only one step away from success. You only need to modify the original code to traverse and splice the string array returned by the split () method and convert it to a string.

 

The code after modification at night is as follows:

Package com. test; import java. util. *; public class Main {/** Creates a new instance of Main */public Main () {} public static void main (String [] args) {commandid = new partition (System. in); String line = bytes. next (); // System. out. println ("the input string is:" + line); if (line. contains ("*") {// The string contains "*" // System. out. println ("string contains *"); if (line. startsWith ("*") | line. endsWith ("*") {// The string at the beginning and end of the string contains "*" int end = line. Length (); if (end = 1) {// The string contains "*" and its length is 1 System. out. print (line);} else {String ss = ""; if (line. startsWith ("*") & line. endsWith ("*") {String strarray = line. substring (1, end-1); // System. out. print ("substring:" + strarray); String [] str = strarray. split ("\ *"); for (int I = 0; I <str. length; I ++) {ss + = str [I];} System. out. print ("*" + ss + "*");} if (line. startsWith ("*")&&! Line. endsWith ("*") {String strarray = line. substring (1, end); // System. out. print ("substring:" + strarray); String [] str = strarray. split ("\ *"); for (int I = 0; I <str. length; I ++) {ss + = str [I];} System. out. print ("*" + ss);} if (! Line. startsWith ("*") & line. endsWith ("*") {String strarray = line. substring (0, end-1); // System. out. print ("substring:" + strarray); String [] str = strarray. split ("\ *"); for (int I = 0; I <str. length; I ++) {ss + = str [I];} System. out. print (ss + "*") ;}} else {// The string contains "*", but it is not at the beginning or end of the System. out. print (line. replaceAll ("\ *", "") ;}} else {// The string does not contain "*" System. out. print (line );}}}

I tested some data with the above code to get the correct results. Although there are still many optimizations (such as code redundancy), they will not be shown here.

Paste several groups of test data (input ):

*************aaa*bb*c*

The corresponding result should be as follows (output ):

********aaabbc*

 

There are other solutions that should be simpler and faster, such as the following (note the I increase and decrease control in this solution loop ):

package com.test;import java.util.Scanner;public class Test {    public static void main(String[] args){        Scanner scanner=new Scanner(System.in);        String line=scanner.next();        StringBuffer str=new StringBuffer(line);        for(int i=1;i<str.length()-1;i++){            if(str.charAt(i)=='*'){                str.deleteCharAt(i);                i--;                System.out.println(str.length());             }        }        System.out.println(str);            }}

 

I was disappointed with the result of a simple machine test. I vowed that the gentleman should be more cautious and this mistake should not be repeated. When the gentleman worked harder, it is difficult to meet the difficulties!

I feel that this article is a bit corrupted. Please do not be surprised (~ O ~)~ ZZ

 

============ View the comment of yuanyou. Here, the string segmentation function in java is pasted: java. lang. string. A simple example of split is as follows:

In Java, we can use split to split the string according to the specified delimiter, and then return the string array. The following is an example of string. split usage and precautions:

 

Split () method: Splits a string into substrings, andReturns the result as a string array..

The usage is as follows:

StringObj. split ([separator, [limit])

Parameter description:

StringObj: A required String object or text to be decomposed. This object is not modified by the split method.

Separator: optional, string or regular expression object. It identifies whether one or more characters are used to separate strings. If this option is ignored, a single array of elements containing the entire string is returned.

Limit: Optional. This value is used to limit the number of elements in the returned array (that is, it can be divided into several array elements at most, which may be affected only when the number is positive)

 

Example 1:

String str="Java string split test"; String[] strarray=str.split(" "); for (int i = 0; i < strarray.length; i++){    System.out.println(strarray[i]); }

The output is as follows:

Javastringsplittest

 

Example 2:

String str = "Java string split test"; String [] strarray = str. split ("", 2); // use limit to split up to two strings for (int I = 0; I <strarray. length; I ++) {System. out. println (strarray [I]);}

The output is as follows:

Javastring split test

 

Example 3:

String str = "192.168.0.1"; String [] strarray = str. split ("\\. "); // note that it cannot be written as String [] strarray = str. split (". "); for (int I = 0; I <strarray. length; I ++) {System. out. println (strarray [I]);}

The output is as follows:

19216801

 

Experience Sharing: 

1. If the separator is ". "," | "," * "," + ", and other escape characters, use str directly. split (". ") will throw an exception, must be added in front of" \ ", such as split (\ | );

2. If "\" is used as the separator, it must be written as follows: String. split ("\\\\"), because "\" is used in Java to represent "\", the custom string is like this: string Str = "a \ B \ c ";

3. If a String contains multiple separators, you can use "|" as a hyphen, for example, string str = "Java String-split # test". You can use Str. split ("|-| #") separates each string;

4. The result of the split method is a string array. In stingObj, the position where separator appears must be decomposed. separator is not returned as part of any array element.

 

Example 4:

String str="192\\168-0.1"; String[] strarray=str.split("\\\\|-");for (int i = 0; i < strarray.length; i++) {    System.out.println(strarray[i]); }

The output is as follows:

1921680.1

 

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.