Xiaomi 2015 written test programming question 1. [Programming questions] understand binary
Time limit: 1 seconds
Space limit: 32768K
There are 10 kinds of people in the world, one understands the binary system, one does not understand. So you know the binary representation of two int32 integers m and n, how many bits are different?
Input Example 1:
1999 2299
Output Example 1:
7
Analysis: The first step is to find the two number of XOR, get a new number, the same bit is 0, the difference is 1; the second step uses the bitwise operation to find the number of 1 in the count as the result.
1 classSolution {2 Public:3 /**4 * Get two different number of shaped binary expression digits5 * 6 * @param m integer m7 * @param n integer n8 * @return Integral type9 */Ten intCountbitdiff (intMintN) One { A intres=m^N; - intCount=0; - while(RES) the { -++count; -Res= (res-1) &Res; - } + returncount; - } +};
2.
[Programming questions] The pig of the tuyere-China bull market
Time limit: 1 seconds
Space limit: 32768K
The pigs can fly under the tuyere. The bull market in China today is a "missed seven years". Give you an opportunity to review the history of a stock, known for a continuous N-day price movement, in the length of an integer array of n, the array of elements I (Prices[i]) represents the stock price of the first day. Suppose you don't have a stock at first, but you have the opportunity to buy 1 shares and then sell 1 shares at most two times, and make sure you have no stock before you buy. If two trading opportunities are abandoned, the yield is 0. Design algorithms to calculate the maximum benefits you can get. Input value range: 2<=n<=100,0<=prices[i]<=100
Input Example 1:
3,8,5,1,7,8
Output Example 1:
12
Analysis:
To calculate the maximum gain for a two trading opportunity, you can cycle through the maximum gains that can be obtained for the first transaction on page K, and then go through the sequence once and record the maximum gains that can be obtained from the second transaction after the K day. The result can be obtained by adding the maximum value of two transactions in K Day. Note that the positive sequence traversal represents the maximum benefit that can be obtained before the K day, and the post-order traversal represents the maximum benefit that can be obtained after k days. The top two gains are the sum of the benefits.
1 classSolution {2 Public:3 /**4 * Calculate the maximum benefit you can get5 * 6 * @param prices Prices[i] that is, the stock price of the first day7 * @return Integral type8 */9 intCalculatemax (vector<int>prices) {Ten intLen =prices.size (); Onevector<int>Leftmax (len); Avector<int>Rightmax (len); - - //Positive sequence Traversal the intPrimin = prices[0]; -leftmax[0] =0; - for(intI=1; i<len;++i) { -Primin =min (primin, prices[i]); +Leftmax[i] = max (leftmax[i-1], Prices[i]-primin); - } + A //Post-post traversal at intPrimax = prices[len-1]; -rightmax[len-1] =0; - for(inti=len-2; i>=0;--i) { -Primax =Max (Primax, prices[i]); -Rightmax[i] = max (rightmax[i+1], Primax-prices[i]); - } in - intPmax =0; to for(intI=0; i<len;++i) +Pmax = Max (Pmax, Leftmax[i] +rightmax[i]); - returnPmax; the * } $};
3.[programming questions] Xiaomi git
Time limit: 1 seconds
Space limit: 32768K
Git is a distributed code management tool that uses a tree to record a file's change history, such as: Base ' <--base<--a<--a ' ^ | ---b<--b ' Xiaomi engineers often need to look for two branches of the nearest split point, that is, base. Assuming that the git tree is a multi-fork tree, implement an algorithm that calculates the nearest split point for any two points on the git tree. (assuming that the number of Git tree nodes is n, the git tree is represented in the form of an adjacency matrix: the string array matrix contains n strings, each consisting of the characters ' 0 ' or ' 1 ', with a length of N.) matrix[i][j]== ' 1 ' when and only if Git tree species are connected to the first and the J nodes. Node 0 is the root node of the git tree. )
Input Example 1:
[01011,10100,01000,10000,10000],1,2
Output Example 1:
1
Analysis: Test instructions not understand, understand the supplementary
Xiaomi 2015 written programming questions