December 19,2007

糟糕不懂作業在幹麻

Homework 5
Program Design

DUE: 23:59 December 30, 2007
(-20% for each day late)

翻譯完還是沒有頭緒,我要不要轉系英文(喂)

5:33 pm
大概下午兩點開始奮鬥(嗯這不是上午就翻譯好了嗎?XD)現在跑出來了!
感謝白情兩位同學的討論指引一點方向!(關於srand( time( NULL ) ) 跟rand() 的取值問題)

一點筆記是call by reference 跟suffling 完要把B[i] assign to A[i]

The task of this homework is to simulate the shuffling of a deck of cards. Each shuffle is done by drawing a pile of cards from the middle of the deck of cards and put them to the top of the whole deck. How many times do you think we have to shuffle the cards so that it is considered well shuffled? To get an answer, we will also write a function to determine whether the deck is shuffled well.

  這個作業要要模擬洗牌。所謂洗牌一次就是從一疊牌的中段抽出一小疊移到這整疊牌的最上面。你覺得必須洗多少次牌才算乾淨?為了得到這答案,寫個程式來決定這副牌有沒有被洗乾淨吧。
(內心OS:所謂牌有沒有洗乾淨?取決的是你的心!)

We will store the cards in an array of 52 integers. We can simply number the cards from 1 to 52. (They could be easily converted into four suits of cards that are numbered from 1 to 13.)

  存一個包含52個整數的陣列,當作是一副牌中的52張牌。(可以簡單的轉換成排中的四種花色各13張。)

Write a function cut_and_move(int A[], int B[], int size) to cut a deck of cards by generating two random numbers k1 and k2 that are between 0 and size. Assume size = 52. The cards in the middle (i.e. A[k1] to A[k2]) are moved to the top of the deck. This can be done by moving A[k1]..A[k2] to B[0]..B[k2-k1]. Note that the other elements in array A must be copied to array B properly. For example, A[k2+1]..A[size-1] should be copied to B[k2+1]..B[size-1]. You also have to move A[0]..A[k1-1] to some proper positions in array B.

  寫一個副程式cut_and_move(int A[], int B[], int size)用來切牌,利用兩個從0到size之間的隨機的數字k1、k2。(假設size等於52。)在中間的牌被移到最上面,也就是說編號A[k1]到編號A[k2]的這一疊牌被移至編號B[0]到編號B[k2-k1],記得在陣列A中的元素要巧妙地移到陣列B。比方說,A[k2+1]到A[size-1]複製到B[k2+1]到B[size-1] ,A[0]..A[k1-1]也要移到陣列B合適的位置。
(內心OS:鬼知道你在說什麼囧)

示意圖

編號
0, 1, 2, ...., k1-1, k1, k1+1, ......, k2-1, k2, k2+1, ....., size-2, size-1

A
0, 1, 2, ...., k1-1, k1, k1+1, ......, k2-1, k2, k2+1, ....., size-2, size-1

B
k1, k1+1, ......, k2-1, k2, 0, 1, 2, ...., k1-1, k2+1, ....., size-2, size-1

Write another function benchmark(int cards[], int size) to determine whether a deck is well shuffled. If a card with number n is followed by the card with number n+1, then it is likely that the deck is not well shuffled. So we will count how many cards in the array are followed by their next number. The function then returns the count. A deck is considered well shuffled if the count is low.

  寫另一個副程式benchmark(int cards[], int size)來決定這副牌乾不乾淨。如果有兩張相鄰數字的牌相鄰如果某張牌編號是n,然後它的下一張編號是n+1,那麼這副牌可能不乾淨。所以我們會計算陣列中有多少這種情形並回傳,這種情形夠少才算乾淨。
(內心OS:到底function的正式中文叫什麼?難道有連續的數字就不隨機了嗎?我們以為是隨機的其實才不隨機啊!我們以為是不隨機的其實才是隨機啊!隨機就是一定會有連續啊!

To help the TAs grade your homework, please output the array cards[] in . Place a '*' in front of the number that is followed by its next number.For example:

  為了方便助教批改,請顯示benchmark()中的陣列cards[],並在n的前面加上*星號。範例如下:

13 5 *8 9 17 50 49 26 *11 12 32 30 *19
20 7 6 52 1 .........

In the main program, call benchmark() and output its count every time a deck is cut and shuffled. Keep shuffling the cards until benchmark() returns 2 or fewer. Output how many shuffles you need to get a deck of well shuffled cards.

  在主程式main中呼叫benchmark()並顯示每洗一次牌,牌中的連續牌數目。一直洗牌直到benchmark()回傳值<=2。最後顯示你必須洗幾次牌才算洗乾淨。

Posted by hrkebright at 樂多Roodo! │10:57 │回應(0)引用(0)
樂多分類:日記/一般 工具:編輯本文
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/4681499