Huawei Java computer questions

Source: Internet
Author: User

First, enter the string length len1, string S1, string len2, string S2. Compare from the back to the front and output the number of different elements based on the shortest string.

Example: input: S1 = ", 5" len1 = 3 S2 = ", 5" len2 = 5

Output: 2

Function prototype

Public int getdiffnum (INT len1, string S1, int len2, string S2)

public class HuaWeiTest {public static void main(String[] args) {String s1 = "1,3,5";String s2 = "2,4,1,7,5";int len1 = 3;int len2 = 5;HuaWeiTest hwt = new HuaWeiTest();System.out.println(hwt.getDiffNum(len1, s1, len2, s2));}public int getDiffNum(int len1, String s1, int len2, String s2)  {int count = 0;int len = 0;String[] arr1 = s1.split(",");String[] arr2 = s2.split(",");if(len1 > len2) len = len2;else len = len1;for(int i=0;i<len;i++) {if(!arr1[len1-i-1].equals(arr2[len2-i-1])) {count ++;}}return count;}}

Question 2: Enter the string length, string, and count M. Count from the beginning to the end. When the number of M elements is reached, M elements are listed and assigned to m, and then from the next count cycle until all numbers are listed, all the given numbers are numbers greater than 0. Output a queue.

For example, enter Len = 4 STR = "3, 1, 2, 4" m = 7

Output: 2, 3, 1, 4

Function prototype

Public String getoutstring (INT Len, string STR, int m)

import java.util.ArrayList;import java.util.List;public class HuaWeiTest {public static void main(String[] args) {int len=4;String str="3,1,2,4";   int m=7;  HuaWeiTest hwt = new HuaWeiTest();System.out.println(hwt.getOutString(len, str, m));}public String getOutString(int len, String str, int m) {String ret ="";String[] arr = str.split(",");List<String> ls = new ArrayList<String>();for(int i=0;i<len;i++) {ls.add(arr[i]);}for(int i=0;i<len;i++) {int temp = (m-1)%ls.size();ret += ls.get(temp);m = Integer.parseInt(ls.get(temp))+temp;ls.remove(temp);}return ret;}}

Question 3: enter an expression with no parentheses and the number is smaller than 0-9. The calculation result is output. All intermediate results are converted into an integer.

Example: input: 3 + 8x2/9-2

Output: 2

Function prototype

View
Plain
  1. Public int getmyret (string Str)

1. Talent Show scoring, which can be divided into expert judges and public judges. The score [] array stores the scores of each judge. The judge_type [] stores the judges' categories corresponding to the score [] array, judge_type [I] = 1, indicating an expert
Judge, judge_type [I] = 2, representing the public judges, and N representing the total number of judges. The scoring rules are as follows: The scores of expert judges and public judges are divided into one average score (average score), and then the total score = average score of expert judges *
0.6 + public judges * 0.4, total score rounded up. If no public judges are available, the total score is equal to the average score of the panel judges, and the total score is rounded up. The function returns the score of the contestant.

Function interface int cal_score (INT score [], int judge_type [], int N)

public class SWTest {public static void main(String[] args) {int score[] = {34,53,65,75,64};int judge_type[] = {1,1,1,2,2};SWTest st = new SWTest();System.out.print(st.cal_score(score, judge_type, 5));}int cal_score(int score[], int judge_type[], int n){int totalExpert = 0;int totalPublic = 0;int numExpert = 0;int numPublic = 0;for(int i=0;i<n;i++) {if(judge_type[i]==1) {totalExpert += score[i];numExpert ++;}if(judge_type[i] == 2){totalPublic += score[i];numPublic ++;}}if(0==numPublic){return (int)totalExpert/numExpert;}else{return (int)((totalExpert/numExpert)*0.6) + (int)((totalPublic/numPublic)*0.4);}}}

2. For an array input [], if the array length N is an odd number, place the largest element in the array in the middle of the output [] array. If the array length N is an even number, put the largest element in the array into the output
[] On the right of the two positions in the middle of the array, and then in the order from large to small, the order is on both sides of the first position, in the order of one left and one right, store the remaining number in sequence.
For example: input [] = {3, 6, 1, 9, 7} output [] = {3, 7, 9, 6, 1}; input [] = {3, 6, 1, 9, 7, 8} output [] = {1, 6, 8, 9, 7, 3}

