当前位置:网站首页>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

边栏推荐
- Leetcode (406) - rebuild the queue based on height
- 解决Unable to create process using ‘D:\Program File
- Leetcode 48. Rotate image (yes, resolved)
- PMP真的有用吗?
- Leetcode 705. Design hash collection
- 雷科防务:4D毫米波雷达产品预计可以在年底量产供货
- Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
- spark sql 生成 json
- 买卖股票的最佳时机
- 鸟类飞行状态下穿戴式神经信号与行为数据检测记录系统的技术难点总结
猜你喜欢

Introduction to common components of IOT low code platform

Q-tester 3.2: applicable to development, production and after-sales diagnostic test software

【黑马早报】腾讯回应大批用户QQ号被盗;薇娅丈夫公司被罚19万;中国恒大被申请清盘;关晓彤奶茶店回应被加盟商起诉...
Technical trendsetter

3. Caller 服务调用 - dapr

快手投资电商服务商易心优选

halcon 基础总结(一)裁切图片并旋转图像

美因基因港交所上市:市值43亿港元 IPO被市场忽略

Leetcode 705. Design hash collection

安杰思医学冲刺科创板:年营收3亿 拟募资7.7亿
随机推荐
vector详解+题目
Summary of technical difficulties of wearable neural signal and behavior data detection and recording system for birds in flight
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和数据表格横向组合起来形成最终结果图
Express模板引擎
鸟类飞行状态下穿戴式神经信号与行为数据检测记录系统的技术难点总结
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、将两个可视的组合结果对齐
BatchNorm2d原理、作用及其pytorch中BatchNorm2d函数的参数讲解
Ding! Techo day Tencent technology open day arrived as scheduled!
[C language] implementation of binary tree and three Traversals
code snippet
Seata数据库中出现以下问题要怎么解决呀?
Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion
请问一下,是不是insert all这种oracle的批量新增没拦截?
Introduction to common components of IOT low code platform
Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market
【数字IC精品文章收录】近500篇文章|学习路线|基础知识|接口|总线|脚本语言|芯片求职|安全|EDA|工具|低功耗设计|Verilog|低功耗|STA|设计|验证|FPGA|架构|AMBA|书籍|
Leetcode 48. Rotate image (yes, resolved)
Setsql function and risk of using lamdbaupdatewrapper
[spatial & single cellomics] phase 1: Study on PDAC tumor microenvironment by single cell binding spatial transcriptome
Rails进阶——框架理论认知与构建方案建设(一)