题目列表

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

  1. 3 个可乐瓶可以换一瓶可乐,现在有 364 瓶可乐。问一共可以喝多少瓶可乐,剩下几个空瓶
  2. 编写⼀个应⽤程序⽤来输⼊的字符串进⾏加密,对于字⺟字符串加密规则如下:‘a’→’d’ ‘b’→’e’ ‘w’→’z’ …… ‘x’→’a’ ‘y’→’b’ ‘z’→’c’‘A’→’D’ ‘B’→’E’ ‘W’→’Z’ …… ‘X’→’A’ ‘Y’→’B’ ‘Z’→’C’?对于其他字符,不进⾏加密。
  3. 编写⼀个控制台程序,要求⽤户输⼊⼀组数字⽤空格间隔,对⽤户输⼊的数字从⼩到⼤输出。(Array.Sort⽅法和冒泡排序)
  4. 输⼊n(n<100)个数,找出其中最⼩的数,将它与最前⾯的数交换后输出这些数
  5. 有n(n<=100)个整数,已经按照从⼩到⼤顺序排列好,现在另外给⼀个整数x,请将该数插⼊到序列中,并使新的序列仍然有序。 输出新的序列
  6. 输⼊⼀个字符串,判断其是否是C#的合法标识符。
  7. “回⽂串”是⼀个正读和反读都⼀样的字符串,⽐如“level”或者“noon”等等就是回⽂串。请写⼀个程序判断读⼊的字符串是否是“回⽂”。
  8. 最近夏⽇炎热,令张三⾮常的不爽。最近张三开始研究天⽓的变化。历经千⾟万苦,他收集了连续N(1<N<1000000)天的最⾼⽓温数据。现在他想知道⽓温⼀直上升的最⻓连续天数。
  9. 输⼊是个不相等的正整数,输出这10个正整数中的第⼆⼤的数
  10. 描述给定⼀个只包含⼩写字⺟的字符串,请你找出第⼀个仅出现⼀次的字符。如果没有输出no。
  11. 把⼀个字符串中所有出现的⼤写字⺟都替换成⼩写字⺟,同时把⼩写字⺟替换成⼤写字⺟
  12. 在⼀个数组中查找⼀个给定的值,输出第⼀次出现的位置(从1开始)
  13. 张三总爱乱花钱。每个⽉的⽉初妈妈给张三300元钱 ,张三会预算这个⽉的花销,并且能做到实际的花销和预算相同。为了让张三学会对⾦钱的管理,妈妈提出,张三可以随时把整百的前存在她那⾥,到了12⽉31⽇,他会加上20%还给张三,也就是说,张三给妈妈的每⼀个100元,到年底都会变成120元!因此张三制定了⼀个存储计划:每个⽉的⽉初,在得到妈妈给的零花钱后,如果他预计到这个⽉的⽉末⼿中还会有多于100元或恰好100元,他就会把整百的钱存在妈妈那⾥,剩余的钱存在⾃⼰⼿⾥。⽐如11⽉初张三⼿中还有83元,妈妈留了张三300元。张三预计11⽉的花销是180元,那么他就会在妈妈那⾥存200元,⾃⼰留下183元。到了11⽉⽉末,张三⼿中会剩下3元钱。
    张三发现这个存储计划的主要⻛险是,存在妈妈那⾥的钱在年末之前不能取出。有可能在某个⽉的⽉初,张三⼿中的钱加上这个⽉妈妈给的钱,不够这个⽉的预算。如果出现这种情况,张三将不得不在这个⽉省吃简⽤,压缩预算。
    现在请你根据2019年1⽉到12⽉每个⽉张三的预算,判断会不会出现这种情况。如果储蓄计划实施过程中会出现某个⽉钱不够⽤的情况,输出-x,x表⽰出现这种情况的第⼀个⽉;如果不会,计算到2019年12⽉31⽇,妈妈将张三平时存的钱加上20%还给张三后,张三⼿⾥会有多少钱

我的解答

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

再来一瓶

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 >= 'a' && text < 'x') || (text >= 'A' && text < 'X')) {
resultText = (char)(text + 2);
}
else if ((text >= 'x' && text <= 'z') || (text >= 'X' && text <= 'Z')) {
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
31
32
// 排序,另外插入一个数,使新序列依然有序
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 >= '0' && headIdentifier <= '9') {
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<char> words = [sentence[0]];
List<int> 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 = 'a' - 'A';
char letter;
for (int i = 0; i < sentence.Length; i++) {
if (sentence[i] >= 'a' && sentence[i] <= 'z') {
letter = (char)(sentence[i] - toggleCase);
newSentence[i] = letter;
}
else if (sentence[i] >= 'A' && sentence[i] <= 'Z') {
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
15
// 在数组中查找值
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;
}
}