Interview Question 1: the number of questions to be submitted, and questions to be submitted

Source: Internet
Author: User

Interview Question 1: the number of questions to be submitted, and questions to be submitted

Source: Public platform Chapter 9 algorithm. Since the 9-Chapter algorithm only provides the algorithm and does not provide the source code, I have provided the source code based on my own capabilities. Of course, there may be many shortcomings. I hope you will not give me any advice. ---- Xiao ran

Description:

There are 2n + 1 numbers, where 2n numbers are paired in pairs, and 1 number is placed in a single order to find this number. The time complexity of O (n) and the space complexity of O (1) are required. Advanced question: What should I do if there are 2n + 2 numbers, of which 2 are listed on the ticket?

Question Analysis:

For any a, a ^ a = 0; a ^ 0 = a; the following code can be written based on these two properties.

Level 1:The number of 2n + 1 is exclusive and the same number is offset. The answer to the exclusive or is the number to be searched.


Advanced:Assume that two different numbers are a and B, and! = B. If the number of 2n + 2 is exclusive, c = a xor B is obtained, and c is not equal to 0. Therefore, we can find a 1 bit in the binary bits of c. It can be inferred that a and B are 0 and 1 respectively, therefore, the 2n + 2 numbers are divided into the group with this bit 0 and the group with this bit as 1. The two groups respectively contain the 2n '+ 1 Number and 2n ''+ 1 number, use the first-level algorithm to solve the problem.


Source code:
/**
* Chapter 9 algorithm interview question 1: number of tickets
* Unique or simple application
* Exclusive or properties: a ^ a = 0; a ^ 0 =;
*/
# Include <iostream>
# Include <cstdio>
Using namespace std;
Int a [10000], B [10000];
Int ans, ans0, ans1;
Int LuoDan1 (int n, int * a) {// The first order problem. The number of 2 * n + 1 is given. Only one is single.
Ans = 0;
For (int I = 0; I <2 * n + 1; I ++ ){
Ans ^ = a [I];
}
Return ans;
}
Int Judge (int n, int k) {// determines whether the k-th (from right to left) is 1
Int I = 1;
While (I ++ <k-1) n> = 1; // shift the K-1 to the left
If (n> 1 = 1) return 1; // if the k-th bit is 1, 1 is returned.
Else return 0;
}

Int LuoDan2 (int n, int * a) {// The first order problem. The number of 2 * n + 2 is given. Only two numbers are single.
Ans = 0;
For (int I = 0; I <2 * n + 2; I ++ ){
Ans ^ = a [I];
}
Int k = 1;
// Cout <ans <endl;
While (ans> 0) {// locate 1 of the second bit
If (ans & 1) = 1) break;
++ K;
Ans> = 1;
}
Ans0 = ans1 = 0;
For (int I = 0; I <2 * n + 2; I ++ ){
If (Judge (a [I], k) {// The k bit is 1
Ans1 ^ = a [I];
}
Else ans0 ^ = a [I];
}
// Cout <ans0 <"" <ans1 <endl;
Return ans;
}
Int main ()
{
Int n;
While (cin> n ){
Int ans = 0;
For (int I = 0; I <2 * n + 1; I ++ ){
Cin> a [I];
// Ans ^ = a [I];
}
Cout <LuoDan1 (n, a) <endl;
For (int I = 0; I <2 * n + 2; I ++ ){
Cin> B [I];
}
// Cout <ans <endl;
LuoDan2 (n, B );
Cout <ans0 <"" <ans1 <endl;
}
Return 0;
}





This is Microsoft's interview question.

No chance of survival
First, the first person will not take only one. Because 1 is the smallest, it will only let him die, and the first person will not take too many green beans, and the second person will know that if the first person takes the number, then he will take the number that is the closest to the first person. Then the two men will take the number of green beans. they are very smart people, so the people behind them will surely know that the second person will take the number closest to the first person in order to save their lives, and the third person will take the same ideas as the second one in order to save their lives. the number adjacent to them, and the rest of the people will not come up with a number larger or smaller than the first two, and the number of the five people to get the mung bean will be two adjacent numbers, there is no center between them. all are killed.

Database interview question 1

Select a. Product Name,
Isnull (b1. product quantity, 0) as Warehouse id_1,
Isnull (b2. product quantity, 0) as Warehouse id_2,
Isnull (b3. product quantity, 0) as Warehouse id_3,
Isnull (b4. product quantity, 0) as Warehouse id_4
From
(Select distinct product name from tablename)
Left join tablename b1 on b1. product name = a. Product Name and b1. repository id = 1
Left join tablename b2 on b2. product name = a. Product Name and b2. repository id = 2
Left join tablename b3 on b3. product name = a. Product Name and b3. repository id = 3
Left join tablename b4 on b4. product name = a. Product Name and b4. repository id = 4

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.