用c语言写一个与next_permutation作用一样的函数
时间: 2023-03-13 18:00:20 浏览: 98
我可以为你提供一个示例:int next_permutation(int arr[], int n)
{
// Find the largest index k such that arr[k] < arr[k + 1]
int k = -1;
for (int i = 0; i < n - 1; i++)
if (arr[i] < arr[i + 1])
k = i;
// If no such index exists, the permutation is the last permutation
if (k == -1)
return 0;
// Find the largest index l greater than k such that arr[k] < arr[l]
int l;
for (int i = 0; i < n; i++)
if (arr[k] < arr[i])
l = i;
// Swap the two numbers
swap(arr[k], arr[l]);
// Reverse the sequence from k + 1 to n
int start = k + 1, end = n - 1;
while (start < end)
swap(arr[start++], arr[end--]);
return 1;
}
阅读全文