1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| // int[] nums = { 1, 2, 3, 4, 5 };
// 根据数组生成所有可能的子数组
// 此例为12345 1234 123 12 1 2345 234 23 2 345 34 3 45 4 5
static IList<ilist<int>> test3(int[] nums)
{
IList<ilist<int>> result = new List<ilist<int>>();
for (int i = 0; i < nums.Length; i++)
{
List<int> list = new List<int>(nums);
for (int j = 0; j < i; j++)
{
list.RemoveAt(0);
}
for (int k = 0; k < list.Count; k++)
{
List<int> list2 = new List<int>(list);
for (int l = 0; l < k; l++)
{
if (list2.Count > 0)
list2.RemoveAt(list2.Count - 1);
}
result.Add(list2);
}
}
return result;
}
// string mes = "{()[]}";
// 判断字符串内的各种括号是否符合规则
static bool test2(string mes)
{
List<char> list = new List<char>();
foreach (char c in mes)
{
if (c == '{' || c == '[' || c == '(')
{
list.Add(c);
}
else if (c == '}')
{
if (list.Last() != '{')
return false;
list.RemoveAt(list.Count - 1);
}
else if (c == ']')
{
if (list.Last() != '[')
return false;
list.RemoveAt(list.Count - 1);
}
else if (c == ')')
{
if (list.Last() != '(')
return false;
list.RemoveAt(list.Count - 1);
}
}
return list.Count == 0;
}
// string[] mes = { "5", "-2", "4", "C", "D", "9", "+", "+" };
// 根据字符数据做运算
static int test1(string[] mes)
{
List<int> list = new List<int>();
foreach (string s in mes)
{
Console.WriteLine(s);
if (s == "C")
{
if (list.Count > 0)
list.RemoveAt(list.Count - 1);
}
else if (s == "D")
{
if (list.Count > 0)
{
int a = list.Last();
list.Add(a * 2);
}
}
else if (s == "+")
{
if (list.Count > 1)
{
int a = list.Last();
int b = list[list.Count - 2];
list.Add(a + b);
}
}
else
{
list.Add(int.Parse(s));
}
}
return list.Sum();
} |
// int[] nums = { 1, 2, 3, 4, 5 };
// 根据数组生成所有可能的子数组
// 此例为12345 1234 123 12 1 2345 234 23 2 345 34 3 45 4 5
static IList<ilist<int>> test3(int[] nums)
{
IList<ilist<int>> result = new List<ilist<int>>();
for (int i = 0; i < nums.Length; i++)
{
List<int> list = new List<int>(nums);
for (int j = 0; j < i; j++)
{
list.RemoveAt(0);
}
for (int k = 0; k < list.Count; k++)
{
List<int> list2 = new List<int>(list);
for (int l = 0; l < k; l++)
{
if (list2.Count > 0)
list2.RemoveAt(list2.Count - 1);
}
result.Add(list2);
}
}
return result;
}
// string mes = "{()[]}";
// 判断字符串内的各种括号是否符合规则
static bool test2(string mes)
{
List<char> list = new List<char>();
foreach (char c in mes)
{
if (c == '{' || c == '[' || c == '(')
{
list.Add(c);
}
else if (c == '}')
{
if (list.Last() != '{')
return false;
list.RemoveAt(list.Count - 1);
}
else if (c == ']')
{
if (list.Last() != '[')
return false;
list.RemoveAt(list.Count - 1);
}
else if (c == ')')
{
if (list.Last() != '(')
return false;
list.RemoveAt(list.Count - 1);
}
}
return list.Count == 0;
}
// string[] mes = { "5", "-2", "4", "C", "D", "9", "+", "+" };
// 根据字符数据做运算
static int test1(string[] mes)
{
List<int> list = new List<int>();
foreach (string s in mes)
{
Console.WriteLine(s);
if (s == "C")
{
if (list.Count > 0)
list.RemoveAt(list.Count - 1);
}
else if (s == "D")
{
if (list.Count > 0)
{
int a = list.Last();
list.Add(a * 2);
}
}
else if (s == "+")
{
if (list.Count > 1)
{
int a = list.Last();
int b = list[list.Count - 2];
list.Add(a + b);
}
}
else
{
list.Add(int.Parse(s));
}
}
return list.Sum();
}