题目列表

从某个地方找到的若干道关于C#数组的习题,我感觉这正是我欠缺的部分,因此将它们都做了一遍。

我的解答

下面这些是我的解法,不一定是最优解(甚至不一定正确)。毕竟在程序中,通向正确的路径不止一条。

再来一瓶

1
2
3
4
5
6
7
8
9
10
// 3 个可乐瓶可以换一瓶可乐,现在有 364 瓶可乐。问一共可以喝多少瓶可乐,剩下几个空瓶
uint bottles = 364, drink = 0;
while (bottles >= 3) {
// 喝三瓶
drink += 3;
bottles -= 3;
// 兑换一瓶
bottles += 1;
}
Console.WriteLine("可以喝{0}瓶可乐,还剩{1}个空瓶", drink + bottles, bottles);

加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 加密
Console.WriteLine("请输入原文:");
string OriginalText = Console.ReadLine();
string cipherText = "";
foreach (char text in OriginalText) {
char resultText;
if ((text >= &#x27;a&#x27; && text < &#x27;x&#x27;) || (text >= &#x27;A&#x27; && text < &#x27;X&#x27;)) {
resultText = (char)(text + 2);
}
else if ((text >= &#x27;x&#x27; && text <= &#x27;z&#x27;) || (text >= &#x27;X&#x27; && text <= &#x27;Z&#x27;)) {
resultText = (char)(text - 23);
}
else {
resultText = text;
}
cipherText += resultText;
}
Console.WriteLine("文本加密后是:");
Console.WriteLine(cipherText);

排序

1
2
3
4
5
6
7
8
9
10
11
// 排序
Console.WriteLine("请输入一组数字,数字之间用空格分割");
string[] numStrList = Console.ReadLine().Split(" ");
int[] numList = new int[numStrList.Length];
for (int i = 0; i < numStrList.Length; i++) {
numList[i] = int.Parse(numStrList[i]);
}
Array.Sort(numList);
foreach (int num in numList) {
Console.WriteLine(num);
}

最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 寻找最小值,然后将它与最前面的数交换
Console.WriteLine("请输入一组数字,数字之间用空格分割");
string[] numStrList = Console.ReadLine().Split(" ");
int[] numList = new int[numStrList.Length];
for (int i = 0; i < numStrList.Length; i++) {
numList[i] = int.Parse(numStrList[i]);
}
// 开始比较
int minNum = 0x7fffffff;
int index, minNumIndex = 0;
for (index = 0; index < numStrList.Length; index++) {
minNum = Math.Min(numList[index], minNum);
minNumIndex = (minNum == numList[index] ? index : minNumIndex);
}
(numList[0], numList[minNumIndex]) = (numList[minNumIndex], numList[0]);
Console.WriteLine("这组数字交换后是:");
foreach (int num in numList) Console.Write(num + " ");

插入

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
// 排序,另外插入一个数,使新序列依然有序
Console.WriteLine("请输入一组数字,数字之间用空格分割");
string[] numStrList = Console.ReadLine().Split(" ");
int[] numList = new int[numStrList.Length];
for (int i = 0; i < numStrList.Length; i++) {
numList[i] = Convert.ToInt32(numStrList[i]);
}
Array.Sort(numList);
Console.WriteLine("这组数字排序后是:");
foreach (int num in numList) Console.Write(num + " ");
Console.WriteLine();
Console.WriteLine("请再输入一个整数:");
int newNum = Convert.ToInt32(Console.ReadLine());
int insertIndex = 0;
int[] newNumList = new int[numList.Length + 1];
for (int i = 0; i < numList.Length; i++) {
if (numList[i] >= newNum) {
insertIndex = i - 1;
break;
}
}
for (int i = 0; i < insertIndex; i++) {
newNumList[i] = numList[i];
}
newNumList[insertIndex] = newNum;
for (int i = insertIndex + 1; i < newNumList.Length; i++) {
newNumList[i] = numList[i - 1];
}
Console.WriteLine("插入这个整数后,这组数字是:");
foreach (int num in newNumList) Console.Write(num + " ");

语法检查

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
// 判断字符串是否是C#合法标识符
Console.WriteLine("请输入标识符:");
string identifier = Console.ReadLine();
bool isLegal = true;
string[] keywords = {
"abstract", "as",
"base", "bool", "break", "byte", "case",
"catch", "char", "checked", "class", "const", "continue",
"decimal", "default", "delegate", "do", "double",
"else", "enum", "event", "explicit", "extern ",
"false", "finally", "fixed", "float", "for", "foreach",
"goto",
"if", "implicit", "in", "int", "interface", "internal ", "is ",
"lock", "long",
"namespace", "new", "null",
"object", "operator", "out", "override",
"params", "private", "protected", "public",
"readonly", "ref", "return",
"sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch",
"this", "throw", "true", "try", "typeof",
"uint", "ulong", "unchecked", "unsafe", "ushort", "using",
"virtual", "void", "volatile",
"while",
};
foreach (string keyword in keywords) {
if (identifier == keyword) {
isLegal = false;
Console.WriteLine("标识符不能和C#的关键字重复!");
}
}
char headIdentifier = identifier[0];
if (headIdentifier >= &#x27;0&#x27; && headIdentifier <= &#x27;9&#x27;) {
isLegal = false;
Console.WriteLine("标识符不能以数字开头!");
}
if (isLegal) {
Console.WriteLine("这是一个合法的C#标识符。");
}

回文串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 回文串
Console.WriteLine("请输入字符串:");
string sentence = Console.ReadLine();
bool isPlalindrome = true;
for (int i = 0; i < sentence.Length / 2; i++) {
if (sentence[i] != sentence[sentence.Length - 1 - i]) {
isPlalindrome = false;
Console.WriteLine("这个不是回文");
break;
}
}
if (isPlalindrome) {
Console.WriteLine("这个是回文");
}

炎炎夏日

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// “上升趋势”
Console.WriteLine("请输入一组数字,数字之间用空格分割");
string[] numStrList = Console.ReadLine().Split(" ");
int[] numList = new int[numStrList.Length];
for (int i = 0; i < numStrList.Length; i++) {
numList[i] = Convert.ToInt32(numStrList[i]);
}
int riseStep = 1, maxRiseStep = 1;
for (int i = 1; i < numList.Length; i++) {
if (numList[i] >= numList[i-1]) {
riseStep++;
}
else if (riseStep > maxRiseStep) {
maxRiseStep = riseStep;
riseStep = 1;
}
}
Console.WriteLine(maxRiseStep);

次等

1
2
3
4
5
6
7
8
9
// 次等最大
Console.WriteLine("请输入一组数字,数字之间用空格分割");
string[] numStrList = Console.ReadLine().Split(" ");
int[] numList = new int[numStrList.Length];
for (int i = 0; i < numStrList.Length; i++) {
numList[i] = Convert.ToInt32(numStrList[i]);
}
Array.Sort(numList);
Console.WriteLine(numList[numList.Length - 2]);

字频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 统计词频
Console.WriteLine("请输入字符串:");
string sentence = Console.ReadLine();
List words = [sentence[0]];
List wordFrequencys = [0];
foreach (char word in sentence) {
for (int i = 0; i < words.Count; i++) {
if (word == words[i]) {
wordFrequencys[i]++;
break;
}
else if (i == words.Count - 1) {
words.Add(word);
wordFrequencys.Add(1);
}
}
}
for (int i = 0; i < words.Count; i++) {
string bar = "";
for (int j = 0; j < wordFrequencys[i]; j++) {
bar += "#";
}
Console.WriteLine("{0}:\t{1}", words[i], bar);
}

倒反天罡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 大小写字母互换
Console.WriteLine("请输入字符串:");
string sentence = Console.ReadLine();
char[] newSentence = new char[sentence.Length];
int toggleCase = &#x27;a&#x27; - &#x27;A&#x27;;
char letter;
for (int i = 0; i < sentence.Length; i++) {
if (sentence[i] >= &#x27;a&#x27; && sentence[i] <= &#x27;z&#x27;) {
letter = (char)(sentence[i] - toggleCase);
newSentence[i] = letter;
}
else if (sentence[i] >= &#x27;A&#x27; && sentence[i] <= &#x27;Z&#x27;) {
letter = (char)(sentence[i] + toggleCase);
newSentence[i] = letter;
}
else {
newSentence[i] = sentence[i];
}
}
Console.WriteLine("字母大小写互换后,这个句子是:");
Console.WriteLine(newSentence);

查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 在数组中查找值
Console.WriteLine("请输入一组数字,数字之间用空格分割");
string[] numStrList = Console.ReadLine().Split(" ");
int[] numList = new int[numStrList.Length];
for (int i = 0; i < numStrList.Length; i++) {
numList[i] = Convert.ToInt32(numStrList[i]);
Console.WriteLine("请输入要查找的数字:");
int numSearch = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < numList.Length; i++) {
if (numList[i] == numSearch) {
Console.WriteLine("这个数字在第 {0} 个。", i+1);
break;
}
}

预算与储蓄

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
// 管理张三的零花钱
// 每月进账¥300
// 可以储蓄¥100 * n;到年底返回¥120 * n
// 储蓄策略:每月初 (储蓄 + 预算) - (进账 + 本金) < ¥100
// 风险:进账 + 本金 < 预算
Console.WriteLine("请输入本年度预算,预算值间用空格分割:");
string[] budgetStrList = Console.ReadLine().Split(" ");
int[] budgetList = new int[12];
for (int i = 0; i < budgetStrList.Length; i++) {
budgetList[i] = (budgetStrList[i] == null ? 0 : Convert.ToInt32(budgetStrList[i]));
}
// 进账、储蓄、总储蓄、本金
int income = 300, saving, savingTotal = 0, capital = 0;
for (byte M = 0; M < 12; M++) {
byte month = (byte)(M + 1);
capital += income;
if (capital >= budgetList[M]) {
capital -= budgetList[M];
saving = capital/100*100;
capital -= saving;
savingTotal += (int)(saving*1.2);
Console.WriteLine("到{0}月末,张三还有{1}元。本月他预算是{2}元,存了{3}元。", month, capital, budgetList[M], saving);
if (month == 12) {
capital += savingTotal;
Console.WriteLine(capital);
}
}
else {
Console.WriteLine("-" + month);
break;
}
}