Huawei written test exercise ---- parses 9-in-order, and the 11-in-order string is a 10-in-order digital output.

Source: Internet
Author: User
Parses the 9-in-hexadecimal format, and the 11-in-hexadecimal string is a 10-in-hexadecimal numeric output.
Description:

Parse the 9-in-9 format. The 11-in-11 string is a 10-in-10 numeric output. The length of the input string's valid bits (0v0012345678) is no more than 8 bits. The previous 00 is not a valid bits. After resolution, it is output in decimal format.
-1 is returned if an invalid string is parsed.

9 hexadecimal:
The value range of the 9-digit number is 0, 1, 2, 3, 4, 5, 6, 7, and 8.
9: 0 V or 0 V
Example of a 9-digit system: 0v11 0v564 0v123 0v0784 0v0 0 V 0 V
9-digit error instance: 0v923 0vt12 00v21 0123
9-to-10: 0v11-> 10
0v564-> 463
 
11 hexadecimal:
The range of numbers in the 11 hexadecimal format: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A ()
The start of the 11 hexadecimal format is 0 w or 0 w.
Correct 11-digit example: 0w11 0w564 0w123 0w0a8a 0 w 0 w
11 hexadecimal error instance: 0wb923 0 wvaa 00w21 0wax123
Convert the hexadecimal value to the hexadecimal value:
0w11-> 12
0w564-> 675

Running time limit: Unlimited
Memory limit: Unlimited
Input:

The input is a line of 9-or 11-digit strings. The format is shown in the preceding figure.

Output:

Number in decimal format

Sample input:
0w564
Sample output:
675
Answer:  

I implemented this question in Java, because the 9-and 11-digit strings have certain rules, so I want to use regular expressions for matching. The 9-digit regular expression is ^ 0 [v | V] [0-8] * $, the Regular Expression in decimal format is ^ 0 [w | w] [0-9 | A] * $. If the input string matches the format, it is parsed. Otherwise, enter-1.

Code implementation:

import java.util.Scanner;  import java.util.regex.Pattern;    public class Main {            public int charge(String num){          int res=0;          String reg1 = "^0[v|V][0-8]*$";          String reg2 = "^0[w|W][0-9|a|A]*$";          char sub[];          if(Pattern.matches(reg1, num)){              sub = num.substring(2).toCharArray();              for(int i = 0;i< sub.length; i++){                  int count =Integer.parseInt(String.valueOf(sub[i]));;                  res += count * Math.pow(9, sub.length-i-1);              }              return res;          }else if(Pattern.matches(reg2, num)){              sub = num.substring(2).toCharArray();              for(int i = 0;i< sub.length; i++){                  if(sub[i]==‘a‘||sub[i]==‘A‘){                    res += 11 * Math.pow(11, sub.length-i-1);                  }else {                    int count = Integer.parseInt(String.valueOf(sub[i]));                    res += count * Math.pow(11, sub.length-i-1);                  }              }              return res;          }          return -1;      }      public static void main(String[] args) {              Scanner cin = new Scanner(System.in);              Main main = new Main();              String m;              while (cin.hasNext()){                  m = cin.next();                   System.out.println(main.charge(m));              }  

This code runs in IDE. However, in a bad test environment of Huawei, a test case fails. Because the test case is not made public, I have not found the reason, I hope users can help find bugs.

Huawei written test exercise ---- parses 9-in-order, and the 11-in-order string is a 10-in-order digital output.

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.