English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am writing a C++ program, and if I can get a working algorithm, I could really tackle this problem more clearly...

Here's my problem: A permutation of the number 1, 2, ... , n arrive on-line. For example if n = 5 then first it may be that 2 will arrive then 3, then 5, then 1, and then 4 ( ot any other permutation). In the example the permutation is 2, 3, 5, 1, 4. When the next number arrives, the algorithm has two options: print the current number to the output, or store it in a given queue Q. Reguardless of the input string, at any moment the algorithm can also perform an unlimited number of dequeue and enqueue operations.

2006-10-15 14:40:44 · 2 answers · asked by excalibur1502 1 in Computers & Internet Programming & Design

2 answers

Your question:
An algorithm that reads the permutation and outputs the numbers in increasing order (1, 2, ... , n).?

My Answer:
I would say that you would push all numbers into an array. After that, sort the array and then print each element starting with element 1 (sometimes, element 0 is counted as the first element) until you've exhausted all elemetns.

Your permutation = 2,3,5,1,4
1 - Push this into Array arPermutation
2 - Sort arPermutation so that it is equal to 1,2,3,4,5
3 - Grab total number of elements and you will find a total of 5
4 - For loop starting with the first element and stopping when we exhausted all elements (This is 5)
5 - For each loop we do, print the element. Since we start with element 1, it will print 1 and continue in increasing order.

2006-10-15 17:35:16 · answer #1 · answered by thepinky 3 · 0 0

What's the problem at the moment?
I would first ask for user input for the max number and then put that into a for loop. Subtracting 1 every time the user has input another number until it finishes.

If you're trying to find how many possible outcomes there are then you can use a simple equation to get that... let's remember back to Algebra 2 shall we...

2006-10-15 14:45:35 · answer #2 · answered by Robin C 4 · 0 0

fedest.com, questions and answers