当前位置:网站首页>Recursive method to achieve full permutation (C language)
Recursive method to achieve full permutation (C language)
2022-07-04 10:27:00 【Lol only plays Timo】
Full permutation is the mathematical tool we learned in mathematics to calculate all the permutations of a group of numbers or letters .
such as ,ABCD These four letters are randomly selected from the full arrangement of three letters 24 There are so many kinds . that , How can we use program to realize it ?
Let's discuss the manual process of this problem :
Nothing more than to see whether the current letter is desirable when taking the current letter , If it's not advisable, look at the next , If it is desirable, take out the letter .
This is very consistent with the characteristics of recursion , We only care about whether the current letter is desirable every time .
We can give an array of subscripts , The initial values of the subscript array are all assigned 0, The length of the subscript array is the same as the length of the input string . When a character in the string is selected , Then the corresponding value of this character in the subscript array is changed to 1. And store this character into the string array we want to output at last , When the last string array has enough three characters , We just let it output , This is also the condition for recursion to stop .
The code is as follows :
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "tool.h"
#define targetCount 3
boolean isSafe(int *arr, int index);
void completeArrangement(int *arr, int index, char *str, char *target, int temp);
void completeArrangement(int *arr, int index, char *str ,char *target, int temp) {
int i;
if (temp == 3) {
printf("%s\n",target);
return;
}
for (i = 0; i < strlen(str); i++) {
if (isSafe(arr,i)) {
arr[i] = 1;
target[temp] = str[i];
completeArrangement(arr, index+1, str , target, temp+1);
arr[i] = 0;
}
}
}
boolean isSafe(int *arr, int index) {
if (arr[index] == 1) {
return FALSE;
} else {
return TRUE;
}
}
int main() {
char str[5];
int length;
int *arr = NULL;
int temp = 0;
char target[targetCount];
printf("please input your string:");
gets(str);
length = strlen(str);
arr = (int *) calloc(sizeof(int),length);
completeArrangement(arr, 0, str , target, temp);
free(arr);
return 0;
}
#ifndef _TOOL_H_
#define _TOOL_H_
typedef unsigned char boolean;
#define TRUE 1
#define FALSE 0
#define NOT_FOUND -1
#endif
About recursion :
Many people think recursion is very tall , It is difficult to , In fact, recursion is the repeated invocation of a method , Don't think too much when writing recursive programs , The simpler and purer the consideration, the better , And when writing recursion, don't start with the core part , contrary , When writing recursive programs, you must be very clear about the end conditions of recursion , Otherwise, it is easy to cause system stack overflow when the program is running .
Writing is not good , Please correct me .
边栏推荐
- Servlet基本原理与常见API方法的应用
- MongoDB数据日期显示相差8小时 原因和解决方案
- 183 sets of free resume templates to help everyone find a good job
- Ruby time format conversion strftime MS matching format
- uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
- Dynamic address book
- Number of relationship models
- C language - stack
- C language structure to realize simple address book
- Exercise 9-3 plane vector addition (15 points)
猜你喜欢

Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)

DML statement of MySQL Foundation

Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing

Development guidance document of CMDB

How do microservices aggregate API documents? This wave of show~
![[200 opencv routines] 218 Multi line italic text watermark](/img/3e/537476405f02f0ebd6496067e81af1.png)
[200 opencv routines] 218 Multi line italic text watermark

Static comprehensive experiment ---hcip1

Today's sleep quality record 78 points

Introduction to tree and binary tree

Two way process republication + routing policy
随机推荐
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
What is devsecops? Definitions, processes, frameworks and best practices for 2022
Whether a person is reliable or not, closed loop is very important
用数据告诉你高考最难的省份是哪里!
Some summaries of the third anniversary of joining Ping An in China
If you don't know these four caching modes, dare you say you understand caching?
Online troubleshooting
A little feeling
When I forget how to write SQL, I
Knapsack problem and 0-1 knapsack problem
Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
View CSDN personal resource download details
Lavel document reading notes -how to use @auth and @guest directives in lavel
VLAN part of switching technology
Deep learning 500 questions
How can Huawei online match improve the success rate of player matching
Ruby time format conversion strftime MS matching format
183 sets of free resume templates to help everyone find a good job
Rhcsa12