Skip to content

Commit c6e1e55

Browse files
authored
Merge pull request #118 from ikesnowy/Day41
Day41
2 parents ea6a525 + 80d82c3 commit c6e1e55

File tree

19 files changed

+959
-2
lines changed

19 files changed

+959
-2
lines changed

1 Fundamental/1.3/1.3.45/1.3.45.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@
4848
<ItemGroup>
4949
<None Include="App.config" />
5050
</ItemGroup>
51+
<ItemGroup>
52+
<ProjectReference Include="..\Generics\Generics.csproj">
53+
<Project>{52b10c1b-78f7-493b-ab00-58efd921fda6}</Project>
54+
<Name>Generics</Name>
55+
</ProjectReference>
56+
</ItemGroup>
5157
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5258
</Project>

1 Fundamental/1.3/1.3.45/Program.cs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Text;
54
using System.Threading.Tasks;
5+
using Generics;
66

77
namespace _1._3._45
88
{
@@ -23,7 +23,83 @@ class Program
2323
static void Main(string[] args)
2424
{
2525
//给定输入序列,判断是否会出现下溢出。
26-
//给定输入和输出序列,判断是否相符合。
26+
string input = "- 0 1 2 3 4 5 6 7 8 9 - - - - - - - - -";
27+
Console.WriteLine(IsUnderflow(input.Split(' ')));//True
28+
input = "0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -";
29+
Console.WriteLine(IsUnderflow(input.Split(' ')));//False
30+
31+
//给定输出序列,判定是否可能。
32+
int[] output = { 4, 3, 2, 1, 0, 9, 8, 7, 6, 5 };
33+
Console.WriteLine(IsOutputPossible(output));//True
34+
output = new int[]{ 4, 6, 8, 7, 5, 3, 2, 9, 0, 1};
35+
Console.WriteLine(IsOutputPossible(output));//False
36+
}
37+
38+
/// <summary>
39+
/// 判断是否会出现下溢出。
40+
/// </summary>
41+
/// <param name="input">输入序列。</param>
42+
/// <returns></returns>
43+
static bool IsUnderflow(string[] input)
44+
{
45+
//记录栈中元素数量,如果元素数量小于 0 则会出现下溢出。
46+
int count = 0;
47+
48+
foreach (string s in input)
49+
{
50+
if (count < 0)
51+
{
52+
return true;
53+
}
54+
if (s.Equals("-"))
55+
{
56+
count--;
57+
}
58+
else
59+
{
60+
count++;
61+
}
62+
}
63+
64+
return false;
65+
}
66+
67+
static bool IsOutputPossible(int[] output)
68+
{
69+
int input = 0;
70+
int N = output.Length;
71+
Stack<int> stack = new Stack<int>();
72+
73+
foreach (int i in output)
74+
{
75+
//如果栈为空,则从输入序列中压入一个数。
76+
if (stack.IsEmpty())
77+
{
78+
stack.Push(input);
79+
input++;
80+
}
81+
82+
//如果输入序列中的所有数都已经入栈过了,跳出循环。
83+
if (input == N && stack.Peek() != i)
84+
{
85+
break;
86+
}
87+
88+
//如果输出序列的下一个数不等于栈顶的数,那么就从输入序列中压入一个数。
89+
while (stack.Peek() != i && input < N)
90+
{
91+
stack.Push(input);
92+
input++;
93+
}
94+
95+
//如果栈顶的数等于输出的数,弹出它。
96+
if (stack.Peek() == i)
97+
{
98+
stack.Pop();
99+
}
100+
}
101+
102+
return stack.IsEmpty();
27103
}
28104
}
29105
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{C5FBF10A-8FD5-42AB-BFB1-5012F3FEE320}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_1._3._46</RootNamespace>
10+
<AssemblyName>1.3.46</AssemblyName>
11+
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Program.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<None Include="App.config" />
50+
</ItemGroup>
51+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
52+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
5+
</startup>
6+
</configuration>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace _1._3._46
8+
{
9+
/*
10+
* 1.3.46
11+
*
12+
* 栈的可生成性问题中禁止出现的排列。
13+
* 若三元组 (a, b, c) 中 a<b<c 且 c 最先被弹出,a 第二,b 第三
14+
* (c 和 a 以及 a 和 b 之间可以间隔其他整数),
15+
* 那么当且仅当排列中不含这样的三元组时(如上题所述的)栈才可能生成它。
16+
*
17+
*/
18+
class Program
19+
{
20+
static void Main(string[] args)
21+
{
22+
//证明参考:http://ceeji.net/blog/forbidden-triple-for-stack-generability/
23+
//显然书中的解答已经十分明确,这里简单说明一下:
24+
//首先有结论:对于栈顶元素 Sn,栈中所有小于 Sn 的值都以递减形式保存(已经输出的不算)。
25+
//表现在输出序列中,Sn 输出之后,如果有小于 Sn 的值输出,其顺序必定是递减的。
26+
//例如序列 4 3 2 1 0 9 8 7 6 5
27+
//4 输出之后,3 2 1 0 递减输出;9 输出之后,8 7 6 5 递减输出。
28+
//依次验证其中的每个值都能满足结论。
29+
//而对于序列 4 6 8 7 5 3 2 9 0 1
30+
//对于 4,之后的 3 2 1 0 并不是以递减顺序输出的,因此这个序列是不合法的。
31+
}
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("1.3.46")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("1.3.46")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 会使此程序集中的类型
18+
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("c5fbf10a-8fd5-42ab-bfb1-5012f3fee320")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{4FA8E7F0-87A7-4084-BA27-BA5DF4CE18A1}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_1._3._47</RootNamespace>
10+
<AssemblyName>1.3.47</AssemblyName>
11+
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Program.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
<Compile Include="Steque.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\Generics\Generics.csproj">
54+
<Project>{52b10c1b-78f7-493b-ab00-58efd921fda6}</Project>
55+
<Name>Generics</Name>
56+
</ProjectReference>
57+
</ItemGroup>
58+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
59+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
5+
</startup>
6+
</configuration>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Generics;
6+
7+
namespace _1._3._47
8+
{
9+
/*
10+
* 1.3.47
11+
*
12+
* 可连接的队列、栈或 steque。
13+
* 为队列、栈或 steque(见练习 1.3.32)添加一个能够(破坏性地)链接两个同类对象的额外操作 catenation。
14+
*
15+
*/
16+
class Program
17+
{
18+
static void Main(string[] args)
19+
{
20+
Queue<string> q1 = new Queue<string>();
21+
q1.Enqueue("first");
22+
q1.Enqueue("second");
23+
24+
Queue<string> q2 = new Queue<string>();
25+
q2.Enqueue("third");
26+
q2.Enqueue("fourth");
27+
28+
q1 = Queue<string>.Catenation(q1, q2);
29+
Console.WriteLine(q1);
30+
31+
Stack<string> s1 = new Stack<string>();
32+
s1.Push("first");
33+
s1.Push("second");
34+
35+
Stack<string> s2 = new Stack<string>();
36+
s2.Push("third");
37+
s2.Push("fourth");
38+
39+
s2 = Stack<string>.Catenation(s2, s1);
40+
Console.WriteLine(s2);
41+
42+
Steque<string> sq1 = new Steque<string>();
43+
sq1.Push("first");
44+
sq1.Enqueue("second");
45+
46+
Steque<string> sq2 = new Steque<string>();
47+
sq2.Push("third");
48+
sq2.Enqueue("fourth");
49+
50+
sq1 = Steque<string>.Catenation(sq1, sq2);
51+
Console.WriteLine(sq1);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)