当前位置:网站首页>递归调用--排列的穷举
递归调用--排列的穷举
2022-04-23 04:15:00 【学到老才能活到老】
题目:从数字1~n中选取r个数字并将其排列,输出所有的排列结果,顺序不同也视为不同的排列。其中n和r为输入的参数:
例如:从数字1~5中选取3个数字时的排列为如下的60中情况:
1 2 3, 1 2 4, 1 2 5, 1 3 2, 1 3 4, 1 3 5, 1 4 2, 1 4 3, 1 4 5, 1 5 2, 1 5 3, 1 5 4,
2 1 3, 2 1 4, 2 1 5, 2 3 1, 2 3 4, 2 3 5, 2 4 1, 2 4 3, 2 4 5, 2 5 1, 2 5 3, 2 5 4,
3 1 2, 3 1 4, 3 1 5, 3 2 1, 3 2 4, 3 2 5, 3 4 1, 3 4 2, 3 4 5, 3 5 1, 3 5 2, 3 5 4,
4 1 2, 4 1 3, 4 1 5, 4 2 1, 4 2 3, 4 2 5, 4 3 1, 4 3 2, 4 3 5, 4 5 1, 4 5 2, 4 5 3,
5 1 2, 5 1 3, 5 1 4, 5 2 1, 5 2 3, 5 2 4, 5 3 1, 5 3 2, 5 3 4, 5 4 1, 5 4 2, 5 4 3
解题:
#include <stdio.h>
/* n的最大值 */
#define N_MAX (100)
/* 若数字已被使用,则将下标为该数字的元素设成1 */
int used_flag[N_MAX + 1];
int result[N_MAX];
int n;
int r;
void print_result(void)
{
int i;
for (i = 0; i < r; i++) {
printf("%d ", result[i]);
}
printf("\n");
}
void permutation(int nth) // 参数nth表示当前正在处理第几个数字
{
int i;
if (nth == r) {
print_result();
return; // 注意这里返回的位置在哪
}
for (i = 1; i <= n; i++) {
if (used_flag[i] == 0) {
result[nth] = i;
used_flag[i] = 1;
permutation(nth + 1);
used_flag[i] = 0;
}
}
}
int main(int argc, char **argv)
{
sscanf(argv[1], "%d", &n);
sscanf(argv[2], "%d", &r);
permutation(0);
}
Linux下执行输出如下:
gcc permutation.c -o permutation
./permutation 3 3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
版权声明
本文为[学到老才能活到老]所创,转载请带上原文链接,感谢
https://blog.csdn.net/MARS_098/article/details/124353715
边栏推荐
- 顺序表的基本操作
- Hard core chip removal
- 为什么推荐你学嵌入式
- [AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
- CRF based medical entity recognition baseline
- 基于PHP的代步工具购物商城
- 【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
- [BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
- 創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場
- [string] ranking of country names ----- problem solving notes
猜你喜欢
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
LabVIEW 小端序和大端序区别
[BIM introduction practice] wall hierarchy and FAQ in Revit
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
Express中间件②(中间件的分类)
Difference between LabVIEW small end sequence and large end sequence
随机推荐
C language character constant
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
【Echart】echart 入门
How Zotero quotes in word jump to references / hyperlink
Alibaba cloud IOT transfer to PostgreSQL database scheme
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
【Pytorch基础】torch.split()用法
Leetcode->1 两数之和
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
洛谷P1858 【多人背包】 (背包求前k优解)
QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)
UDP protocol and TCP protocol
现货黄金操作技巧_估波曲线
什么是软件验收测试,第三方软件检测机构进行验收测试有什么好处?
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估