Some interesting face questions (continuous update) __ Other

Source: Internet
Author: User
Tags rand shuffle

Original address: http://blog.csdn.net/wangyuling1234567890/article/details/38565239


1. Write an efficient C language program to calculate the number of 1 in an unsigned integer.

for (count=0 x; count++) x &= x-1;
Similarly, compute the number of digits in 0: for
(count=32 x; count--) x &= x-1;


2, given the string S1 and S2, write the program to determine whether the S2 can rotate from S1, requires only one call to the STRSTR system functions.

void Main ()
{
    char *str1 = ' Wang ';
    Char *str2 = "Angw";

    Char *tmp = (char*) malloc (2*strlen (STR1) + 1);
    sprintf (tmp, "%s%s", str1, str1);

    if (STRSTR (TMP, STR2))
        cout<< "yes" <<endl;

    GetChar ();
}


3, if the condition fill in what thing, can let the following program print out HelloWorld.

if (<condition>)          
    printf ("Hello");
else
    printf ("World");

if (!printf ("Hello"))
    printf ("Hello");
else
    printf ("World");

4, only modify or add a character, so that the following program to print out 20 * numbers. (There are at least 3 kinds of solutions)

int main ()
{
    int i, n =;
    for (i = 0; i < n; i--)
        printf ("*");
    return 0;
}

Solution 1:
intmain ()
{
    inti,n=20;
    for (i=0;i<n;n--)
        printf ("*");
    Return0;
}

Solution 2:
intmain ()
{
    inti,n=20;
    for (i=0;i<n;i++)
        printf ("*");
    Return0;
}


5, write an algorithm, reverse the sequence of words in the string.

For example: Hi Welcome to Cricode inversion into Cricode to Welcome hi