Function interface void sort (INT input [[, int N, int output [])

public class SWTest {public static void main(String[] args) {int input[] = {3,6,1,9,7,8};int output[] = new int[6];SWTest st = new SWTest();st.sort(input, 6, output);for(int i=0;i<output.length;i++){System.out.print(output[i]);}}void sort(int input[], int n, int output[]){for(int i=0;i<n-1;i++){for(int j=n-1;j>i;j--){if(input[j]>input[j-1]) {int temp = input[j];input[j] = input[j-1];input[j-1] = temp;}}}int low = n/2-1;int high = n/2+1;output[n/2] = input[0];for(int i=1;i<n;){output[low] = input[i];low --;i++;if(i == n) break;output[high] = input[i];high++;i++;}}}

3. Operating System Task Scheduling Problems. Operating system tasks are classified into system tasks and user tasks. The system task priority is <50, the user task priority is> = 50, and <= 255. If the priority is greater than 255, it is invalid.
And should be removed. The existing task queue task [] has a length of n. The element value in the task indicates the priority of the task. The smaller the value, the higher the priority. The scheduler function implements the following functions, store the tasks in task [] to the system_task [] array and the user_task [] array in sequence by system tasks and user tasks (the element value in the array is the subscript of the task in the task [] array ). ), in addition, tasks with the highest priority are listed first, and tasks with the same priority are listed
The queues are arranged in sequence (that is, the tasks that first enter the queue are listed in the front), and the array element is-1, indicating the end.
Example: task [] = {0, 30,155, 1, 80,300,170, 40, 99} system_task [] = {0, 3, 1, 7, -1} user_task [] = {4, 8, 2, 6,-1}

Function interface void scheduler (INT task [], int N, int system_task [], int user_task [])

void scheduler(int task[], int n, int system_task[], int user_task[]){int min = 0 ,s =0, u =0 ;for(int i=0;i<n-1;i++) {min = 0;for(int j=0;j<n;j++) {if(task[min]>task[j]) min = j;}if(task[min]<50) {system_task[s++] = min;}else if(task[min]<=255) {user_task[u++] = min;}task[min] = 300;}system_task[s]= -1;user_task[u] = -1;}

Problem description:
The standard format of Chinese Mainland carrier mobile phone numbers is: country code + mobile phone number, for example: 8613912345678. Features:
1. 13 characters in length;
2. Start with 86 country codes;
3. Each digit of a mobile phone number is a number.
Please implement the function of determining the validity of the mobile phone number (Note: The examinee does not need to pay attention to the authenticity of the mobile phone number, that is, the mobile phone number such as 86123123456789, we also think it is legal). Requirements:
1) if the mobile phone number is valid, return 0;
2) If the phone number length is invalid, 1 is returned.
3) if the mobile phone number contains non-numeric characters, 2 is returned;
4) if the phone number does not start with 86, 3 is returned;
[Note] Except for successful cases, the priority of the above legitimacy judgments is reduced in turn. That is to say, if the length is determined invalid, 1 is returned directly, and no other validity judgment is required.
Required implementation functions:
Int s int verifymsisdn (char * inmsisdn)
[Input] char * inmsisdn, indicating the input mobile phone number string.
[Output] None
[Return] the result of judgment. The type is int.
Example
Input: inmsisdn = "869123456789"
Output: None
Return Value: 1
Input: inmsisdn = "8813912345678"
Output: None
Return Value: 3
Input: inmsisdn = "8613912345678"
Output: None
Return Value: 0

public class PhoneNumber {public static void main(String[] args) {String  inMsisdn = "8613912345678";System.out.println(new PhoneNumber().verifyMsisdn(inMsisdn));}int verifyMsisdn(String inMsisdn){char[] inchars = inMsisdn.toCharArray();if(inchars.length!=13) return 1;for(int i=0;i<13;i++) {if(!Character.isDigit(inchars[i])){return 2;}}if((Character.digit(inchars[0], 10)!=8 )||(Character.digit(inchars[1], 10)!=6) ) return 3;else return 0;}}

Related Article

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.