当前位置:网站首页>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 .
边栏推荐
- 有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
- AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
- Es entry series - 6 document relevance and sorting
- Check 15 developer tools of Alibaba
- 7-17 crawling worms (15 points)
- 【Day2】 convolutional-neural-networks
- Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
- Introduction to tree and binary tree
- Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
- OSPF summary
猜你喜欢

Rhcsa learning practice

How do microservices aggregate API documents? This wave of show~

OSPF comprehensive experiment

When I forget how to write SQL, I

The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago

Idea SSH channel configuration

【OpenCV 例程200篇】218. 多行倾斜文字水印

VLAN part of switching technology

Recursion and divide and conquer strategy

leetcode1-3
随机推荐
Some summaries of the third anniversary of joining Ping An in China
BGP advanced experiment
Basic data types of MySQL
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
如果不知道這4種緩存模式,敢說懂緩存嗎?
Es entry series - 6 document relevance and sorting
Velodyne configuration command
Map container
Es advanced series - 1 JVM memory allocation
Custom type: structure, enumeration, union
uniapp---初步使用websocket(长链接实现)
When I forget how to write SQL, I
Rhcsa - day 13
system design
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
Tables in the thesis of latex learning
Sword finger offer 31 Stack push in and pop-up sequence
Intelligent gateway helps improve industrial data acquisition and utilization
Deep learning 500 questions
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)