'commit'
This commit is contained in:
38
C++专题课程/全排列/ShjCppStudy/DFS实现全排列.cpp
Normal file
38
C++专题课程/全排列/ShjCppStudy/DFS实现全排列.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
int a[10], book[10], n;
|
||||
|
||||
//ȫ<><C8AB><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dfs
|
||||
void dfs(int step)
|
||||
{
|
||||
int i;
|
||||
if (step == n + 1) //<2F><><EFBFBD><EFBFBD>վ<EFBFBD>ڵ<EFBFBD>n+1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾǰn<C7B0><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>к<EFBFBD>
|
||||
{
|
||||
for (i = 1; i <= n; i++) //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>1-n<><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>˿<EFBFBD><CBBF>Ʊ<EFBFBD><C6B1>ţ<EFBFBD>
|
||||
{
|
||||
cout << a[i];
|
||||
}
|
||||
cout << endl;
|
||||
return; //<2F><><EFBFBD><EFBFBD>֮ǰ<D6AE><C7B0>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD>dfs<66><73><EFBFBD><EFBFBD><EFBFBD>ĵط<C4B5><D8B7><EFBFBD>
|
||||
}
|
||||
//<2F><>վ<EFBFBD>ڵ<EFBFBD>step<65><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰӦ<C7B0>÷<EFBFBD>ʲô<CAB2><C3B4>
|
||||
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>1<EFBFBD><31>2<EFBFBD><32>3<EFBFBD><33>4.....<2E>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
//<2F>ж<EFBFBD><D0B6>˿<EFBFBD><CBBF><EFBFBD>i<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (book[i] == 0)
|
||||
{
|
||||
a[step] = i; //<2F><><EFBFBD><EFBFBD>i<EFBFBD><69><EFBFBD>˿<EFBFBD><CBBF>Ʒŵ<C6B7><C5B5><EFBFBD>step<65><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
book[i] = 1; //<2F><>book[i]<5D><><EFBFBD><EFBFBD>Ϊ1<CEAA><31><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>i<EFBFBD><69><EFBFBD>˿<EFBFBD><CBBF>Ʋ<EFBFBD><C6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
dfs(step + 1); //<2F><>step<65><70><EFBFBD><EFBFBD><EFBFBD>ӷźú<C5BA><C3BA><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
book[i] = 0; //ͬʱ<CDAC>ջظղų<D5B2><C5B3>Ե<EFBFBD><D4B5>˿<EFBFBD><CBBF><EFBFBD>
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
int main() {
|
||||
n=3;
|
||||
dfs(1);
|
||||
return 0;
|
||||
}
|
||||
20
C++专题课程/全排列/ShjCppStudy/STL实现全排列.cpp
Normal file
20
C++专题课程/全排列/ShjCppStudy/STL实现全排列.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
//C++STL<54><4C>ȫ<EFBFBD><C8AB><EFBFBD>к<EFBFBD><D0BA><EFBFBD>next_permutation<6F><6E>ʹ<EFBFBD><CAB9>
|
||||
// https://blog.csdn.net/ac_gibson/article/details/45308645
|
||||
int main()
|
||||
{
|
||||
int a[3] = { 3,1,2 };
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
sort(a, a + 3);
|
||||
|
||||
//ȫ<><C8AB><EFBFBD><EFBFBD>
|
||||
do
|
||||
{
|
||||
cout << a[0] << a[1] << a[2] << endl;
|
||||
} while (next_permutation(a, a + 3));
|
||||
|
||||
return 0;
|
||||
}
|
||||
31
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.sln
Normal file
31
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.sln
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.29306.81
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ShjCppStudy", "ShjCppStudy.vcxproj", "{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Debug|x64.Build.0 = Debug|x64
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Debug|x86.Build.0 = Debug|Win32
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Release|x64.ActiveCfg = Release|x64
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Release|x64.Build.0 = Release|x64
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Release|x86.ActiveCfg = Release|Win32
|
||||
{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {9C8E3F9E-D637-4138-816C-B43DBD8F12D2}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
158
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.vcxproj
Normal file
158
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.vcxproj
Normal file
@@ -0,0 +1,158 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DFS实现全排列.cpp" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<ProjectGuid>{26EB7DDD-AA76-44F3-8780-67869B7CF8DD}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>ShjCppStudy</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
22
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.vcxproj.filters
Normal file
22
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.vcxproj.filters
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DFS实现全排列.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
6
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.vcxproj.user
Normal file
6
C++专题课程/全排列/ShjCppStudy/ShjCppStudy.vcxproj.user
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ShowAllFiles>true</ShowAllFiles>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
39
C++专题课程/全排列/ShjCppStudy/递归实现版本.cpp
Normal file
39
C++专题课程/全排列/ShjCppStudy/递归实现版本.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
//<2F><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>Ԫ<EFBFBD><D4AA>
|
||||
void prt(int arr[], int end) {
|
||||
for (int i = 0; i <= end; ++i) {
|
||||
printf("%d", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
//ȫ<><C8AB><EFBFBD><EFBFBD> c++ʵ<><CAB5>
|
||||
//https://blog.csdn.net/u013309870/article/details/68941284
|
||||
//https://blog.csdn.net/jiaobuchong/article/details/85369970
|
||||
|
||||
//https://www.cnblogs.com/kiritozhj/p/10501470.html
|
||||
// https://blog.csdn.net/sofia_m/article/details/78865892
|
||||
|
||||
void perm(int arr[], int begin, int end) {
|
||||
//<2F>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
|
||||
if (begin == end) {
|
||||
prt(arr, end);
|
||||
return;
|
||||
}
|
||||
//<2F><>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>еݹ<D0B5><DDB9>ĵݹ<C4B5><DDB9>忪<EFBFBD><E5BFAA>
|
||||
for (int i = begin; i <= end; ++i) {
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>ֵ
|
||||
swap(arr[begin], arr[i]);
|
||||
//<2F>ݹ<EFBFBD><DDB9><EFBFBD>
|
||||
perm(arr, begin + 1, end);
|
||||
//<2F>ָ<EFBFBD>ԭ״
|
||||
swap(arr[begin], arr[i]);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int arr[3] = { 3,1,2 };
|
||||
perm(arr, 0, 2);
|
||||
}
|
||||
37
C++专题课程/全排列/ShjCppStudy/非递归解法.cpp
Normal file
37
C++专题课程/全排列/ShjCppStudy/非递归解法.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
//<2F><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>Ԫ<EFBFBD><D4AA>
|
||||
void prt(int arr[], int end) {
|
||||
for (int i = 0; i <= end; ++i) {
|
||||
printf("%d", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void perm(int arr[], int len) {
|
||||
if (len < 2) return;
|
||||
int i, j, temp;
|
||||
while(1) {
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
||||
prt(arr, len - 1);
|
||||
i = j = len - 1;
|
||||
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>С<EFBFBD><D0A1>Ԫ<EFBFBD><D4AA>
|
||||
while (i > 0 && arr[i] < arr[i - 1]) --i;
|
||||
temp = i;
|
||||
if (i == 0) break;
|
||||
//<2F>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>arr[i-1]<5D><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
||||
while (temp + 1 < len && arr[temp + 1] > arr[i - 1]) ++temp;
|
||||
swap(arr[i - 1], arr[temp]); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
reverse(arr + i, arr + len); //<2F><><EFBFBD><EFBFBD>
|
||||
};
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[3] = { 3,1,2 };
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
sort(arr, arr + 3);
|
||||
//<2F><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
|
||||
perm(arr,3);
|
||||
}
|
||||
Reference in New Issue
Block a user