C#程序設計大賽試題
1 英文單詞求複數問題(10 分)
題目描述:請編寫一個程序,可以將英語規則名詞(不考慮不滿足以下規則的英語單詞)由單數變成複數。已知規則如下:
- 以輔音字母 y 結尾,則將 y 改成 i,再加 es;
- 以 s,x,ch,sh 結尾,則加 es;
- 以元音 o 結尾,則加 es;
- 其他情況直接加 s。
要求用鍵盤輸入英語規則名詞,屏幕輸出該名詞的複數形式。樣例輸入:
box
樣例輸出:
Boxes
代碼如下:
class Program
{
static void Main(string[] args)
{
int i = 0;
Console.WriteLine("請輸入一個英文單詞:");
string s = Console.ReadLine();
int n = s.Length;
char[] arr = new char[n];
foreach(char c in s)
{
arr[i] = c;
i++;
}
switch (arr[n - 1])
{
case 'y':
arr[n - 1] = 'i';
foreach (char c in arr) Console.Write(c);
Console.Write("es");
break;
case 's':
case 'x':
case 'o':
foreach (char c in arr) Console.Write(c);
Console.Write("es");
break;
case 'h':
if (arr[n - 2] == 's' || arr[n - 2] == 'c')
{
foreach (char c in arr) Console.Write(c);
Console.Write("es");
}
else
{
foreach (char c in arr) Console.Write(c);
Console.Write("s");
}
break;
default:
foreach (char c in arr) Console.Write(c);
Console.Write("s");
break;
}
Console.ReadKey();
}
}
- 逆數對問題(20 分)
題目描述:設 A[1…..n]是一個包含 n 個不同數的數組,如果在 i<j 的情況下, 有 A[i]>A[j],則(i,j)就稱爲 A 中的一個逆序對。
請編寫程序,根據用戶輸入的正整數 n(n>=2)和 n 個不同的數,求出數組 A[n] 的逆序對個數。其中,第一行輸入數組包含的元素個數 n,第二行輸入 n 個不同的數(以逗號分隔)。
假設有數組 A[10],給數組輸入 10 個數,則樣例輸入:
10
1,2,3,6,4,5,7,8,9,10
樣例輸出:
2
代碼如下:
class Program
{
static void Main(string[] args)
{
int i = 0;
int m = 0;
string s = Console.ReadLine();
int n = int.Parse(s);
int[] arr = new int[n];
string s2 = Console.ReadLine();
string[] arr2 =s2.Split(',', ',');
foreach(string c in arr2)
{
arr[i] = int.Parse(c);
i++;
}
for(int j = 0; j < arr.Length; j++)
{
for(int k = j + 1; k < arr.Length; k++)
{
if (arr[j] > arr[k]) m++;
}
}
Console.WriteLine(m);
Console.ReadKey();
}
}
- 數組排序問題(20 分)
題目描述:輸入 30 個數到一個 5 行 6 列的數組,經排序後該數組各元素值按
列從小到大排列,並顯示該 2 維數組。請編寫程序實現此功能。
樣例輸入:
1 3 20 11 13 10
2 6 21 12 18 19
5 4 27 14 15 16
7 9 17 22 23 24
8 0 25 26 28 29
樣例輸出:
0 5 10 15 20 25
1 6 11 16 21 26
2 7 12 17 22 27
3 8 13 18 23 28
4 9 14 19 24 29
代碼如下:
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[5,6];
for (int i = 0; i < 5; i++)
{
string s = Console.ReadLine();
string[] arr2 = s.Split(' ');
for(int j = 0; j < 6; j++)
{
arr[i,j] = int.Parse(arr2[j]);
}
}
for (int y =0 ; y < 6; y++)
{
for (int x = 0; x < 5; x++)
{
for (int m = y; m < 6; m++)
{
if (m == y)
{
int n = x;
int b;
for (; n < 5; n++)
{
if (arr[x, y] > arr[n, m])
{
b = arr[x, y];
arr[x, y] = arr[n, m];
arr[n, m] = b;
}
}
}
else
{
int n = 0;
int b;
for (; n < 5; n++)
{
if (arr[x, y] > arr[n, m])
{
b = arr[x, y];
arr[x, y] = arr[n, m];
arr[n, m] = b;
}
}
}
}
}
}
int v = 0;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
4.連續正整數問題(20 分)
題目描述:一個正整數有可能可以被表示爲 n(n>=2) 個連續正整數之和,如:
15=1+2+3+4+5
15=4+5+6
15=7+8
請編寫程序,根據輸入的任何一個正整數,找出符合這種要求的所有連續正整數序列。
輸入數據:一個正整數,通過鍵盤輸入一個正整數。
輸出數據:在標準輸出上打印出符合題目描述的全部正整數序列,每行一個序列,每個序列都從該序列的最小正整數開始、以從小到大的順序打印。如果結果有多個序列,按各序列的最小正整數的大小從小到大打印各序列。此外,序列不允許重複,序列內的整數用一個空格分隔。如果沒有符合要求的序列,輸出“沒有符合要求的序列”。
樣例輸入:
15
樣例輸出:
1 2 3 4 5
4 5 6
7 8
樣例輸入:
16
樣例輸出:
沒有符合要求的序列
5.Windows 資源管理器設計(30 分)
題目描述:使用 C#創建一個 Windows 應用程序,在 Form 內設計一個資源管理器。
功能要求:以樹形目錄結構顯示類似於 Windows 中資源管理器的常用功能。當單擊樹形目錄中的節點時,能在右邊顯示該文件夾下所有的子文件和文件信息。並且均能夠在右邊的顯示框中,使用快捷菜單(單擊右鍵)實現文件夾的查看(以大圖標、小圖標、列表、詳細信息等方式)、新建、刪除、重命名、複製等功能。