Huawei on-Machine problem Summary (vii)
Note: The compilation environment is Visual Studio 2012, and the answer is for informational purposes only.
Directory
- Huawei on-Machine problem summary Seven
- Directory
- Question 31st
- Question 32nd
- Question 33rd
- Question 34th
- Question 35th
Question 31st
Known 2 metro lines, where a is the ring, B is the east-west line, the lines are two-way. After the site name is as follows, two lines crossing the transfer point with T1, T2 expressed. Write the program, enter the name of any two sites, output the minimum number of stations that need to pass through the subway (including the starting and ending points of the input, the transfer site is counted only once).
Metro line A (loop) past station: A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
Subway Line B (straight line) past station: B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
#include <iostream>#include <string>using namespace STD;inline intMinintAintb) {returna > B? B:a;}inline intMaxintAintb) {returnA < b? B:a;}intMindistance (intStation1,BOOLType1,intStation2,BOOLtype2) {if(!type1 &&!type2) {intminstation = min (Station1, station2);intMaxstation = Max (Station1, Station2);returnMin (maxstation-minstation, Minstation + --Maxstation); }Else if(Type1 && type2) {intminstation = min (Station1, station2);intMaxstation = Max (Station1, Station2);returnMaxstation-minstation; }Else if(!type1 && type2) {intD1 = mindistance (Station1, Type1,Ten, type1) + mindistance (Station2, type2,6, type2);intD2 = Mindistance (Station1, Type1, the, type1) + mindistance (Station2, type2, A, type2);returnMin (d1, D2); }Else{intD1 = mindistance (Station1, Type1,6, type1) + mindistance (Station2, type2,Ten, type2);intD2 = Mindistance (Station1, Type1, A, type1) + mindistance (Station2, type2, the, type2);returnMin (d1, D2); }}intComputestation (Const string&s,BOOL&type) {Const stringTMP (S.begin () +1, S.end ());intnum = Stoi (TMP);if(s[0] ==' A ') {type =0;if(Num >=Ten) num++;if(Num >= -) num++;returnNum }Else if(s[0] ==' B ') {type =1;if(Num >=6) num++;if(Num >= One) num++;returnNum }return-1*num;}intPassstations (Const string&S1,Const string&S2) {BOOLType1 =0, type2 =0;intStation1 = Computestation (S1, type1);intStation2 = computestation (s2, type2);if(Station1 <=0&& Station2 <=0) {returnStation1 = = Station2?1:6; }Else if(Station1 <=0) {if(Station1 = =-1)returnMin (Mindistance (Ten,0, station2,type2), Mindistance (6,1, station2,type2)) +1;Else returnMin (Mindistance ( the,0, station2,type2), Mindistance ( A,1, station2,type2)) +1; }Else if(Station2 <=0) {if(Station2 = =-1)returnMin (Mindistance (Ten,0, Station1,type1), Mindistance (6,1, station1,type1)) +1;Else returnMin (Mindistance ( the,0, Station1,type1), Mindistance ( A,1, station1,type1)) +1; }Else{returnMindistance (station1,type1,station2,type2) +1; }}intMain () {stringS1, S2;Cin>> s1 >> S2;cout<< passstations (S1,S2) << Endl;return 0;}
Question 32nd
32. Enter a number of strings, separated by ', ', to output the number of all numbers left after the maximum and minimum values have been removed. (where maximum and minimum values may be multiple)
Smple input:3,3,5,3,6,9,7,9 Sample Output:3
#include <iostream>#include <string>#include <vector>using namespace STD;intResidualnum (Const string&s) { vector<int>VAutoBegin = S.begin (); while(Begin! = S.end ()) {AutoAhead = begin +1; while(Ahead! = S.end () && *ahead! =', ') {ahead++; }stringTMP (begin, ahead); V.push_back (Stoi (TMP));if(ahead = = S.end ()) Break; begin = ++ahead; }intMin = v[0], max = v[0], Mincount =1, MaxCount =1; for(inti =1; I < v.size (); i++) {if(V[i] < min) {min = v[i]; Mincount =1; }Else if(V[i] = = min) {mincount++; }if(V[i] > Max) {max = v[i]; MaxCount =1; }Else if(V[i] = = max) {maxcount++; } }returnV.size ()-mincount-maxcount;}intMain () {stringSCin>> s;cout<< Residualnum (s) << Endl;return 0;}
Question 33rd
33. To choose 2 persons from 5 people as the etiquette, each person's height range is 160-190, the height difference of 2 people is required to be the smallest (if the difference is the same, select the highest of the two), in order to output two people in ascending height.
Smple input:161 189 167 172 188 Sample output:188 189
#include <iostream>#include <vector>#include <algorithm>using namespace STD;voidSelectman ( vector<int>&v,int&I1,int&I2) {sort (V.begin (), V.end ());intMin = v[1]-v[0], index =0; for(unsignedi =1; I < v.size ()-1; i++) {if(v[i+1]-v[i] <= min) {min = v[i+1]-v[i]; index = i; }} i1 = V[index]; I2 = v[index+1];}intMain () { vector<int>V for(inti =0; I <5; i++) {intNumCin>> num; V.push_back (num); }intI1,i2; Selectman (V,I1,I2);cout<< I1 <<" "<< i2 << Endl;return 0;}
Question 34th
34. Enter a string of strings, with ordinary characters and parentheses (including ' (', ') ', ' [', '] '), to verify that the brackets match, or output 0 if matched, or output 1.
Smple Input:dfa (SDF) Df[dfds (DFD)] Smple output:0
#include <iostream>#include <string>#include <stack>using namespace STD;BOOLIsMatch (Const string&s) { Stack<char>Charstack; for(AutoBegin = S.begin (); Begin! = S.end (); ++begin) {if(*begin = =' ('|| *begin = =' ['|| *begin = =' {') {Charstack.push (*begin);Continue; }if(*begin = =' ) ') {if(!charstack.empty () && charstack.top () = =' (') {Charstack.pop ();Continue; }return false; }if(*begin = ='] ') {if(!charstack.empty () && charstack.top () = =' [') {Charstack.pop ();Continue; }return false; }if(*begin = ='} ') {if(!charstack.empty () && charstack.top () = =' {') {Charstack.pop ();Continue; }return false; } }return!charstack.empty ();}
Question 35th
35. Determine the number of palindrome, is returned 1, not return 0.
#include <iostream>#include <vector>using namespace STD;BOOLIspalindrome (intN) { vector<int>V while(n! =0) {V.push_back (n%Ten); N/=Ten; } for(unsignedi =0; I < v.size ()/2; i++) {if(V[i]! = v[v.size ()-i-1]) {return false; } }return true;}intMain () {intNCin>> N;cout<< ispalindrome (n) << Endl;return 0;}
Huawei on-Machine problem Summary (vii)