void Main ()
{
    string str ("Hi Welcome to Cricode");
    Stack<char> Cstack;
    stack<char> tmp;
    int index = 0;

    for (index = 0; index < str.size (); index++)
    {
        cstack.push (Str[index]);

    index = 0;
    while (!cstack.empty ())
    {
        if (' = = Cstack.top ())  //experimental code, does not make a judgment of punctuation
        {while
            (!tmp.empty ())
            {
                str[index++] = Tmp.top ();
                Tmp.pop ();
            }
            str[index++] = ';
            Cstack.pop ();
        }
        else
        {
            Tmp.push (cstack.top ());
            Cstack.pop ();
        }

    while (!tmp.empty ())
    {
        str[index++] = Tmp.top ();
        Tmp.pop ();
    }

    cout<<str<<endl;
    GetChar ();
}

6, 100 floors, give you two balls, the characteristics of the ball is as follows: If you drop the ball from a floor less than x in this building, the ball will not break, if you drop it from a floor greater than or equal to X, the ball will surely break. If you can reuse a ball that is not broken, give an algorithm to find the boundary floor x with a minimum number of throw shots.

If it is two glass balls, the minimum number m determines which layer of the height of N is starting to break the glass ball, which is equivalent to finding the smallest m, making 1+2+...+m >= N.
assuming n is exactly equal to 1+2+...+m, then I think the best strategy is to throw the first glass ball to the M layer, and if it breaks, it obviously takes the remaining m-1 layer to try from the bottom up to one by one, and the worst case is m;
assuming M is not broken, the problem is equivalent to building n ' =1+2+...+ (m-1) Where the same problem required the number of times M ' +1 (1 is the first attempt at the M-level),
according to our recursion, easy to get n ' corresponds to the required number of times exactly is m-1, so the total number of times is also M.

our score should tend to be equal to the total number of tests in both cases, regardless of failure or success. So this should be the optimal algorithm.

of course, when n cannot be represented as a 1+2+...+m, we can only find the smallest m as the number of times that needs to be tested.
As for the 100 floors, obviously m=14, our first throw should be in the 14th, if not broken to continue in 14+13=27, and then not broken to throw in the 27+12=39 layer, and so on.

7, A, b Two cities apart from 1000Km, we have 3,000 bananas from a city to B city, known an elephant can transport up to 1000 bananas at a time, and every time the elephants go 1Km to eat a banana, if the banana was eaten, the elephant can not continue to go forward. How many bananas will you be able to transport to City B in the end?

1000 meters to set a back point, is not feasible. 1000 meters set two back point a,b, that is, 1000 meters into 3 paragraphs. The length of each segment is from starting point to point a x1,a point to B Point X2,b point to X3,
x1+x2+x3=1000
at a point need to go back and forth 5 times to 3000 of the banana to a point. Then a point has 3000-5*x1 banana,
by inference, 3000-5*x1 is less than equal
to, so to B point 3000-5*x1-3*x2, this value is less than equal to 1000, the
end is left 3000-5*x1-3*x2-x3, According to the above judgment, if all is equal to:
x1=200;x2=334
finally left 532.

Note: In fact X2 take 333, this time the remaining 533, note that in this case need to discard a banana.


8, to give you 6 with the same length of chopsticks, the requirements of the composition of four equilateral, not allowed to break or bend chopsticks.

Just the tetrahedron.


9, the exchange of two plastic variable value, do not use the third variable

Solution 1: Put
two plastic number on the axis, such as figure (spit slot, blog can not draw, really inconvenient, you can consider adding simple drawing function): 
<span style= "color: #000099;" >-----<span style= "color: #FF0000;" >|</span>-------------<span style= "color: #FF0000;" >|</span>------></span>
     a <span style= "color: #FF0000;" ><--b-a--></span> B

So there is
a=b-a  //a for the distance between A and B b=b-a  minus A, b distance, equal to the value of a before
//b +a  //now B plus the distance between a and B, equal to the value of B before the

solution 2: According to the
XOR or operator nature, a=a^b^b:
a=a^b  //a ' = a^b, b ' = b
b=b^a  //b ' = A, a ' = A^b
a=b^a  //b ' = A, a ' = B

Solution 3:
pointer operation, principle 1


10, do not use the comparison operator to find the larger number of two

Or the principle of the axis

#define MAX (A, B) (((a) + (b) + ABS ((a)-(b)))/2)


11, Shuffle algorithm

Solution 1

A, define a 54 integer array poker[54], and assign the value respectively 1~54

b, generate two random numbers: M=rand ()%54 + 1, N=rand ()%54 + 1

C, Exchange Pocker[m] and Pocker[n] values

D, repeat B, C action n times, n value larger the average, but need to consider efficiency.


Solution 2

Use a collection, such as a vector or list

Vector Pocker;  No shuffle of a poker, in order for 1~54
vector orderd;  A shuffle of poker collection, initially null while

(Pocker.size ())
{
    p = rand ()% pocker.size ();
    Orderd.push_back (Pocker[p]);
    Pocker.erase (Pocker.begin () + p);
}

12, there is a 27 cm of fine wood rods, in the 3rd centimeter, 7 centimeters, 11 centimeters, 17 centimeters, 23 centimeters each of these five positions have an ant. The wooden pole is very fine and cannot pass through two ants at the same time. At the beginning, the ants head toward any (left or right), they will only forward or turn, not backward. When any of the two ants meet, two ants will turn back and go in the opposite direction. Suppose the ant walks 1 centimeters per second, writes the program, asks all ants to leave the wooden pole the shortest time and the longest time. (from the beauty of programming)

Solution 1

Brute Force solution (brute force), enumerating the initial orientation of each ant, simulating the movement of each ant to solve.


Solution 2 Consider, although two ants all turn to the opposite direction after meeting, but, can be seen as the two ants met, pass by (see here may be a lot of people have a sense of suddenly feel it). In other words, it can be thought that the movement of ants is independent, whether there is a meeting is not the focus of the problem.
This way, though, each ant's trajectory is different from the original. But the shortest time and the longest time that any ant leaves a wooden pole is unchanged. It is only necessary to calculate the time each ant leaves the wood bar separately, so that all the ants can get out of the wood.
This way, the program only needs to traverse all the ants, and the longest time each ant goes out of the wood bar (the ant is walking farther away from him), the shortest time (ants toward their closer to the end of the walk) to find out, even if the maximum value, the two maximum is all the ants leave the wooden rod of the longest time and the shortest time.

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.