0
点赞
收藏
分享

微信扫一扫

CSE101 C++ Introduction to Data Structures and Algorithms

产品喵dandan米娜 03-23 21:00 阅读 2

CSE 101

Introduction to Data Structures and Algorithms

Programming Assignment 5

In this project you will create a new, and somewhat different integer List ADT, this time in C++. You will use this List to perform shuffling operations, and determine how many shuffles are necessary to bring a List  back into its original order. Begin by carefully reviewing Queue and Stack examples posted on the webpage  in Examples/C++. These examples establish our norms and conventions for building ADTs in the C++  language. Also read the handout ADTs in C++ . The header file List.h has also been posted at

Examples/pa5, along with a test client, some output files and a Makefile for this project.

The Perfect Shuffle

A perfect shuffle (also called a riffle shuffle ) is one in which a deck of cards is split evenly, then merged into a new deck by alternately inserting cards from each half into the new deck. For instance, if our deck  contains 7 cards, labeled 0-6, we would perform the following steps.

Deck:  0 1 2 3 4 5 6

Split: 0 1 2 | 3 4 5 6

Prepare to Merge: 3 4 5 6

0 1 2

Merge:  3 0 4 1 5 2 6

Performing the same perfect shuffle operation on the new list, we get: 1 3 5 0 2 4 6. Repeating the  shuffle once again gives the original order: 0 1 2 3 4 5 6. We say that the order of this re-arrangement  (or permutation) is 3, since applying it to any deck 3 times returns the deck to its original order.

We will represent a deck of 𝑛 cards by a list of length 𝑛 , consisting of the integers (0, 1, 2, … , 𝑛 − 1) . If 𝑛 is even, then we can split the list into two equal halves, each of length 𝑛/2 .

(0, 1, … , 𝑛 2 − 1) ( 𝑛 2 , 𝑛  2 + 1, … , 𝑛 − 1)

If 𝑛 is odd, we place the extra card in the right half. The left half then contains ⌊𝑛/2⌋ and the right half ⌈𝑛/2⌉.

(0, 1, … ,⌊ 𝑛 2 ⌋ − 1) (⌊ 𝑛 2 ⌋,⌊ 𝑛 2 ⌋ + 1, … , 𝑛 − 1)

Observe that the latter formulas are correct in both the even and odd case. Your top level client in this project, which will be written in C++, will be called Shuffle.cpp. It will contain a function with the following prototype. void shuffle(List& D);

Function shuffle() will alter its List& (List reference) argument D by performing one shuffle operation, as described above. Function main() will read a single command line argument, which will be a positive integer specifying the maximum number of cards in a deck. For each 𝑛 in the range 1 up to this maximum, your program will perform shuffles until the list (0, 1, 2, … , 𝑛 − 1) is brought back to its original order,counting the number of shuffles as it goes. It will print a table to standard output giving this count, for each value of 𝑛. A sample session follows.

举报

相关推荐

0 条评论