חומר טכני לראיונות עבודה PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

.

1 :
""
" "

.2 :

"

...
..


..



'
...'



'

'.
-


'

..

.3 :





.4 ...

( ).barakman@yahoo.com .

""

. ,
, , .
. , ,
, .
: (
) . , , .

" "

.
, ( Low Level- ) ( High Level- ).
, .
, ( ).
.
, . ,
. .
, ,
( ) . ( ,)
. , (
) .


.1
.2
.3
.4
.5
.6
.7
.8

... :
... :
... :
... :
... :
... :
... :
... :

. ,
. , .
, ...

()
.1 , .
. .
.2 . .

" ()
.3
.4

.5
.6

. , .
. .
, . ?
'Task . Task , ,
"" Running ( Pending) . Running
, Pending ( ) .
Task ( , , Running
, Pending ) .
Task- (.)Running, Pending, Not-Alive
.
static ?C- static?C++-

( )
.7 N . , .
. ,Rand .
.8 , () .
( )Instance ( ) .
'Instance ?

... ()
.9 100 . . .
. . 100 . ?
.10 , .
. . ,
. , . .
.11 , . .
( ) .
?
.12 100 . ,
( ) . ( "").
.

.. ()
.13 - . . .
. . .
,
. , . .Rand
, N , .
.14 N . . ,Rand
(' , .)7

()
. , ...

()
.15 ? ?
.16 ( C- ,printf)?
.17 . , (
) . , .
, , .
N.
.18 , .
.19 String ,Set
Set- .String- . .ASCII
, ?
.20 . ?
.
.21 . + , 12.
3 . :
4 .
255 .
16 .
. ? .
. :
K1 P1 L1.
K2 P2 L2.
...
Kn Pn Ln.
.22 ( 2 ) . , .
. ,
.

.. ( )
.23 ?
.24 ?
.25 Copy Constructor ?
.26 ?V-Table-
.27 Singleton ?
.28 ?Mutex-
.29 ?

()
.30 C- .
.31 C- (.)FIFO
.32 M . G1 ,S1 ,
. G2 ,S2 ,
. . ) Open(i
,i ) Close(i ,i ) Test(i .i
.33 ?Mutex-
.34 Process?Thread-
.35 . , .
. .
. .

( )
.36 . , , .
. ,
.
.37 ( .)XY ,
, ?

( )
.38 .int array[] = {} :
.
. ( )Image- ?
.39 0 ,N-1 . ,Rand .
.40 ,N N . ,
.
. .

' ( )
.41 . ( ) , (
) . , . .
.
.42 . , ( , , ).
, , .
.43 .1MB ,
. :
void* malloc() 8 , .
void free(void* p) 8 , .
, 8 .

...' ( )
.44 C- C++- main ?
.45 , .
( ) ,
?
.46 memcopy .
. . ?
. , ?
.47 Context Switch 'Task , ?
.48 'Task . , .a 'Task-
, .b
. .a=b ?a ,
?
. .b=a ?b ,
?
.49 :
)char* strcat(char* first, char* last
{
;]char buff[100
;)sprintf(buff,"%s%s",first,last
;return buff
}
?
.50 ( )ISR ?

( )
.51 - . ( )data (.)next
, . (.O)1
.52 - .N . (.O)N

()
...

( )
. , ,
. ...

' ()
C .53 :
OS_small_malloc(int sz) 512 .
OS_large_malloc(int sz) 512 .
OS_free(void* p, int sz) sz p.
. ) .void* malloc(int sz , 16.
. ) .void free(void* p .
. ,free .
C .54 .Sleep(int seconds) : ," 'Timer-:
) ,void CreateTimer(void (*func)(), int seconds .Timer
)( ,void StartTimers 'Timer- .
Timer , .Callback-
'Timer-:
;)CreateTimer(func1,3
;)CreateTimer(func2,7
;)CreateTimer(func3,10
;)(StartTimers
( ):
3 .func1-
4 .func2-
3 .func3-

()
.55 :
)void func(int* x
{
;))x = (int*)malloc(sizeof(int
;*x = 17
}
, ?
)(void main
{
;int y = 42
;)func(&y
;)printf("%d",y
}

'. ( )
.56 :
)(void main
{
;"const char* arr = "abcde
;)char* ptr = (char*)malloc(strlen(arr)+1
)for (int i=0; i<strlen(arr)+1; i++
;]*ptr++ = arr[i
;)free(ptr
}
?
.57 'Thread . , :
)(int func
{
;int a = 1
;a++
;return a
}
Thread- . , .Context Switch
Thread- . , .Context Switch
a ?
.58 15 ( 30 ,) . , .
( , ) ?

- ()
.59 " "Stack- ?
.60 .

( )
.61 ?delete this
.62 C++- .By Reference ?
.63 ,A B C- , D . A ""
,D ?

()
.64 ( ) ,
( , )Transpose .
.65 A 'Msg H .Msg/MilliSec H 'Ack A .
Msg . Ack H Msg- A ,
'Ack- 'Msg- . Msg A , H
Ack . A ,H ,B .
,B .

' ( )
.66 'Task A : B , C , .
A C- , ( , A- ,
C- ,) . C , ,
B , . , ?

()
.67 32 . .1
?
.68 'Task . , Log
'Task- . 'Task .

.. ( )
.69 , 8KB .60KB , :
Load(loc,buf) .1 1KB loc buf (.)250 Cycles
Store(loc,buf) .2 1KB buf loc (.)250 Cycles
Process(buf) .3 1KB buf (.)600 Cycles
1 2- ,DMA ( 3 ).
, 60KB ,
( ).
.70 :
)long func(short x
{
;long res = 0
;]static short array[20000
;]extern short const_array[20000
)for (int i=19999; i>0; i--
;]array[i] = array[i-1
;array[0] = x
)for (int j=0; j<20000; j++
;]res += (long)array[j]*const_array[j
;return res
}
'Cycles , ( = ,* ,- ,+') Cycle.
. , ?
. :
( i = (i+1)%n i 1- .)n
inc_mod(i,n) .1
( i = (i-1+n)%n i 1- .)n
dec_mod(i,n) .2
Cycle . , .


