| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11213 | permutations |
|
| 11686 | more simple exercise of pointer on array |
|
Description
Given a set of n≧1 elements, the problem is to print all possible permutations of this set. For example, if the set is (1,2,3), then the set of permutations is {(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,2,1), (3,1,2)}.
<Hint1>
Looking at the case of four elements (1,2,3,4). The answer can be constructed by writing
- ‘1’ followed by all the permutations of (2,3,4)
- ‘2’ followed by all the permutations of (1,3,4)
- ‘3’ followed by all the permutations of (1,2,4)
- ‘4’ followed by all the permutations of (1,2,3)
<Hint2>
A recursive method to implement the above idea is as follows:
Consider the case of (1,2,3,4), that is, n=4.
- Place the set elements in a global array, and set the position index “k” as 0.
- Use a for-loop to “swap” (or exchange) the 1st element with the 1st element, the 2nd element, the 3rd element, and the 4thelement, respectively.
- In each loop-iteration:
- increment the position index “k” by 1 (for considering only the remaining elements in the following recursive call);
- use the updated k to recursively call your permutation function;
- note that because you use a global array, remember to swap back the two elements after the iteration.
- In each loop-iteration:
- In a recursive-call path, when k reaches n, it means that you get a possible permutation.
You will be provided with the following sample code, and asked to implement function "Swap" and "Perm.
#include <stdio.h>
char list[10];
void show(int n)
{
int i;
printf("(%c", list[0]);
for (i=1; i<n; i++) {
printf(",%c", list[i]);
}
printf(")\n");
}
void Swap(int k, int i)
{
/*your code*/
}
void Perm(int k, int n)
{
/*your code*/
}
int main(void)
{
int num, i;
scanf("%d", &num);
for(i=0; i<num; i++)
list[i] = '1'+i;
Perm(0, num);
return 0;
}
Input
The decimal number n that represents the number of elements in the set.
(1≦n≦5)
Output
In the output you should print all the permutations.
Be sure to add a newline character '\n' at the end of each line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In this exercise, you should exchange the content of two given one dimensional integer arrays with same length.
We have written the code about reading the size and the content of arrays, and we want to pass them to a function for getting the final answer. What you need is helping us to complete the function.
What input into the function is two pointers representing the two arrays and the length of array, and the function has to exchange the content of the two arrays.
For more specific, you only need to implement the following function:
#include <stdio.h>
#include "function.h"
void array_exchanger(int* array_a, int* array_b, int length) {
// your code
}
The .c and .h file below might be helpful if you want to know how the function and the whole program works.
Input
The input may have 3 lines of input.
The very first line is a number, representing the length of 2 arrays.
The second and the third lines are several integers, representing the content of 2 arrays.
Output
The content in two arrays after exchanged.