Questions about the basis of the written examination in the October 28 minimally invasive examination

Source: Internet
Author: User

The interview questions we saw in the Forum C ++ were used for research.
1) Implement string tohex (INT) to convert a decimal to hexadecimal. (Implemented entirely using algorithms)
2) The string multiply (sting, string) function that returns the result of multiplying a large number of digits (such as more than 100 digits ). (Use algorithms completely)
3. Write a function to determine whether the computer's byte storage order is 10 ascending (little endian) or descending (bigdian)

The first question is very simple, that is, to use the decimal number to divide the number by 16 without stopping, and to accumulate all the remainder is the result. I used C ++ for simple implementation. The following code (VC ++. Net/Windows XP ):
# Include <iostream>
# Include <string>
Using namespace STD;

String tohex (INT dec)
{
Unsigned int UDEC = (Dec> = 0 )? (DEC) :(-Dec), Yushu;
Int Pos = 8;
Char hex [11];
Memset (Hex, '0', 9 );
Hex [9] = 'H ';
Hex [10] = '/0 ';

Do // calculate the remainder
{
Yushu = UDEC % 16;
Hex [pos --] = (Yushu> 9 )? (Yushu + 55) :( Yushu + 48 );
UDEC = UDEC/16;
}
While (UDEC! = 0 );
If (hex [POS + 1]> 64) // if the highest digit is a letter, add 0 before the string
Hex [POS] = '0 ';
Else
++ Pos;
If (Dec <0) // if it is a negative number, add a negative number to the front
Hex [-- POS] = '-';

Return (string (hex + POS ));
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Int I;
Cin> I;
String shex = tohex (I );
Cout <Endl <shex;
Return 0;
}
I also saw a good practice, and now I have excerpted the original code below:
// Prepared:Sutra (answers to classic questions only)
String tohex (int I)
{
String strhex, STRCH;
String table [] = {
"0", "1", "2", "3 ",
"4", "5", "5", "4 ",
"8", "9", "A", "B ",
"C", "D", "E", "F"
};
While (I ){
Strhex = table [I & 0xf] + strhex;
I >>= 4;
}
Return strhex;
}
Another method that uses recursion is as follows:
// Prepared:Cchon (Chinese character)
Char chhex = "0123456789 abcdef ";

String tohex (INT iinput)
{
String strret = "";
If (iinput <0)
{
Strret = "-";
Iinput = 0-iinput;
}

If (iinput <16)
{
Return strret + chhex [iinput];
}
Else
{
Return strret + tohex (iinput/16) + chhex [iinput % 16];
}
}
The second question is quite complicated. The multiplication of large digits cannot use atoi to multiply the string into an integer, which will overflow. It is not possible to simulate a manual computation, because it requires a large amount of memory to record intermediate results.
The better method I think of is to simulate multiplication in a computer. The concept of computer composition I just learned this semester involves the implementation of the First-digit multiplication of codes in the computer. You can easily find the specific content of this method on the Internet. Using this method to calculate the n-digit multiplication requires a 2n + 2-bit buffer. But it is quite troublesome to implement it. I have no idea whether it is actually feasible. I just want to explain my views. What are your comments.
Some cool people in that post gave the C ++ implementation and copied it to the following (I dare not read it, it's too long ):
 Xiao_wang (Xiao Wang)
# Include "stdafx. H"
# Include <iostream>
# Include <string>

Using namespace STD;

Class slong
{
Public:
Slong ()
{
_ STR = "";
}

Slong (string Str)
{
String: iterator itor = Str. Begin ();
For (; itor! = Str. End (); ++ itor)
{
If (* itor <'0' | * itor> '9 ')
Throw "the parameter is error .";
}
_ STR = STR;
}

Slong (char * s)
{
If (S = NULL)
{
_ STR = "";
Return;
}

Size_t size = strlen (s );
For (size_t I = 0; I <size; ++ I)
{
If (s [I] <'0' | s [I]> '9 ')
Throw "the parameter is error .";
}

_ STR = s;

}

Slong (const slong & SL)
{
_ STR = SL. _ STR;
}

Slong & operator = (const slong & SL)
{
If (& SL = This)
Return * this;
_ STR = SL. _ STR;
Return * this;
}

Slong operator * (slong & SL)
{
If (SL. _ Str. Length () = 0)
Return * this;

Int n = static_cast <int> (SL. _ Str. Length ());
Int M = static_cast <int> (this-> _ Str. Length ());

Int T = m + n-1; // caculate times

String: reverse_iterator itor1, itor2;
Int J = 0;
Int K = 0;

Int C = 0;
Int r = 0;
Int V = 0;

Char s [2];

String rproduct;
For (INT I = 0; I <t; ++ I)
{
V = 0;
J = 0;

Itor1 = _ Str. rbegin ();

For (; itor1! = _ Str. rend (); ++ itor1)
{
K = 0;
Itor2 = SL. _ Str. rbegin ();
For (; itor2! = SL. _ Str. rend (); ++ itor2)
{
If (J + k = I)
{
V + = ctoi (* itor1) * ctoi (* itor2 );
Break;
}
K ++;
}
J ++;
If (j> I)
Break;
}

V + = C;
R = V % 10;
C = V/10;

ITOA (R, S, 10 );
Rproduct + = s;
}

If (C! = 0)
{
ITOA (C, S, 10 );
Rproduct + = s;
}

String temp;
Temp. Resize (product. Length ());
String: reverse_iterator ritor = product. rbegin ();
String: iterator itor = temp. Begin ();
For (; itor! = Temp. End (); ++ itor)
{
* Itor = * ritor;
Ritor ++;
}

Slong product (temp );
Return product;
}

Int ctoi (char C)
{
Return C-48;
}

String Value ()
{
Return _ STR;
}

PRIVATE:
String _ STR;
};

Int _ tmain (INT argc, _ tchar * argv [])
{
String S1, S2;
Cout <"Please input First operator" <Endl;
Cin> S1;
Cout <"Please input second operator" <Endl;
Cin> S2;

Slong sl1 (S1 );
Slong sl2 (S2 );

Slong sl3 = sl1 * sl2;
Cout <"the value is:" <Endl;
Cout <sl3.value () <Endl;

Char word;
While (CIN> word)
If (WORD = 'q ')
Break;

Return 0;
}
Third, the ascending order is the sort of index data in the memory from the least meaningful to the most meaningful. Store low-level data before high-level data.
1 In the ascending order of 32 computers
0000 0000 0000 0001 0000 0000 0000
In descending order
0000 0000 0000 0000 0000 0000 0000
This problem should be solved with pointers.
Int I = 1;
Char * Pc = (char *) (& I );
PC ++;
If (* Pc> 0) in ascending order; else in descending order;
I saw another method, excerpted below:
// Prepared:Sutra (answers to classic questions only)
Bool is_little_endian ()
{
Union {
Int I;
Char C;
} UIC;
UIC. I = 1;
Return (UIC. C! = 0 );
}

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.