.71 , . ,
( ) . . ,
, , .
.72 40" . ,
( ) 80"?
.73 . ?
.74 ( x>y ,a<b :') .
.
.75 N . .
, : .
, .
.
( ) . .
.76 .n [ ]m,...,m+n+1 , ,
. , ,m=2 ,n=6 ,2,4,6,7,8,9 .3,5
. ?
( , ).
.77 4 ( On- .)Off-
, . .
On- .Off- ,
( ) . ,
( , ) . , ,
. , . :
, .
.78 . , 3 3 , 3 3,
1-9 ,
, .
.79 .} { ,] [ ,) ( :
. .
. , , .| | :
.80 2^N 2^N .
( ).

.81 . .
?
.82 100 . . " ,
.
. ( ) ,
.
. ?
. .
. .
.83 , ,
.5-
.84 "" - , .
.85 : ,
() . , ""
( ) . , "" , .
, . .

()
.86 , ,2 O)1(- ?
.87 ? "
?
.88 M .N . .
.89 .XY
.[(x1,y1) , (x2,y2)] :
.
, 4 .
.90 Constructor , Destructor ?
.91 . ,
( C++-?)A* a = new A ,
.92 C++- .virtual
.93 N . , 1N- .
.94 M .N .
.
.95 malloc , free ,
, . " .
, :C
void* MyMalloc(int size) .
void MyFree(void* ptr) .
.96 Real-Time N ('Buffer) . :
)( GetBuffer FreeBuffer(buffer) , .
" . .
.97 8 4 . Rx:
INC Rx Rx-.1-
DEC Rx Rx-.1-
CLR Rx .Rx-
JUMP Rx LABEL ,LABEL- Rx- .
. " .R3R2*R1
?
. ( ,
?)2-
.98 Prog .Data Prog- Data-
. Data- 8 , ( 0) .
,Prog- .
.99 , .x x
,)x++( 1- x .)x--( 1- x , ,0-
m n ().
. x ?
. , ( ) . x?

()
.100 ? ?
.101 , .
?
, ( ) .
.102 . .
, . ,
. ?
.103 .
( )?
.104 N ,1 N N- .
.105 N , [ .]1 ,N-1 .
, .
.106 ,7- .
.107 30 12 12 . 12 12 . ,
, 30 12 .
, . 42
, 40 ?
12

12
30

.108 , .
( )?
.109 ( .)unsigned int ,1- .
.110 N . ( ) .
.111 O(log(N))- ," .
, : }1,2,3,4,5{- .}3,4,5,1,2{- (
) , .
.112 XOR NAND.
.113 A B 100 . A 5 .B
, , A 5 .B
, ? ( ).
.114 30 X 30 .Y :
" 30 , , " .
, X ( Y) . ,
. ,
. ?

()
.115 N ] .[0N-1 .
.116 . 0 , 2 ,1 3.
X , X- , .
,X X- .4- .
.117 0 100?
C++.118 :
. (?)Type Array[10] ,
. (?)Type Array=new Type[10] ,
C++.119 :
T* p=0; delete p; .
T* p=new T[10]; delete p; .
C++.120 RTTI ?
.121 :
f
. .a, b, c, e, f
c a
. a, b, c.
b

.122 A B- C .C = min(A,B), D = max(A,B) :D-


, ,
. ( ).
MIN

MAX

X
Y
Z
W

C
D

A
B

.123 . D-Flip-Flop ,
( , ).
.124 A .B- A , 60 .
B , 30 .
( , 55 ,A 5- ).
45 ? A ,C- C
60 ( .)A 45
?
.125 :
. , .
. , .
. , .

:
," .
" :
, ( )#define, typedef, template .
,Preprocessing- " .
.
, " .
, ( )Memory Address Space .
, .
, , Offset- .
, :
.1 (.)Data Section
.2 (.)Code Section
.3 (.)Stack
, :
)Instruction Pointer( IP- , .
)Stack Pointer( SP- .
, , :
, .
, .
:
.1 .
.2 .
.3 .
.4 ( ).
Scope- . .
, ( ).
.
, :SP-
, ( )SP- .
, .
, ( ).
, ( ).
, , { : ,} { , }.
, , .
, , ( ).
:
, .
, .
,
. ( ).

:
:

" " ( " ,


)

( )*



( )


* ( ).
( ):


,


,

,


, ( , )
,

) ( void MyClass::Func1 : MyClass* myPtr


//Get the address of the instance
//Get the address of the function
//Push the address of the instance into the stack
//Push the address of the function into the stack
//Save some registers and jump to the beginning of the function

Ax = myPtr
Bx = MyClass::Func1
Push Ax
Push Bx
CallF

) ( void MyClass::Func2 : MyClass* myPtr


//Get the address of the instance
//Get the address of the instance's V-Table
//Add the number of the function in its class
//Get the address of the appropriate function
//Push the address of the instance into the stack
//Push the address of the function into the stack
//Save some registers and jump to the beginning of the function

Ax = myPtr
Bx = myPtr->__vfptr
Cx = Bx + MyClass::Func2_Num
Dx = *Cx
Push Ax
Push Dx
CallF

, , ( Read
V-Table- )V-Table- Add ( ).

:
OOD/OOP ,
( ).
:
.1 (.)Encapsulation
.2 (.)Derivation
, .
" " , :
.1 ().
.2 ().
,
. , (
) , ( ) .
LIB ,DLL .
, .
A :B
.1 B ,A ( .)B has an A ,
.2 B ,A ( .)B is an A ,
, . ,
A Play ,Stop- B .Rewind A
, , . , B ,
A .B
, , .
, A , B C- :
, - .
, .
, , .
, .A
,A B C- ( ).
,Polymorphism .
, , .
:
, ( , ).
, .DLL

:
:
.
, .
, .
( ).
(:)Ram
.
, .
, .
, :
.1( Cache- ,)L1 .
.2 ( Ram- ,)L2 .
(:)Hard Disk
.
, .
, .
( ).
( ) , .
, .
(.)MMU
,
. ,
:
, .Cache-
( ,)Miss .Ram-
( ,)Page Fault .Hard Disk-
, , ""
( ,)Last Recently Used ," . , , .
, , .
.
, .
"" , , MMU-
Base Address- " .
, ,
( , Base Address- ).

:
( )Process . ,
, . ,
. ,
, .
( ,)Multi Tasking .
, , .
( ) ,
( ) . ( ,)Context Switch
.
( )Thread , .
, . , ( )
. .
, .
, , .
, , , ( )
. ,
( ) , .
( ) ,
. , (.)Critical Section
, , .
, ( .)Mutual Exclusion ,
"" ( .)Deadlock ,
( .)Starvation
:
Disable/Enable Interrupts .1 ( ,
, ) . ,
, ( ).
Busy Wait .2 x=1 ( ) . :
.
x ,0- .
x ,0- , x.1-
, "" x .0
, .
Mutex .3 ,Mtx :
Mtx.Wait( ) Mtx , . , .
Mtx.Signal( ) Mtx , .
) ( Mtx.Wait , ) ( Mtx.Signal.
Mtx , " . ,
. ,Busy Wait-
" "" . , " .
, " " .
: Mtx , ,
( .)Block , ,Mutex-
, . ,
( .)Wakeup

1
:
.1 A B .
.2 A , .
.3 B , .
.4 A ,B .
.5 " .
.6 ( .)2
, ...
) ,O(N ).O(1

2
-:
)void Node::Swap(Node* pNext
{
)if (pNext != NULL
{
;)pNext->Swap(pNext->m_pNext
;pNext->m_pNext = this
}
}
)(void List::Reverse
{
)if (m_pHead != NULL
{
;))(m_pHead->Swap(m_pHead->GetNext
;)m_pHead->SetNext(NULL
}
;Node* pTemp = m_pHead
;m_pHead = m_pTail
;m_pTail = pTemp
}

-:
)(void Node::Swap
{
)if (m_pNext != NULL
{
;)(m_pNext->Swap
;m_pNext->m_pNext = this
}
;m_pPrev = m_pNext
}
)(void List::Reverse
{
)if (m_pHead != NULL
{
;)(m_pHead->Swap
;)m_pHead->SetNext(NULL
}
;Node* pTemp = m_pHead
;m_pHead = m_pTail
;m_pTail = pTemp
}

3
: A B .
,f .0-
, .
A :
,f(B)=0 . ) f(A.1-
,f(B)=1 . 1 , ) f(A ).f(B
B :
,f(A)=0 . ) f(B.1-
,f(A)=1 . 1 , ) f(A ).f(B
( ) .

4
,Task- Task- .
,Task- :
[ ] [ ].
,Task- . , .
End Time

Current Time
Pending

Running

Pending

Begin Time
Running

Pending

Running

)STATE Task::GetState(iCurrentTime
{
)if (m_iBeginTime<=iCurrentTime && iCurrentTime<=m_iEndTime
{
;)int iRem=(iCurrentTime-m_iBeginTime)%(m_iRunTime+m_iPendTime
)if (iRem<m_iRunTime
;return RUNNING
else
;return PENDING
}
else
;return NOT_ALIVE
}

, Task- .
, iRem<m_iRunTime .iRem>=m_iPendTime

5
, .
Constructor ,1- Destructor- .1-

6
static "" , :
( ) , ( )
( ) . ,
( ) . Scope- .
, Data Section- ( ,
, Stack- ).
( ) (
) . , (
) . Scope- .
( , ,
).
( 3 : ).

7
N.
:i
.1 , N-i .
.2 , .i-
.3 , ,)N-i(-" .
)]void RandReorder(Song* inputArray[N], Song* outputArray[N
{
)for (int i=0; i<N; i++
{
;)int index = Rand()%(N-i
;]outputArray[i] = inputArray[index
;)Swap(inputArray,index,N-1-i
}
}

:
- , ) (1,N .N
:
.1 Rand" . .P
.2 ( P ) , P , N.
.3 . index = (index + P) % N :
( ) , .
:
. ( , ).
. , N (
) . , . :
= < .N
N = N = 1 * 2 * * N.

8
, :
. Constructor- protected- . .
. Constructor- private- , ,
( ,Singleton- ).

9
: -.
- : ...16 ,9 ,4 ,1

10
, , ( ,
).
:
. .
. , ( ).

11
, ( ) .
:
= A = 1/2
= B = 1/1
= C = 1/2
= D = 9/19
:
= A*B+C*D = 14/19

12
, .N
N , ( k=N :)k=1
.1 k . , k-1:
. : .
, .
.2 , k 1- .
:
.N
o , 1 ( N-1 ).
o" , N ( N-1 , ).
.N+N-1
o , N+1 ( N+N-2 ).
o" , N ( N-2 , ).
, .N
N 100:
" N ,100 N + N-1 + N-2 + + 1 100 :
(N+1)*N/2 100 N^2+N200 0 N [-1+(1+800)]/2 N 13.65 N = 14
, .99 ,95 ,90 ,84 ,77 ,69 ,60 ,50 ,39 ,27 ,14 :

13
1 ( , ).
2 ,1/2 .Rand()%2
3 ,1/3 .Rand()%3
X ,1/X .Rand()%X
N , :
,N=1 1 .100%
,N=2 2 ,50% 1 .50%-
N=k :N=k+1
o k , .
.1/k
o , k+1 ) ,1/(k+1
k- ) ,1-1/(k+1 ).k/(k+1
o k , ,
,[k/(k+1)]/k ).1/(k+1
o k+1 k- .
).1/(k+1

14
.7

15
( ) Polymorphism:C++-
, .
, .
, .
, ( , ).
( 3 : ).

16
.C-
,C++- Default .
. void func(int x, ) :
, .
( ,)... .
( ) , .
, :
.
4 8 , .
4 4 , 8.
, , .
, , .
, :
;short y = 1000
;)"int sum = func(1,y,5000,"abc
:
) int func(char x,
{
short y = (short)((int*)&x+1)[0]; //y = 1000
int
z = (int )((int*)&x+2)[0]; //z = 5000
"char* s = (char*)((int*)&x+3)[0]; //s[02] = "abc
;]return x+y+z+s[0
//1+1000+5000+'a' = 6098
}
( va_list, va_start, va_end, va_arg .)stdarg.h
:
, "" .
printf , ( ):
' '% .
'%'-.
, ,
("") . , ,
" " ( 4 8).
:
. ): printf("%d %d",5
"" ( ).
. ): printf("%d %f",1.2,3.4
8 Floating-Point .
4- .Fixed-Point
8- .Floating-Point
4- .
,
"" ( ") . .

17
, Busy Wait :
.1 ,y=1 .
.2 ,x=0 Busy Wait" " .
)for (int y=1; ; y++
{

;)(DisableInterrupts
x++; //Load x, Inc, Store x
;)(EnableInterrupts
} { )while (x < y*2

}
:
" " ,y x ,1- "" .
" " ,y x ,1- "" .
:
. Busy Wait- , ,
( x ).
. x ," ,
(" ).
N Busy Wait , ,
,x .N-
, N , Priority Inversion :
)while (true
{

;)(DisableInterrupts
)if (x < N-1
{
;)(DecreaseThisThreadPriority
;x++
}
else
{
;)(IncreaseAllThreadsPriority
;x = 0
}
;)(EnableInterrupts

}
:
, " " , . ,
( ).
" " , . ,
( ).

18
n .S[1n] :
, ] S[1n :
: , ().
:x
.1 ] ,S[x+1n" .
.2 !) (n-x , n-x.
.3 , ) S(x .
.4 n-x+1 ).S(x
.5 !) (n-x+1 , n-x+1.
)List Permute(String string
{
)if (string.length == 0
;)return EmptyStrings(1
;))List prevList = Permute(SubString(string,1,string.length
;)List nextList = EmptyStrings(string.length*prevList.length
)for (int i=0; i<prevList.length; i++
{
)for (int j=0; j<string.length; j++
{
;)nextList[i*string.length+j] += SubString(prevList[i],0,j
;]nextList[i*string.length+j] += string[0
;)nextList[i*string.length+j] += SubString(prevList[i],j,string.length-1
}
}
;return nextList
}
:
. ) EmptyStrings(n n .
. ) SubString(str,i,j ,str ( i) ( j ).

19
Set-:String-
.1 256 ( .)ASCII
.2 ,Set true .
.3 ,String .true
)int FirstIndex(const char* string, const char* set
{
;}bool array[256] = {false
)for (int i=0; i<strlen(set); i++
;array[set[i]] = true
)for (int j=0; j<strlen(string); j++
)if (array[string[j]] == true
;return j
;return -1
}
. .
, , .
Hash Table- ( Hashing) .
Hash Table- , .
, Hashing ( ) .,
.
: String- , .Set-
, .

20
" ,Hash Table- .
, ,Hash Table , .
.
, : .
( , , ) Hash Table-.
.Hash Table-
, Hash Table- () , .
, ,
( ).
" , :
. ,18
. , , .
. , .
, . -
, . ,
, .

21
" 4096 .2^12 = 4096 : , 3.
4 , 2048 .4 * 2^)3*3( = 2048 :
255 , 2040 .255 * 2^)1*3( = 2040 :
16 , 16 .16 * 2^)0*3( = 16 :
" 4104 , .
: ]) [Ki * 2^(Pi*Li .

22
:
.1 ( Pre-Order , , ) , .
.2 .
.3 , ( ).
.4 , .
.5 , .
.6 , .
, ( 0 ) .
:
.1 , .
.2 , , .
.3 , , .
,Pre-Order .

23
, .
, () .

24
:
. .
. ( ).
:
. ( ).
. .

25
Copy Constructor , .By Reference Constructor-
( ,)A a2=a1 ,
( By Value , Copy Constructor- ).
Copy Constructor- " ,
. , Copy Constructor- ( ).
,
. , , .

26
:
, .
, .
,
, . ,
. :V-Table
, .
,V-Table .
, V-Table.
, ,
( ) V-Table- , .
V-Table- , ( ).
( 3 : ).

27
Singleton ( ,)Design Pattern .
h-:
class MyClass
{
public:
;)(static MyClass* CreateInstance
private:
;)(MyClass
;static MyClass* m_pInstance
;}
cpp-:
;MyClass* MyClass::m_pInstance = NULL
)(MyClass* MyClass::CreateInstance
{
)if (m_pInstance == NULL
;)(m_pInstance = new MyClass
;return m_pInstance
}
:
. Constructor- :private-
, .
. :
, .

28
, Mutex- :
. ,
Mutex . ( ,)Mutual Exclusion
. , ,
( ) ,
( )Context Switch .
,
Mutex . .
Mutex .
.

29
:
" , .
, Pipe .Message Box
:
" .
" .
, .
, .
( ) , ,
. ,
( , ') , .
( 3 : ).

30
:
bool strcmp(const char* str1, const char* str2)
{
int i;
for (i=0; str1[i]!=0 && str2[i]!=0; i++)
if (str1[i] != str2[i])
return false;
return str1[i] == str2[i];
}

31
:)FIFO(
void Create(Queue* pQueue);
void Destroy(Queue* pQueue);
bool IsFull(const Queue* pQueue);
bool IsEmpty(const Queue* pQueue);
bool Insert(Queue* pQueue, Item* pItem);
bool Extract(Queue* pQueue, Item** pItem);

33
Mutex , :
.
Mutex .

34
( )Process (:)Thread
. .
.
. .
.
. .
.
. .
.
( 3 : ).

35
:
)bool check(char c
{
for (int i=0; i<N; i++) //Push i, Push N, Compare, Push i, Inc, Pop i
if (arr[i] == c) //Push arr, Push i, Add, Load, Push c, Compare
;return true
;return false
}
i :
)bool check(char c
{
;char* end = arr+N
)for (char* ptr=arr; ptr!=end; ptr++
)"if (*ptr == c) //Push ptr, Load, Push c, Compare (removed "Push i, Add
;return true
;return false
}
i :
)bool check(char c
{
;]char x = arr[N-1
)if (x == c
;return true
;arr[N-1] = c
)for (int i=0; ; i++
)"//Push i, Inc, Pop i (removed "Push i, Push N, Compare
)if (arr[i] == c
;break
;arr[N-1] = x
;return i<N-1
}
.

36
, :
.X-
) ,(0,n .Y-
) ,(p,q .XY
. ) ,(x,0 .
, ".f(x) = (x+n) + ((p-x)+q) :
, .
, x .
, "" :
)(0,n
)(p,q

)(p,-q
)(0,-n
"" .
, .
:
n = a*0+b , -q = a*p+b b = n , a = -(q+n)/p y = -(q+n)/p*x+n
:
]y = -(q+n)/p*x+n , y = 0 -(q+n)/p*x+n = 0 x = n*p/(q+n) [n*p/(q+n) , 0

37
. - .
, , :
. , ,
. , .
. ,X- (
X- X- ).
. , (
).
. - , , ,
.
, , :

38
( )Image- .
, ( ) .
, Data Section- . ,
. ,
, .

39
0 ( N-1 ) , N.
i ,j i- :j-
)for (i=0; i<N; i++
;array[i] = i
)for (i=0; i<N; i++
;)Swap(array,i,Rand()%N

40
( )
:
.1 , .
.2 , (
) , . ,
:

12

10

10

11

12

2
3

13

4
5

13

.3 , .
) ,O(N^2 ).O(N^2

42
:
,a ).N(a
,b<c N(b)-.N(c)-
x:y-
.N(x)- ,N(y)- y- .x-
, .N(y)- ,N(x)- x- .y-
, .

43
: 8 , 8.
: . , "".
:
" " . , .
,1MB 20 , 8.
" ," . " ," .
:
,dword* listHead .
- , ( ).
:
)(void* malloc
{
;void* p = (void*)listHead
;)listHead = (dword*)(*listHead
;return p
}
:
)void free(void* p
{
;*((dword*)p) = listHead
;listHead = (dword*)p
}

44
, main.
( ,)int x=5 , . ""
( )Image- .
( ,)int z=x+y , main.
,Constructor .
, C- ,C++- .main

45
:
( word :)dword
o Little Endian LSB- .MSB-
o Big Endian MSB- .LSB-
( Bits-Fields }:)struct Byte{uint8 b1:1,b2:1,b8:1
o LSB- .MSB-
o MSB- .LSB-
.
, ( .)char, short, int, long

46
:
)void memcopy(void* dst, void* src, int len
{
)for (int i=0; i<len; i++
;]((byte*)dst)[i] = ((byte*)src)[i
}
:
. ( byte- word , ,)dword .
( )len , ( src )dst- Aligned
, Unaligned Load/Store .
. src dst- ,i .
, ] [Push ptr, Push i, Add, Load ] ,[Push ptr, Load, Load
".
, :
, ( ).
src dst- len ( " ).
] src[0len-1 dst[0len-1]- ( , ).

47
Context Switch 'Task .

48
,a=b ,a
(" Task ) . ,a
( )a=b , . :
. (.)Disable/Enable Interrupts
. (.)Busy Wait
. (.)Mutex
:b=a , a .
( 3 : ).

49
:
. .buff
( ) , " " ,
( "").
. buff " first .last-
,buff .
" . char* buff = new char[strlen(first)+strlen(last)+1] :

50
, :
, Instruction Pointer- .
, Instruction Pointer- .
, RetI .RetF

51
, .
,O)1(- , O)1(- .
- ,O)1(- .
, .
)void Cut(Node* curr
{
;)Node temp = *(curr->next
;)free(curr->next
;*curr = temp
}
:
"" , (
"") . ,"" .

52
- . , :
.1 ( ).
.2 , , .
.3 , . ,NULL- .
)bool SearchLoop(Node* head
{
;Node temp
)while (head!=&temp && head!=NULL
{
;Node* next = head->next
;head->next = &temp
;head = next
}
;return head == &temp
}
:
. (.O)N
. , .84

55
,42 x func ,
( main .)y
, func ( )x .17
, "" ( ).

56
, :
, " .ptr
, .ptr
, " .ptr
, .

57
a ,func :Thread
Thread- ,1 Thread- .2

58
:
, 29 . , .1/29
, .1/29
:
2 ,30 " 30 ,(30!/2!/28!) "2 .15*29
,15 ) ,15/(15*29 .1/29

59
" "Stack-:
. Stack- ,
.. for (int i=0; i<STACK_SIZE; i++) SP[i]=0x12345678 :
. Stack Pointer- ,
.. if (MaxSP<SP) MaxSP=SP :

61
,delete this ,
. , .
, .

62
By Reference "" By Value :By Pointer
"" ( By Value ).
"" ( By Pointer ).
:
. Casting "" , Casting ,
. , .
. Copy Constructor .By Reference
By Value ( Copy Constructor- ).
. Template By Reference
.By Value 'Template ( "" "").

63
D ,A B C- :
class B : virtual public A
class C : virtual public A
D ,A B C- :
class B : public A
class C : public A
D ,A :
B::var , B::func()
C::var , C::func()
" var , func- ,A B.C-

64
, :
)void Transpose(int** matrix, int n
{
)for (int i=0; i<n; i++
{
)for (int j=i; j<n; j++
{
;]int temp = matrix[i][j
;]matrix[i][j] = matrix[j][i
;matrix[j][i] = temp
}
}
}

65
:
A Msg , 1000-'Ack- H , .
, B:
FIFO 1000'Msg- A.
Msg , H Ack .
A Msg , B:
Msg- .
.
Msg- .
H Ack , B:
Msg- Ack-.
Msg- Ack.
Msg ,O(1)- B:
Hash Table Msg .O(1)-
: B 'Msg- 'Ack- .

66
, A- C- , B- C-
. B , A- , .
:Priority Inversion
C- , , B
.A C , .

67
,1 32:
)int FindThird1(int reg
{
;int count = 0
)for (int i=0; i<32; i++
{
;count += (reg>>i)&1
)if (count == 3
;return i
}
;return -1
}
- :
NumOf1s[256] 8 , .1
First1[256] 8 , .1
Second1[256] 8 , .1
Third1[256] 8 , .1
( 1 "):
)int FindThird1Fast(int reg
{
;int count = 0
;}int* Locations[4] = {NULL, First1, Second1, Third1
)for (int n=0; n<4; n++
{
;))int unit = (byte)(reg>>(8*n
)if (count + NumOf1s[unit] < 3
;]count += NumOf1s[unit
else
;]return 8*n+Locations[3-count][unit
}
;return -1
}
:
, ( ) .1
3- , ( ) .1

68
'Task- , 'Task-.
'Task , .
, :X
.1 .1-
.2 Task- Task .X
.3 'Task- Task- X Task- X-1.
( 'Task- :)N
2 , .O(log(N))-
.O(1)-
" )).O(N*log(N

71
.
, .

72
:
S .
V .
T1 .
T2 .
:
()
T1 = S / 40
( )
V = S / (T1+T2)
:
T2 = S / V - S / 40
:
V = 80
:
T2 = S / 80 - S / 40 = 0
( " ").

73
A .B :


A=A^B
A=A+B
B=A^B
B=AB
A=A^B
A=AB
: , .

74
:
.1 , , X ,Y- X>Y
X- .Y- , .X>Y>Z><X
, ( ).
.2 :
. , (.)DFS
. "" ( ) , .
: , .

75
1 ,N- .
, , .
,N- .
,N .
N-1 , .

76
, ( ).
, ( ):
.1 ( S0 P0-).
.2 m- n- , ].[m,,m+n+1
.3 ( S P-):
. P = [m,,m+n+1] P0 , S = [m,,m+n+1] S0
.4 .x1,x2 x1 + x2 = S : . x1 * x2 = P
.5 , . x1,x2 = [-S (S-4P)] / -2 :
: ( .)1

77
4 2- ( On ,)Off " 16
, 2- . 2 , 14
( ):
A
0101, 1010
On- Off-
B
0011, 0110, 1100, 1001
On- Off-
C
0001, 0010, 0100, 1000
On- Off-
D
0111, 1110, 1101, 1011
On- Off-
N
0000, 1111

, C ,B ,A D- :N
A ,X .N
, , C ,B D- A .N
B ,Y A.N-
, , C D- B ,A .N
C D- ,Z B ,A .N
" , ( ):
X
^ 0101

Y
^ 0011

Z
^ 0001

( ) , :
X
Y
X
Z
X
Y
X

A
N
B
B
A, N
N
C
C, D
C, D
C, D
A, B, N B, N
A, N
N
D
C, D
C, D
C, D
A, B, N B, N
A, N
N

78
9 , 9.
, 1 9 :
1 .1
2 .4
3 .7
4 .2
5 .5
6 .8
7 .3
8 .6
9 .9
Table[i][j] = ( ( i * 6 - i / 3 + j ) % 9 ) + 1 :
Table : 9 9 i,j , 08-

9
6
3
8
5
2
7
4
1

8
5
2
7
4
1
6
3
9

7
4
1
6
3
9
5
2
8

6
3
9
5
2
8
4
1
7

5
2
8
4
1
7
3
9
6

4
1
7
3
9
6
2
8
5

3
9
6
2
8
5
1
7
4

2
8
5
1
7
4
9
6
3

1
7
4
9
6
3
8
5
2

79
.} { ,] [ ,) ( :
:
. .} ,] ,) :
. .{ ,[ ,( :
:
:
o , ( .)Push
o , . , .
, ( )Pop .
, .
, . , . ,
.
, ,| | : ,
Push .Pop , .
, , .

80
,N=1 22 ,
,N=2 44 ,
2^N 2^N , .N=k
2^N 2^N , :N=k+1
, 2^k 2^k .
, , :
o 1 , .
o 2 , .
o 3 , .
o 4 , .
,
, ) 2^(k+1) 2^(k+1 .
1
, ,N=2 2 :N=3

3
4
2^n 2^n :
A1 = 1
An+1 = 4An + 1

81
:
)int CountOnes(byte x
{
))#define BITS_PER_BYTE (8*sizeof(byte
;int res = 0
)for (int i=0; i<BITS_PER_BYTE; i++
;res += (x>>i)&1
;return res
}
, (:)Hash Table
)int CountOnesFast(byte x
{
)#define VALS_PER_BYTE (1<<BITS_PER_BYTE
;static bool first_time = true
;}static int table[VALS_PER_BYTE] = {0
)if (first_time == true
{
)for (int i=0; i<VALS_PER_BYTE; i++
;)table[i] = CountOnes(i
;first_time = false
}
;]return table[x
}
- ( ) , .

82
Count(N)- N:
)int Count(int num
{
)if (num<3
;return num
;)return Count(num-1)+Count(num-2
}
) ,O(2^N .
, ( ).
:
)int Count(int num
{
;int prev = 1
;int curr = 1
)for (int i=1; i<num; i++
{
;int next = prev + curr
;prev = curr
;curr = next
}
;return curr
}
, ) ,O(N ).O(1

83
" , .
" "0 .
" "0 .ACCEPT
( 2 )10 .
.REJECT

1
1

0
( 0 )1:
NextState = (2 * CurrState + Input) % 5

0
2

0
1

0
3

84
"" - , ,A B
.A- ,
K ( ) , :
.1 , K .
.2 , "" .
.3 , K .
.4 , "" ( ).
.5 , K .
)bool SearchLoop(List* list
{
)for (Node* tail = list->head; tail != NULL; tail = tail->next
{
)if (tail->next == tail
;return true
)for (Node* curr = list->head; curr != tail; curr = curr->next
{
)if (tail->next == curr
;return true
}
}
;return false
}
) ,O(N^2 ).O(1
( ):
ptr1 : .ptr2- ptr1 ptr2 .
, "" .
, ) O(N ).O(1

( 85 )
, , . :
,
- , . , ,
.
, N ( N , ) ,
- . :
,N .
, 1 ,N-1 -:
o ,1 ,2 .
o ,N-1 ,N-2 .
, -.
- , .

86
A , :
.A = 0 . ( A ) .
.A < 0 . ,A ( MSB- :)1
A , .10...0
1 , .01...1 :
1 , .
, and A A-1 , A .

87
, ( , ).
, .
Visual Studio 6.0 , .ECX
( 3 : ).

88
8 () . N , N/8.
:
. :
;}Byte table[1<<8] = {0
)for (int i=0; i<(1<<8); i++
)for (int j=0; j<8; j++
;)table[i] |= ((i>>j)&1)<<(8-j-1
. ( - )Byte:
)for (int m=0; m<M; m++
)for (int n=0; n<N/8; n++
;]]outputImage[m][N/8-n-1] = table[inputImage[m][n
:
. Bit-Field , 8:
typedef struct
{
;Byte bit1:1
;Byte bit2:1

;Byte bit8:1
}
;Bits
. ( - )Bits:
)for (int m=0; m<M; m++
{
)for (int n=0; n<N/8; n++
{
;outputImage[m][N/8-n-1].bit8 = inputImage[m][n].bit1
;outputImage[m][N/8-n-1].bit7 = inputImage[m][n].bit2

;outputImage[m][N/8-n-1].bit1 = inputImage[m][n].bit8
}
}
:
,
. , ,
. ( " ) , ).O(M*N

( 89 )
X- .
. , , 1 0-
. X ,
, .
,Count = MaxCount = 0 :
, Count.1-
, .MaxCount
, Count.1-
MaxCount.
:
, .
( , ) ,
, .2 , Count 1-
Count 1- , .MaxCount = 2
( , ) ,
, .0 , Count 1-
Count 1- , .MaxCount = 0
: Y- .

90
Constructor ,:
V-Table- .
V-Table- , , .
, Constructor- , Constructor-
. , Constructor- . ,
.
( , ,)Constructor- ,
.
Destructor ( ) ,:
" , ( )
( ) , Destructor- ,
Destructor- . , Destructor-
. , " ( )new ,
( .)delete delete- ,
. , Destructor- , delete- Destructor-
, Destructor- .,
delete- Destructor- , Destructor-
.
, ,A B :
A* ptr = new B:
new- ( B ) , Constructor- .B
delete ptr:
Destructor- A , delete- Destructor- .B
Destructor- B , Destructor- .A
Destructor- A , delete- Destructor- .A
Destructor- B .
( 3 : ).

91
,A A
,new .A ,
( A )A- . ,
. ( ),
,
( A ) .
.NULL
void* operator new(unsigned int stAllocateBlock, char chInit ); :A
;A* a=new(6) A
:

92
C++- :virtual
, ( ).
, ,type .
type . , .
Constructor .type
Constructor , type , .
, Constructor Constructor- ,
. , type , .
, A B C- .
,virtual A ,A
A B .C
, A , type
. ,A ( A ,
) . B ,C this- ,
.
, this-
"" ( ).
"" :A
)(int A::func
{
)'if (type == 'A
;return 0
)'else if (type == 'B
;)(return ((B*)this)->func
)'else //if (type == 'C
;)(return ((C*)this)->func
}
:
. type ( ") ,dynamic_cast
.
, . , .NULL ,
) ,RTTI (Run-Time Type Information
.
. , RTTI- " ,
.OO ,
( .)Polymorphism
.
. , () ,
"" . , ' (
).

93
:
, .
( ,)if x<y N.
N N- :
N.
N.
, .N + N + N = 1N

94
M ,N :
. ,
. , .
. ,1
.
. ( , ) ,
:
] [i][j 3 ]. [i-1][j-1] , [i-1][j] , [i][j-1
4- , ] [i][j .1
4 , ] [i][j + 1
3- ]. [i-1][j-1] , [i-1][j] , [i][j-1
. , .
, , :
R R G G B
G G G G G
G G G G G
B B B G G
B B B G G
, :
1 1 1 1 1
1 1 1 2 1
1 2 2 x
1
1
.x = 1+Min(1,2,2) = 2 :

95
MyMalloc 4 , :
)void* MyMalloc(int size
{
;)int* mem = malloc(sizeof(int)+size
;mem[0] = sizeof(int)+size
;)return (void*)(mem+1
}
MyFree 4- , :
)void MyFree(void* ptr
{
;int* mem = (int*)ptr-1
;]int size = mem[0
;)free((void*)mem,size
}

96
:
"" , "" " /".
N , " N" . ,Real-Time
( ) ," :
. ( , ).
. , .
)]:void InitBuffers(void* addresses[N
" N" "" , ).O(N
)(:Buffer* GetBuffer
, "" , "" ).O(1
):void FreeBuffer(Buffer* buffer
"" "" , "" ).O(1
"" ".

97
R1 ,R2 :R3
//R3 = 0
//R7 = 0
//R8 = 0
//R1 = R1 - 1
//R7 = R7 + 1
//R2 = R2 - 1
//R8 = R8 + 1
//R3 = R3 + 1
//if (R2 > 0) goto Loop_R2
//R8 = R8 - 1
//R2 = R2 + 1
//if (R8 > 0) goto Restore_R2
//if (R1 > 0) goto Loop_R1
//R7 = R7 - 1
//R1 = R1 + 1
//if (R7 > 0) goto Restore_R1

CLR R3
CLR R7
CLR R8
Loop_R1:
DEC R1
INC R7
Loop_R2:
DEC R2
INC R8
INC R3
JUMP R2 Loop_R2
Restore_R2:
DEC R8
INC R2
JUMP R8 Restore_R2
JUMP R1 Loop_R1
Restore_R1:
DEC R7
INC R1
JUMP R7 Restore_R1

:
,0 .16-
4 , .0
:
CLR DEC.JUMP-
INC DEC( JUMP- INC DEC ).
:
8 .
.-8*1 ,-7*1 ,-6*1 ,-5*1 ,-4*1 ,-3*1 ,-2*1 ,-1*1 :

100
. ,
, ( .)this
, . ,
. , )( MyObject->Func )(.MyClass::Func
( 3 : ).

( 101 )
, ( ,
).
,
( ).
( ) ,
.
, :
, . ,
.
, . ,
.

102
. ,
: ,
.

103
. :
x , .
.
.
,x 3- .

104
,x x- 1 .N N N-:
. x- ,1 :
10...0 ,x LSB- .1
01...1 ,x-1 LSB- .0
and x ,x-1 .x
, x- 1 ,0- x- .
. x , .N

105
:
.1 N .
.2 N-1 ] [1, N-1 .1+2++N-1 = N*(N-1)/2
.3 N N-1 ].[1, N-1

106
x ,7- .x*7 = x*(8-1) = x*8-x = (x<<3)-x :

107
- , - :
12

12
30

12

12

12
24


12

32
30

12

, .
( 5 )6 . .(32+24) = 40 :

108
:
.
.
.
, . .
, - . ":
.
.
.
.

109
x- ( LSB-:)MSB-
,1 0- .
,0 1- .

//The bit value is 1


//The bit value is 0

)for (i=1; i!=0; i<<=1


{
)if (x & i
;x &= ~i
else
{
;x |= i
;break
}
}

110
( N List .)Array
.
( , ) , :
. Array[1] = List[1] :
.
. Array[i] = Max(Array[i-1]+List[i], List[i]) :
:
+ .
( ).
, .
.O(N) : .O(N) :
:
. ( ,
) , . ,
, .

111
, . , . ,
. , :
. .
, .
, .
. , "" (
) . , , .
:
, )).O(log(N
, , )).O(log(N

112
XOR NAND :
x y
)(xy

)((xy)y
...

))(x(xy
:
)((x(xy))((xy)y)) = (x(xy))V((xy)y) = (xx)V(xy)V(xy)V(yy) = (xy)V(xy

113
A 100 , B 95 ( ).
A 5 ,B , 5 .
5 A ( B ).

114
:
X 29.
Y 28.
X .Y
:
:X
o X 28.
o Y 28.
o X .Y
, .
.
.
( 28 ) .

115
N ].[0N-1
,N .false-
X , X .
,false ,true- . , X ,.
N , ] [0N-1 .
.O(N) : .O(N) :
)]bool func(int Array[N
{
;}bool Test[N] = {false
)for (int i=0; i<N; i++
)if (Test[Array[i]] == true
;return true
;return false
}

116
:
X- , X-4.
2 ,1 3 , .
, X-4 , X-8 ( ).
:
X%4.
, .4-
, "" .4-
:
X ,4- .1 X%4 3
2 ,1( X%4 )3.
Y ,1 Y 3 . .1 4-Y 3
Y , 2 ,1( 4-Y )3.

117
0 ,X X- .10-
10 2 ,5- X- 10- :
X- .2-
X- .5-
100 "" 20 5- .
4 ( )100 ,75 ,50 ,25 5- .
, 100 , 5- .
100 "" 50 2- ( 5 , " " ).
100 : 10- 24 , 0 24 .

118
,Type Array[10] , ).sizeof(Array)/sizeof(Type
,Type Array=new Type[10] , .

119
; T* p=0; delete p ) sizeof(T 0.
,delete T
, . , .
; T* p=new T[10]; delete p Destructor- T .p
T ( ,)int , , T
( Destructor-) , " " . delete[] p .delete p

120
:RTTI Run-Time Type Information
, .
,dynamic_cast
. , . , .NULL
A , , B- C- :
;]A* arr[3
;arr[0] = new A
;arr[1] = new B
;arr[2] = new C
)for (int i=0; i<3; i++
{
;)]B* ptr = dynamic_cast<B*>(arr[i
)if (ptr != NULL
;
//arr[i] is of type B
else
;
//arr[i] is not of type B
}
, :
( ,)dynamic_cast .
, V-Table- ( )
V-Table- ( , ).
dynamic_cast .
( .)V-Table
:
, ( RTTI- )dynamic_cast .OO
, (.)Polymorphism

121
:a, b, c, e, f
a+e .b+f
, 2a+2e .2b+2f

f
a
b

a, b, c:
.
( c ).
, .2c

f
a
b

c
e

c
e

122

.1
.2
.
.
.

MIN

MAX

A1
B1

A2
B2

" , .

124
A 60 , B 30:
A - , 30 , .
B - , 15 , .
45 A:B-
.1 A - , , 30.
.2 , B - , , 15.
C 60:
45 A:C-
.1 A C -.
.2 30 A , C 30 .
.3 C , , 15.

125
, :
.
.
)Byte ReverseByte(Byte input
{
;Byte output = 0
)for (int i=0; i<8; i++
{
;Byte unit = (input>>i)&1
;)output |= unit<<(8-i-1
}
;return output
}
, :
- .
.
)Byte ReverseByteFast(Byte input
{
;static bool first_time = true
;}static Byte table[1<<8] = {0
)if (first_time == true
{
)for (int i=0; i<(1<<8); i++
;)table[i] = ReverseByte(i
;first_time = false
}
;]return table[input
}
, :
.
.
)Register ReverseRegisterFast(Register input
{
;Register output = 0
)for (int i=0; i<sizeof(Register); i++
{
;))Register unit = ReverseByteFast(input>>(8*i
;))output |= unit<<(8*(sizeof(Register)-i-1
}
;return output
}

You might also like