In this example, this four-digit number is called the correct answer.. 3 When a competitor receives the other competitor’s guess, it is compared with the receiving competitor’s own corre
Trang 1MINISTRY OF SCIENCE AND TECHNOLOGY
HOÀ L C HIGH TECH PARK
MANAGEMENT BOARD
VIETNAM IT EXAMINATION AND
TRAINING SUPPORT CENTER (VITEC)
SOFTWARE DESIGN AND DEVELOPMENT
ENGINEER EXAMINATION
26 th January 2003
Afternoon I I
Do not open the exam booklet until
instructed to do so
Inquiries about the exam questions
will not be answered.
B KHOA H C VÀ CÔNG NGH
BAN QU N LÝ KHU CÔNG NGH CAO HOÀ L C TRUNG TÂM SÁT H CH CÔNG NGH THÔNG TIN VÀ H TR ÀO T O (VITEC)
SÁT H CH
K S THI T K VÀ PHÁT TRI N
PH N M M Ngày 26 tháng 1 n m 2003
Ph n thi bu i chi u I I
Không m đ thi tr c khi đ c phép
Các th c m c v câu h i thi s không đ c tr l i
Trang 22003 VITEC
Software Design and Development Engineer Examination
(Afternoon, Part 2) Questions must be answered in accordance with the following:
Question Nos Q1 Question Selection All Sub-Questions are compulsory Examination Time 15:30-16:30 (60 minutes)
3 After the test, you may take this question booklet home with you
Do not open the exam booklet until instructed to do so
Inquiries about the exam questions will not be answered
Trang 33 Cu i gi thi, b n có th gi l i b đ thi này
Không m t p gi y thi ch ng nào ch a đ c h ng d n làm nh v y M i vi c h i thêm v các câu h i sát h ch s không đ c tr l i
Trang 53 Cu i gi thi, b n có th gi l i b đ thi này
Không m t p gi y thi ch ng nào ch a đ c h ng d n làm nh v y M i vi c h i thêm v các câu h i sát h ch s không đ c tr l i
Trang 7(3) Write each answer in the space specified for that question
(4) Write your answers clearly and neatly Answers that are difficult to read will receive a lower score
3 After the test, you may take this question booklet home with you
Do not open the exam booklet until instructed to do so
Inquiries about the exam questions will not be answered
Trang 8Q1 Read the following text regarding the creation of a game program, then answer
Sub-Questions 1 through 4
A decision was made to create a number guessing game program in which a person (hereinafter referred to as the “player”) competes with a computer The rules of the game are as follows
(1) The competitors (player and program) each select a four-digit number consisting of four different numerals In this example, this four-digit number is called the correct answer The first digit in the correct answer (most significant digit) may be 0
(2) Competitors take turns trying to guess each other’s correct answer, and the first one to guess correctly wins If both competitors guess correctly in the same number of tries, then they tie
(3) When a competitor receives the other competitor’s guess, it is compared with the receiving competitor’s own correct answer, and a response is sent to the other competitor consisting of the number of hits, hit, or numerals which are correct in both numerical value and digit position; as well as the number of misses, miss, or numerals which are contained in the correct answer, but have incorrect digit positions For example, if the correct answer is 1357 and the other competitor guesses 5310, then the response consists of one hit (hit) and two misses (miss)
An overview of the number guessing game program is presented below
1 The program determines a correct answer using random numbers
2 The following are repeated until one of the competitors guesses the other’s correct answer
2-1 Comparison against correct answer
The program receives the player’s guess, compares it against the correct answer, and returns the numbers of hits, hit and misses, miss
2-2 Guessing process
2-2-1 The program determines the next guess based on the hit and miss it
has so far received, then sends that guess to the player
2-2-2 The program receives the hit and miss calculated by the player
Below, the variables defined in the function are local variables, and the function’s arguments are passed by value unless otherwise specified The array subscripts start at 0, and array elements are designated as 0th, 1st, 2nd, etc
Figure 1 shows the function for determining the correct answer With this function, numbers 0 through 9 are randomly generated one at a time, and an array tab consisting of ten elements is used to obtain a four-digit number with no duplicate numerals “random”
2
Trang 9is a function for randomly generating natural numbers, and “%” is a modulus operator The array tab is used as follows
¦ The initial value of all elements is 0
If the tab[a] value remains at the initial value 0 for a randomly generated digit number a, then a is a number appearing for the first time, and 1 is inserted in tab[a]
single-¡ If the value of tab[a] is 1, then the value is not changed
function set_answer ()
int tab[10], n, i, a
Set all tab elements to 0
n = 0 for (i = 1 to 4)
Fig 1 “set_answer” Function
The algorithm for calculating hit and miss will now be considered
First, the similarity of two four-digit numbers x and y is defined as follows, using the hit and miss of the two numbers:
similarity(x, y) = hit * 10 + miss The set of the similarity values and the (hit, miss) values has a one-to-one correspondence, so the similarity is used within the program instead of the hit and miss
Similarity calculation is used when the player’s guess is compared with the correct answer, and when the program is determining its own guess This calculation is done frequently, so a highly efficient execution method will now be considered
In order to improve execution efficiency, before similarity is calculated during the game, one of the numbers to be compared is translated into a form called an array expression In this translation process, if the subject number is d4d3d2d1, then i is stored
in the dith element of the array, and 0 is stored in the other elements Figure 2 shows the
Trang 10function for translating the number y into an array expression “pos” is an array which comprises ten elements and is called by reference
function make_pos(y, pos)
int i, m Set all pos elements to 0 for(i = 1 to 4)
Fig 2 “make_pos” Function
When the array expression (pos) for the number y is used, similarity(x, y) can be determined using the “sim” function in Figure 3
4
Trang 11function sim(x, pos)
int hit, miss, i, p hit = 0
miss = 0 for(i = 1 to 4)
p = x % 10
x = x / 10 if( c ) if( d ) hit = hit + 1 else miss = miss + 1
Fig 3 “sim” Function
The guessing algorithm will now be considered
If all four-digit numbers consisting of four different numerals are generated, the set of generated numbers will necessarily contain the correct answer Therefore, these numbers are generated in advance and inserted in the “nums” array
The algorithm for selecting a guess value in order to guess the player’s correct answer is as follows
Numbers are taken from “nums” sequentially, and a number which is not inconsistent with the similarity information obtained thus far is used as the next guess
For example, consider a case in which the third guess is to be selected Assume that the similarity values of the correct answer and the previous two guesses were as follows:
Trang 12Now assume that the next number taken from “nums” is 0432 A calculation is performed
to determine whether 0432 is suitable as the next guess, with the following results: similarity(0432, 1234) = 12,similarity(0432, 1345) = 02 These results show that 0432 is not inconsistent with the previous results, so it is used as the next guess
When these considerations are put together, the function for determining the kth guess is as shown in Figure 4 In this case, the “nums” array is an array containing correct answer candidates; guess[i-1] is the array expression for the ith guess; and log[i-1] is the similarity value between the ith guess and the correct answer “index” is a subscript specifying a position before which the correct answer does not exist in the
“nums” elements The initial value for “index” is –1
if(j < k - 1)
f else
Trang 13When all of the above-mentioned functions are used, the number guessing game program
is as shown in Figure 5 “nums”, “guess”, “log” and “index” are global variables int log[M], guess[M][10], nums[N], index // M and N are array sizes
main()
In “nums”, insert all four-digit numbers comprising four different numerals
read guess_person // Read player’s guess
r = sim(guess_person, pos_a) // Calculate similarity
write r / 10, r % 10 // Display results
g = make_guess(i) // Calculate i th guess
read hit, miss // Read guess results
if(hit = 4 or r = 40) // Was guess correct?
break
endif
log[i-1] = 10 * hit + miss // Record results
make_pos(g, guess[i-1]) // Record guess
i = i + 1 endfor
Trang 14After the player plays the game for a while, the program stops responding An investigation
of the issue showed that it occurred because the player’s hit or miss response value was incorrect, so the program could not find a candidate which would be the correct answer
Changes need to be made so that in such cases, “make_guess” returns a value of –1
What type of judgment should be added to the “make_guess” function in Fig 4? Answer
in 4 words or less
Sub-Question 4
In the algorithm of the program in Figure 5, the computer always repeats the same guesses every time, even when the player plays against the computer repeatedly For this reason, it becomes possible for the player to find numbers that are difficult for the computer to guess
In order to avoid this, the program should use a different sequence of guesses each time In order to use a different guessing sequence each time, a certain operation should be performed on the “nums” array value immediately before “play” is called from “main” Describe this operation in 4 words or less
8
Trang 15[Memo]
Trang 16[Memo]
10
Trang 17Write your examinee number in the space provided, and mark the appropriate space below each digit
(2) Date of Birth
Write your date of birth (in numbers) exactly as it is printed on your examination admission card, and mark the appropriate space below each digit
(3) Write each answer in the space specified for that question
(4) Write your answers clearly and neatly Answers that are difficult to read will receive a lower score
3 After the test, you may take this question booklet home with you
Do not open the exam booklet until instructed to do so
Inquiries about the exam questions will not be answered.
Trang 18Q1 Hãy đ c đo n v n sau đây v vi c vi t m t ch ng trình trò ch i, sau đó tr l i các câu h i
(2) Các đ u th đ n l t ch i s đoán đáp án đúng c a bên kia, và bên nào đoán đúng
tr c là bên th ng N u c hai đ u th đoán đúng sau cùng s l n đoán, thì hoà
(3) Khi m t đ u th nh n câu đoán c a bên kia, nó so sánh câu đoán v i đáp án c a mình,
và tr l i đ i ph ng s đi m đoán đúng, hit, t c là s ch s đoán đúng c v giá tr
và v trí, c ng nh s đi m đoán sai, t c là s ch s có trong đáp án nh ng l i sai v
v trí Ví d , n u đáp án là 1357 và s đoán là 5310, thì tr l i là m t đúng (hit) và 2 sai (miss)
T ng quan v ch ng trình đoán s đ c trình bày d i đây
2-2-1 Ch ng trình đoán ti p d a trên s hit và miss đã nh n đ c cho đ n
lúc đó, sau đó g i ph ng đoán cho ng i ch i
đ c m t s có 4 ch s không có ch s trùng nhau “random” là hàm dùng đ sinh
ng u nhiên m t s t nhiên, và “%” là phép chia l y ph n d M ng tab đ c dùng nh sau:
Trang 19a
¦ Giá tr kh i t o c a t t c các ph n t b ng 0
N u giá tr tab[a] v n còn là giá tr kh i đ u 0 cho m t s a có 1 ch s đ c sinh
ng u nhiên, thì a là s xu t hi n l n đ u, và 1 đ c thêm vào tab[a]
¡ N u giá tr c a tab[a] là 1, thì giá tr này không thay đ i
function set_answer ()
int tab[10], n, i, a
Set all tab elements to 0
n = 0 for (i = 1 to 4)
Thu t toán đ tính hit và miss bây gi s đ c xét t i
u tiên, hàm similarity cho hai s có 4 ch s x và y đ c đ nh ngh a nh sau, s
d ng hit và miss c a hai s :
similarity(x, y) = hit * 10 + miss
T p các giá tr c a similarity và các giá tr (hit, miss) có t ng ng 1-1, nên similarity đ c dùng trong ch ng trình thay cho hit và miss
Vi c tính similarity đ c dùng khi ph ng đoán c a ng i ch i đ c so sánh v i đáp
án đúng, và khi ch ng trình xác đ nh ph ng đoán c a nó Vi c tính toán này đ c th c
hi n th ng xuyên, nên m t ph ng pháp th c hi n hi u qu cao s đ c đ c p sau đây
c i thi n hi u qu th c hi n, tr c khi tính similarity trong trò ch i, m t trong các
s đem so sánh s đ c bi n đ i thành m t d ng g i là bi u th c m ng Trong quá trình
bi n đ i này, n u s đem bi n đ i là d4d3d2d1, thì i đ c l u vào ph n t th i (ký hi u
di) c a m ng, và 0 đ c l u vào các ph n t khác Hình 2 bi u di n hàm dùng đ bi n đ i
s y sang bi u th c m ng “pos” là m t m ng g m có 10 ph n t và đ c g i theo tham chi u
Trang 20function make_pos(y, pos)
int i, m Set all pos elements to 0 for(i = 1 to 4)
Trang 21function sim(x, pos)
int hit, miss, i, p hit = 0
miss = 0 for(i = 1 to 4)
p = x % 10
x = x / 10 if( c ) if( d ) hit = hit + 1 else miss = miss + 1
Bây gi ta xem xét đ n thu t toán ph ng đoán
N u t t c các s 4 ch s ch a 4 ch s khác nhau đ c sinh, thì b các s sinh ra s c n bao g m đáp án đúng Do đó, các s này đ c sinh tr c và đ a vào m ng “nums”
Thu t toán ch n giá tr ph ng đoán dùng đ đoán ra đáp án đúng c a ng i ch i là nh sau Các s đ c l y tu n t t m ng “nums”, và m t s nh t quán v i thông tin similarity thu đ c cho đ n lúc đó s đ c dùng cho l n ph ng đoán ti p theo
Ví d , tính đ n tr ng h p trong đó ph ng đoán th 3 đ c ch n Gi s r ng giá tr similarity c a đáp án đúng và hai ph ng đoán tr c đó là nh sau:
Trong tr ng h p này, gi s s ti p theo l y t m ng “nums” là 2304 Phép tính đ c
th c hi n đ tính xem 2304 có thích h p đ làm ph ng đoán ti p theo hay không cho k t
qu sau: similarity(2304, 1234) = 12,similarity(2304, 1345) = 11
K t qu cho th y 2304 không nh t quán v i k t qu th hai, vì th nó không thích h p đ làm ph ng đoán ti p theo
Trang 22Bây gi gi s r ng s ti p theo l y t m ng “nums” là 0432 M t phép tính đ c th c
hi n đ tính xem 0432 có thích h p đ làm ph ng đoán ti p theo hay không cho k t qu
nh sau: similarity(0432, 1234) = 12,similarity(0432, 1345) =
02 Các k t qu này cho th y 0432 nh t quán v i các k t qu tr c, nên có th dùng làm
ph ng đoán ti p theo
Khi các các cân nh c này đ c đ t vào cùng v i nhau, hàm đ tính ph ng đoán th k đ c
mô t trong hình 4 Trong tr ng h p này, m ng “nums” là m t m ng ch a các ng viên
c a đáp án; ph n t guess[i-1] là bi u th c m ng c a ph ng đoán th i, và 1] là giá tr similarity gi a ph ng đoán th I v i đáp án đúng “index” là ch s xác
log[i-đ nh v trí trong m ng “nums” mà tr c log[i-đó log[i-đáp án log[i-đúng v n ch a có trong các ph n t c a
“nums” Giá tr kh i t o c a “index” là –1
if(j < k - 1)
f else
Trang 23Khi t t c các hàm nói trên đ c s d ng, ch ng trình trò ch i đoán s đ c mô t trong hình 5
“nums”, “guess”, “log” và “index” là các bi n toàn c c
int log[M], guess[M][10], nums[N], index // M and N are array sizes
main()
In “nums”, insert all four-digit numbers comprising four different numerals
read guess_person // Read player’s guess
r = sim(guess_person, pos_a) // Calculate similarity
write r / 10, r % 10 // Display results
g = make_guess(i) // Calculate i th guess
read hit, miss // Read guess results
if(hit = 4 or r = 40) // Was guess correct?
break
endif
log[i-1] = 10 * hit + miss // Record results
make_pos(g, guess[i-1]) // Record guess
i = i + 1 endfor