In 2014, Huawei school recruitment questions and various code implementation (the maximum number of common approx., the same substring, word count), 2014 Common approx.
Reprinted please indicate the source: http://blog.csdn.net/zhoubin1992/article/details/46460055
As we all know, every year, we have to perform a machine test before the Huawei school recruitment interview. The questions vary from place to place. Generally, there are three questions, and the question difficulty is gradually increased. Just make a question. In addition, I found that Huawei especially prefers to take the string processing questions.
The following is a set of questions and answers I have made in Xi'an for 14 years. Each question provides a variety of solutions. The detailed explanation is in the notes, suitable for beginners.
Huawei trial in 2014-Maximum number of common appointments
Question 1: The maximum number of common orders
Enter a group of positive integers (the number is less than 20) and the maximum number of public approx.
Inputs: 121 33 44 11 1111
Output: 11
Idea: first obtain the maximum common divisor of two numbers, then find the maximum common divisor of the maximum common divisor and the third number .....
/******************************* ----------------------------------- 2014 Huawei trial- question 1: calculate the maximum common approx. Input a group of positive integers (the number is smaller than 20) and output its maximum common approx. Input: 121 33 44 11 1111 output: Minimum Public multiple = product of two integers maximum public approx. ------------------------------------- Author: Mu Zhi, Date: June 1, 2015 Email: bzhou84@163.com **********************************/# include <stdio. h> int largestCommonDivisor (int a, int B) // division of moving phase -- recursion {return a % B? LargestCommonDivisor (B, a % B): B;} int largestCommonDivisor1 (int a, int B) // division of moving phases -- while loop {int c; while (B! = 0) {c = a % B; // remainder c a = B; B = c;} return a;} int largestCommonDivisor2 (int a, int B) // subtraction {while (! = B) {if (a> B) a = a-B; else B = B-a;} return a; // return B;} int main () {int N, a [20], I; while (scanf ("% d", & N )! = EOF) // input N and implement multi-group data input {for (I = 0; I <N; I ++) {scanf ("% d ", & a [I]) ;}int z = a [0]; // initialize the maximum public approx. for (I = 1; I <N; I ++) // traverse {z = largestCommonDivisor2 (z, a [I]) from a [1]; // compare the initial maximum public approx. With the last one to obtain the maximum public approx, then compare the maximum number of common approx.} printf ("% d \ n", z);} return 0 ;}
2014 Huawei trial-same substring
Question 2: enter a group of numbers (up to 15) and remove the numbers whose number of consecutive times is greater than or equal to three. If the number is greater than or equal to three after the number is removed,
Continue with the same processing until there are no consecutive numbers greater than or equal to three in the result.
If "none" is eliminated"
Input: 1 1 1 1 2 2 2 1 3 3 3 1 1 1 1
Output: none
/******************************* ----------------------------------- 2014 Huawei trial- same substring Question 2: enter a group of numbers (up to 15 numbers) and remove the numbers with the number of consecutive times greater than or equal to three. If the number is still greater than or equal to three after the removal, continue the same processing, until the number of consecutive times is greater than or equal to three is not displayed in the result. If all of the outputs are finally cleared, the output is "none". Input: 1 1 1 1 1 2 2 2 1 3 3 3 1 1 Output: none unknown Author: Mu zhidian Date: May 25, 2015 Email: bzhou84@163.com *********************************/# include <iostream> # include <vector> using namespace std; void deleteNum2 (vector <int> a) {// method 1 int cnt = 1, flag = 0; while (! Flag) {vector <int >:: iterator iter; for (iter = a. begin (); iter! = A. end (); iter ++) {// vector <int>: iterator iter2 = iter + 1; if (iter + 1 )! =. End () {if (* iter = * (iter + 1) {cnt ++;} else {if (cnt> = 3) {. erase (iter + 1-cnt, iter + 1); flag = 0; cnt = 1; break;} cnt = 1 ;}} else {if (cnt >= 3) {. erase (iter + 1-cnt, iter + 1); flag = 1; break ;}} flag = 1 ;}} if (. empty () {cout <"none" <endl;} else {for (vector <int>: iterator it =. begin (); it! =. End (); it ++) {cout <* it <";}}} void deleteNum (vector <int>) // method 2 {for (vector <int>: iterator iter =. begin (); iter! = A. end ();) {int I = 1; int flag = 0; vector <int >:: iterator iter2; for (iter2 = iter + 1; iter2! =. End (); iter2 ++) {if (* iter = * iter2) {I ++;} else {if (I> = 3) {. erase (iter, iter + I); flag = 1; I = 1; break;} else {flag = 0; break ;}} if (I> = 3) {. erase (iter, iter + I); break;} if (flag) {iter =. begin () ;}else {iter ++ ;}} if (. size () = 0) {cout <"none" <endl;} else {for (vector <int>: iterator it =. begin (); it! =. End (); it ++) {cout <* it <";}}} int main () {vector <int> a; int n; while (cin> n & n! = 10) {a. push_back (n);} deleteNum (a); system ("pause"); return 0 ;}
2014 Huawei trial-word count
Question 3: enter an article and output the words with the highest and secondary frequencies (all in lower case, separated by commas ).
Only the space, comma, and period delimiters are displayed in the article. Regardless of the frequency. Case Insensitive.
Input: I am a student. I come from XiDian, I love XiDian.
Output: I, xidian
Train of Thought: after entering the article, remove the comma and the period to split the string. Ignore case sensitivity, use map to calculate the frequency of occurrence, and finally find the words with the highest and secondary frequencies.
/******************************* ----------------------------------- 2014 Huawei trial- question 3: enter an article, and output the words with the highest frequency and the secondary frequency (all in lower case, separated by commas ). Only the space, comma, and period delimiters are displayed in the article. Regardless of the frequency. Case Insensitive. Input: I am a student. I come from XiDian, I love XiDian. Output: I, xidian ------------------------------------- train of thought: after entering the article, remove the commas and periods to separate the strings. Ignore the case and use map to calculate the occurrence frequency. ----------------------------------- Author: Muzhi, Date: May 25, 2015 Email: bzhou84@163.com *********************************/# include <iostream> # include <map> # include <string> # include <vector> # include <algorithm> using namespace std; void maxSecond (vector <string> v) {// 1. remove commas (,) and periods (periods) to separate strings. // note that after erase () is deleted, the iterator points to the problem !! For (vector <string >:: iterator it = v. begin (); it! = V. end ();) // traverse all strings {int flag = 0; // flag (delete to 1, default 0) int length = (* it ). length (); for (int I = 0; I <length; I ++) // traverses string characters {if (* it) [I] = ', '| (* it) [I] = '. ') {// capture string t = (* it ). substr (0, I); // substr (0, I) obtains the string z = (* it) whose length starts from 0th bits and is I ). substr (I + 1); // a parameter, intercepted to the end of the string by default v. erase (it); // Save the truncated string, delete it, and insert the string if (t! = "") {V. push_back (t);} if (z! = "") {V. push_back (z);} flag = 1; // to 1 break;} after the if (flag) // delete operation, the iterator points to v. begin () {it = v. begin () ;}else {it ++ ;}// 2. traverse all strings to ignore case sensitivity (vector <string>: iterator it = v. begin (); it! = V. end (); it ++) {transform (it-> begin (), it-> end (), it-> begin (),: tolower );} // 3. map statistics frequency map <string, int> m; for (vector <string >:: iterator it = v. begin (); it! = V. end (); it ++) {m [(* it)] ++;} // 4. find the words multimap <int, string> mi at the highest and secondary frequencies. // multimap allows you to insert elements with duplicate key values. The frequency is key and the string is value for (map <string, int>: iterator it = m. begin (); it! = M. end (); it ++) {mi. insert (pair <int, string> (it-> second, it-> first); // The frequency is key, using the automatic sorting function of map (from small to large)} multimap <int, string >:: iterator iter = mi. end (); // outputs the cout of the highest and secondary frequencies <(-- iter)-> second <","; cout <(-- iter) -> second <endl;} int main () {string s; vector <string> v; while (cin> s) {v. push_back (s);} maxSecond (v); system ("pause"); return 0 ;}