solution.md 2.9 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4
# 排列序列

<p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol>	<li><code>"123"</code></li>	<li><code>"132"</code></li>	<li><code>"213"</code></li>	<li><code>"231"</code></li>	<li><code>"312"</code></li>	<li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= n <= 9</code></li>	<li><code>1 <= k <= n!</code></li></ul>

ToTensor's avatar
ToTensor 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
以下程序实现了这一功能,请你填补空白处的内容:

```cpp
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *getPermutation(int n, int k)
{
	int i;
	char *result = malloc(n + 1);
	bool *used = malloc(n * sizeof(bool));
	memset(used, false, n * sizeof(bool));
	int total = 1;
	for (i = 1; i <= n; i++)
	{
		total *= i;
	}
	k = k - 1;
	for (i = 0; i < n; i++)
	{
		total /= (n - i);
		int gid = k / total;
		k %= total;
		int x = -1;
		int count = 0;
		____________________;
		used[x] = true;
		result[i] = x + 1 + '0';
	}
	result[n] = '\0';
	return result;
}
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr, "Usage: ./test n, k\n");
		exit(-1);
	}
	printf("%s\n", getPermutation(atoi(argv[1]), atoi(argv[2])));
	return 0;
}
```

每日一练社区's avatar
每日一练社区 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
## template

```cpp
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *getPermutation(int n, int k)
{
	int i;
	char *result = malloc(n + 1);
	bool *used = malloc(n * sizeof(bool));
	memset(used, false, n * sizeof(bool));
	int total = 1;
	for (i = 1; i <= n; i++)
	{
		total *= i;
	}
	k = k - 1;
	for (i = 0; i < n; i++)
	{
		total /= (n - i);
		int gid = k / total;
		k %= total;
		int x = -1;
		int count = 0;
		while (count <= gid)
		{
			x = (x + 1) % n;
			if (!used[x])
			{
				count++;
			}
		}
		used[x] = true;
		result[i] = x + 1 + '0';
	}
	result[n] = '\0';
	return result;
}
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr, "Usage: ./test n, k\n");
		exit(-1);
	}
	printf("%s\n", getPermutation(atoi(argv[1]), atoi(argv[2])));
	return 0;
}
```

## 答案

```cpp
ToTensor's avatar
ToTensor 已提交
105 106 107 108 109 110 111 112
while (count <= gid)
{
	x = (x + 1) % n;
	if (!used[x])
	{
		count++;
	}
}
每日一练社区's avatar
每日一练社区 已提交
113 114 115 116 117 118 119
```

## 选项

### A

```cpp
ToTensor's avatar
ToTensor 已提交
120 121 122 123 124 125 126 127
while (count <= gid)
{
	x = (x + 1) % n;
	if (used[x])
	{
		count++;
	}
}
每日一练社区's avatar
每日一练社区 已提交
128 129 130 131 132
```

### B

```cpp
ToTensor's avatar
ToTensor 已提交
133 134 135 136 137 138 139 140
while (count < gid)
{
	x = (x + 1) % n;
	if (used[x])
	{
		count++;
	}
}
每日一练社区's avatar
每日一练社区 已提交
141 142 143 144 145
```

### C

```cpp
ToTensor's avatar
ToTensor 已提交
146 147 148 149 150 151 152 153
while (count <= gid)
{
	x = x % n;
	if (used[x])
	{
		count++;
	}
}
每日一练社区's avatar
每日一练社区 已提交
154
```