If Code When the while (* P) clause is used to determine the end of the string, use * P! = '\ 0.
1) questions about sizeof and const Series
A. For struct s {char a; int B} sizeof (S) = 8 because of memory alignment
B. For struct s {int A; char B} sizeof (S) = 5, memory alignment is not required here. alignment is only upward and downward, which is rarely used.
C. for int A [200] sizeof (A) = 200 * sizeof (INT) = 800 evaluate the entire array, int * A = new int [200], sizeof () = 4 pointer Evaluation
D. this type of bit domain also has a maximum of 8 positions from top to bottom, BITs = 1 + 1 (4 + 2 <8) + 1 (3) = 3. the element must be at most one char and eight characters in size.
Struct bits
{
Char A: 8;
Char B: 4;
Char C: 2;
Char D: 3;
};
2) write the binary search code.
Int bfind (int * a, int Len, int Val)
{
Int M = Len/2;
Int L = 0;
Int r = Len;
While (L! = M & R! = M)
{
If (A [m]> Val)
{
R = m;
M = (m + l)/2;
}
Else if (a [m] <Val)
{
L = m;
M = (m + r)/2;
}
Else
Return m;
}
Return-1; // not found
}
3) write the code to find the occurrence times of the substring in the parent string.
Int count1 (char * STR, char * s)
{
Char * S1;
Char * S2;
Int COUNT = 0;
While (* Str! = '\ 0 ')
{
S1 = STR;
S2 = s;
While (* S2 = * S1 & (* S2! = '\ 0') & (* S1! = '0 '))
{
S2 ++;
S1 ++;
}
If (* S2 = '\ 0 ')
Count ++;
STR ++;
}
Return count;
}
Locate the first matched substring. If the return value is S1 length, len1 indicates that no substring is found.
Size_t find (char * S1, char * S2)
{
Size_t I = 0;
Size_t len1 = strlen (S1)
Size_t len2 = strlen (S2 );
If (len1-len2 <0) return len1;
For (; I <len1-len2; I ++)
{
Size_t M = I;
For (size_t J = 0; j <len2; j ++)
{
If (S1 [m]! = S2 [J])
Break;
M ++;
}
If (j = Len)
Break;
}
Return I <len1-len2? I: len1;
}
* 4) write fast sorting or some sortAlgorithmCode
Quick sorting:
Int partition (int * a, int L, int R)
{
Int I = L-1, j = r, V = A [R];
While (1)
{
While (A [++ I] <v );
While (A [-- J]> V) if (j <= I) break;
If (I> = J)
Break;
Swap (A [I], a [J]);
}
Swap (A [I], a [R]);
Return I;
}
Void qsort (int * a, int L, int R)
{
If (L> = r) return;
Int I = partition (A, L, R );
Qsort (A, L, I-1 );
Qsort (A, I + 1, R );
}
Bubble Sorting:
Void buble (int * a, int N)
{
For (INT I = 0; I <n; I ++)
{
For (Int J = 1; j <n-I; j ++)
{
If (A [J] <A [J-1])
{
Int temp = A [J];
A [J] = A [J-1];
A [J-1] = temp;
}
}
}
}
Insert sorting:
Void insertsort (int * a, int N)
{
Int key;
For (Int J = 1; j <n; j ++)
{
Key = A [J];
For (INT I = J-1; I >=0 & A [I]> key; I --)
{
A [I + 1] = A [I];
}
A [I + 1] = key;
}
}
the number of occurrences is quite frequent
5) write an algorithm to search for all child sets output from a set.
????
* 6) Implement the strcpy function
char * strcpy (char * destination, const char * Source)
{< br> assert (destination! = NULL & source! = NULL);
char * target = destinaton;
while (* destinaton ++ = * Source ++);
return target;
}< br> frequent occurrences
* 7) Implement the strcmp function
int strcmp11 (char * l, char * r)
{< br> assert (L! = 0 & R! = 0);
while (* l = * R & * l! = '\ 0') l ++, r ++;
If (* L> * r)
return 1;
else if (* l = * r)
return 0;
return-1;
}
// Implement string flip
Void reserve (char * Str)
{
Assert (STR! = NULL );
Char * P1 = STR;
Char * P2 = str-1;
While (* ++ P2); // generally, strlen cannot be used.
P2-= 1;
While (P1 <P2)
{
Char c = * P1;
* P1 ++ = * P2;
* P2 -- = C;
}
}
Appears frequently
8) reverse order of a single-chain table
Struct list_node
{
List_node (int A, list_node * B): Data (A), next (B) // This is convenient for testing.
{}
Int data;
List_node * next;
};
void reserve (list_node * phead)
{< br> list_node * P = phead-> next;
If (P = NULL | p-> next = NULL) return; // only the first node or one node
list_node * P1 = p-> next;
P-> next = NULL;
while (P1! = NULL)
{< br> P = p1-> next;
P1-> next = phead-> next;
phead-> next = p1;
P1 = P;
}< BR >}
test Program :
List lt;
lt. phead = new list_node (0, 0);
lt. phead-> next = new list_node (1, 0);
lt. phead-> next = new list_node (2, 0);
lt. phead-> next = new list_node (3, 0);
lt. reserve ();
list_node * P = lt. phead;
while (p)
{< br> cout data P = p-> next;
}< br> 9) switch and delete nodes in the cyclic linked list.
// Bidirectional Loop
List_node * earse (list_node * node)
{
// If (node = rear) return node-> next; // you can or cannot judge the header node. It is best to add
List_node * Next = node-> next;
Next-> Prev = node-> Prev;
Node-> Prev-> next = next;
Delete node;
Retrun next;
}
// Single Cycle
List_node * earse (list_node * node)
{
// If (node = rear) return node-> next; // you can or cannot judge the header node. It is best to add
List_node * P = rear;
While (p-> next! = Node) P = p-> next;
P-> next = node-> next;
Delete node;
Retrun p-> next;
}
* 10) convert a numeric string to a number. "1234" --> 1234
Int AtoII (char * s)
{
Assert (s! = NULL );
Int num = 0;
Int temp;
While (* s> '0' & * S <'9 ')
{
Num * = 10;
Num + = * s-'0 ';
S ++;
}
Return num;
}
Appears frequently
11) Add or multiply Integers of any length.
Void bigadd (char * num, char * STR, int Len)
{
For (INT I = Len; I> 0; I --)
{
Num [I] + = STR [I];
Int J = I;
While (Num [J]> = 10)
{
Num [j --]-= 10;
Num [J] + = 1;
}
}
}
* 12) write the function to copy the memory.
Void * memcpy (void * DST, const void * SRC, unsigned int Len)
{
Register char * D;
Register char * s;
If (LEN = 0)
Return DST;
If (DST> SRC) // consider Overwriting
{
D = (char *) DST + len-1;
S = (char *) SRC + len-1;
While (LEN> = 4) // expand the loop to improve execution efficiency
{
* D -- = * s --;
* D -- = * s --;
* D -- = * s --;
* D -- = * s --;
Len-= 4;
}
While (Len --)
{
* D -- = * s --;
}
}
Else if (DST <SRC)
{
D = (char *) DST;
S = (char *) SRC;
While (LEN> = 4)
{
* D ++ = * s ++;
* D ++ = * s ++;
* D ++ = * s ++;
* D ++ = * s ++;
Len-= 4;
}
While (Len --)
{
* D ++ = * s ++;
}
}
Return DST;
}
Appears frequently
13 what is the purpose of static? (Please specify at least two types)
1. restrict the scope of Variables
2. Set the storage domain of the variable to be visible only in the variable-specific source file.
Frequently Asked
14. What is the difference between reference and pointer?
1) The reference must be initialized and the pointer is not required.
2) The reference cannot be changed after initialization. the pointer can change the object.
3) There is no reference pointing to a null value, but there is a pointer pointing to a null value.
4) Reload operators can use references to perform string test.
Frequently Asked
15. Are global variables and local variables different in memory? If so, what is the difference?
Global variables are stored in the Global static storage area, and local variables are stored in the stack.
16. What is a balanced binary tree?
Both left and right Subtrees are balanced binary trees, and the absolute value of the depth difference between left and right Subtrees is not greater than 1.
17. What functions cannot be declared as virtual functions?
Constructor
13. What is the time complexity of the bubble sort algorithm?
O (N ^ 2)
18. Write the if statement for comparing float X with "zero value.
If (x> 0.000001 & x <-0.000001)
This is all antique. I'm afraid it was before 8086. you can use a command to compare the compilation. since want to test accuracy, it is not 0, such as 0.00002, if (x-0.00002> 0.000001 & x-0.0002 <-0.000001)
19. The user inputs the M and N values. The number of sequential loops starts from 1 to n. This value is output on every count to m until all are output. Write the C program.
Cyclic linked list, with the remainder operation
// This write is not very good. If it is set to 1, it indicates it has been accessed.
Void Joe (int n, int m)
{
Int * A = new int [N];
Int I = 0;
Int Pos = 0;
While (I <n)
{
Int c = m;
Pos % = N;
While (c)
{
C --;
While (A [POS] = 1)
{
Pos ++;
Pos % = N;
}
Pos ++;
Pos % = N;
}
A [pos-1] = 1;
Cout <POS <"";
I ++;
}
}
20. The following descriptions and definitions are provided:
Typedef Union {long I; int K [5]; char C;} date; // sizeof (INT) * 5 = 20
Struct data {int cat; Date cow; double dog;} too; // 4 + 20 + 8 = 32
Date Max;
The execution result of the statement printf ("% d", sizeof (struct date) + sizeof (max); is: ___ 52 ____
21. Use a pointer to display the "abcd1234efgh" character string
// Do not use strlen to evaluate the string length.
The Code is as follows:
Char str123 [] = "abcd1234efgh ";
Char * P1 = str123;
Char * P2 = str123-1;
While (* ++ P2 );
P2-= 1;
While (P1 <P2)
{
Char c = * P1;
* P1 ++ = * P2;
* P2 -- = C;
}
22. There are 1 billion floating point numbers, and the maximum number is 10000. If the standard library is used, you can only write the heap function by yourself.
Vector <float> bigs (random, 0 );
Vector <float >:: iterator it;
For (IT = Bigs. Begin (); it! = Bigs. End (); It ++)
{
* It = (float) rand ()/7; // The data is simulated with a random number.
}
Cout <Bigs. Size () <Endl;
Make_heap (Bigs. Begin (), Bigs. End (), greater <float> ());
Float ff;
Time_t T1, T2;
Time (& T1 );
For (INT I = 0; I <1000000000; I ++)
{
FF = (float) rand ()/7;
If (FF> bigs [0])
{
Pop_heap (Bigs. Begin (), Bigs. End (), greater <float> ());
Bigs. pop_back ();
Bigs. push_back (ff );
Push_heap (Bigs. Begin (), Bigs. End (), greater <float> ());
}
}
Time (& T2 );
Cout <(long) (t2-t1) <Endl;