当前位置:网站首页>C语言学习-19-全排列
C语言学习-19-全排列
2022-06-28 15:04:00 【阳光九叶草LXGZXJ】
一、测试环境
| 名称 | 版本 |
|---|---|
| 操作系统 | win10 |
| CPU | 12th Gen Intel Core i7-12700H |
| 内存 | 16G |
| VS | 2017 |
二、个人理解
假设数据:{1,2,3}进行全排列。
我们可以先分为三种情况:
(1)以1为开头的组合
{1,{剩下元素进行全排列}}
(2)以2为开头的组合
{2,{剩下元素进行全排列}}
(3)以3为开头的组合
{3,{剩下元素进行全排列}}
也就是说每个元素都要放到首位,剩余的元素进行全排列,有没有感觉规律很明显,可以用递归的方式尝试解决,编写递归函数时,
一:我们需要找到规律。
二:我们需要找到结束点。
一我们已经找到我们来开始找二。
中间剩下元素进行全排列,又可以分为两种情况(这里以第一组为例):
(1)以2为开头的组合
{2,{剩下元素进行全排列}}
(2)以3为开头的组合
{3,{剩下元素进行全排列}}
最后就剩下一个元素了,说明数组已经遍历完,可以结束,这样我们就找到了结束条件。
三、源码
#include <stdio.h>
#include <time.h>
void PrintArray(int Array[], int ArraySize);
void SwapArrayElement(int Array[], int index_1, int index_2);
void AllPermutaion(int Array[], int top, int end);
void ComputeProcedureTime(void(*Func)(int*, int, int), int Array[], int index_1, int index_2);
int main()
{
int Array[] = {
1 ,2 ,3 };
int ArraySize = sizeof(Array) / sizeof(int);
ComputeProcedureTime(AllPermutaion, Array, 0, ArraySize - 1);
}
void ComputeProcedureTime(void (*Func)(int[], int, int), int Array[], int index_1, int index_2)
{
printf("++++++++++++++++++++++++\n");
clock_t start, finish;
clock_t Total_time;
start = clock();
Func(Array, index_1, index_2);
finish = clock();
Total_time = finish - start;
printf("Elapsed Time : %ld ms\n", Total_time);
}
void PrintArray(int Array[], int ArraySize)
{
int i;
for ( i = 0; i < ArraySize; i++)
{
printf("%d ",Array[i]);
}
printf("\n");
}
void SwapArrayElement(int Array[], int index_1, int index_2)
{
//如果位置相等,说明是同一个元素,不用进行交换,可以稍微提升一点效率。
if (index_1 != index_2)
{
int temp = Array[index_1];
Array[index_1] = Array[index_2];
Array[index_2] = temp;
}
}
void AllPermutaion(int Array[], int top, int end)
{
//如果位置相等,说明数组里的元素已经遍历完,我们可以打印一下数组结果。
if (top == end)
{
PrintArray(Array, end + 1);
}
else
{
int i;
for ( i = top; i <= end; i++)
{
SwapArrayElement(Array, i, top);
AllPermutaion(Array, top + 1, end);
SwapArrayElement(Array, i, top);
//交换两次元素,是为了避免出现重复结果。
}
}
}
四、测试结果
++++++++++++++++++++++++
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2
Elapsed Time : 3 ms

边栏推荐
- Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
- 5000倍回报,南非报业投资腾讯赚了一个省
- Summary of technical difficulties of wearable neural signal and behavior data detection and recording system for birds in flight
- 字节跳动埋点数据流建设与治理实践
- 当下不做元宇宙,就像20年前没买房!
- The latest pycharm activation cracking code in 2022 is permanent_ Detailed installation tutorial (applicable to multiple versions)
- Leetcode (88) -- merge two ordered arrays
- 新零售线下店逆势起飞,通膨乌云下的消费热情
- Setsql function and risk of using lamdbaupdatewrapper
- WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
猜你喜欢
![[MySQL learning notes 23] index optimization](/img/08/644fddf2521b47de41dff99ebaad56.png)
[MySQL learning notes 23] index optimization

C#/VB.NET 将PDF转为Excel

Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion

After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time

名创优品通过上市聆讯:寻求双重主要上市 年营收91亿

High "green premium" of environmental protection products? How far is the low-carbon lifestyle from people

vscode编写markdown文件并生成pdf

PMP认证证书的续证费用是多少?
技术弄潮儿

Facebook出手!自适应梯度打败人工调参
随机推荐
Express模板引擎
当下不做元宇宙,就像20年前没买房!
【算法篇】刷了两道大厂面试题,含泪 ”重学数组“
美因基因港交所上市:市值43亿港元 IPO被市场忽略
雷科防务:4D毫米波雷达产品预计可以在年底量产供货
dolphinscheduler2. Installation of X (valid for personal test)
考了这个PMP证书到底有什么好处?
竞远安全冲刺创业板:拟募资4亿 启元投资与云游软件是股东
Rails advanced -- framework theory cognition and construction scheme construction (I)
[collection of excellent articles on Digital IC] nearly 500 articles | learning route | basic knowledge | interface | bus | scripting language | chip job search | security | EDA | tools | low power de
How can I get the stock account opening discount link? Is it safe to open a mobile account?
第四大运营商,难成「鲶鱼」
WSUS客户端访问服务端异常报错-0x8024401f「建议收藏」
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
Does Frankfurt currently support SQL?
code snippet
After QQ was stolen, a large number of users "died"
Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
High "green premium" of environmental protection products? How far is the low-carbon lifestyle from people
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、将两个可视的组合结果对齐