1. 将字符串s1中所有出现在字符串s2中的字符删去
using System; namespace ConsoleApplication56 { class Program { static void Main(string[] args) { string s1 = "aaabbccabfcabcc"; string s2 = "abcd"; char[] results = new char[s1.Length]; int m=0; int j, i; for (i = 0; i < s1.Length; i++) { for (j=0; j < s2.Length; j++) { if (s1[i] != s2[j]) { continue; } else { j = 0; break; } } if (j == s2.Length) { results[m++] = s1[i]; } } foreach (char a in results) { Console.WriteLine(a.ToString()); } Console.ReadLine(); } } }
2. 计算数字出现次数(using LinkedList)using System; using System.Collections.Generic; namespace ConsoleApplication57 { class Program { static void Main(string[] args) { int[] nums = { 1, 2, 3, 4, 2, 1 }; int count = 0; if (nums != null) { LinkedList<int> numbers = new LinkedList<int>(nums); int givenInt = 1; LinkedListNode<int> currentNode = numbers.First; while (currentNode != null) { if (currentNode.Value == givenInt) { count++; } currentNode = currentNode.Next; } } Console.WriteLine(count); Console.ReadLine(); } } }3. 删除重复元素
using System; namespace ConsoleApplication41 { class Program { static void Main(string[] args) { Console.WriteLine("Please input the size of the int Array:"); int size = Int32.Parse(Console.ReadLine()); int[] array = new int[size]; Console.WriteLine("Please input {0} numbers:", size); if (size > 0) { for (int i = 0; i < size; i++) { array[i] = GetInputNum(); } int[] result = DelDupElements(array); foreach (int i in result) { Console.WriteLine(i); } } else { Console.WriteLine("Please input new array with more than zero numbers"); } Console.ReadLine(); } /// <summary> /// /// </summary> /// <returns></returns> public static int GetInputNum() { try { return Int32.Parse(Console.ReadLine()); } catch { Console.WriteLine("Please input a number"); return GetInputNum(); } } /// <summary> /// /// </summary> /// <param name="a"></param> /// <returns></returns> public static int[] DelDupElements(int[] a) { // Special judgement if (a.Length == 0) { Console.WriteLine("Please input new array with more than zero numbers"); return null; } else { // Sort the array Sort(a); Console.WriteLine("The sorted array:"); foreach (int i in a) { Console.WriteLine(i); } Console.ReadLine(); int[] resultArrary; int j = 1; int size = 1; for (int i = 1; i < a.Length; i++) { if (a[i] != a[i - 1]) { size++; } } resultArrary = new int[size]; resultArrary[0] = a[0]; for (int i = 1; i < a.Length; i++) { if (a[i] != a[i - 1]) { resultArrary[j++] = a[i]; } } return resultArrary; } } /// <summary> /// /// </summary> /// <param name="a"></param> public static void Sort(int[] a) { int temp; for (int i = 1; i < a.Length; i++) { for (int j = 0; j < a.Length - i; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } } }4. 合并有序数组
using System; namespace ConsoleApplication62 { class Program { static void Main(string[] args) { int[] a = { 1, 2, 2, 3, 5, 6, 7, 7 }; int[] b = { 1, 2, 3, 4, 8, 8, 9, 10, 11, 12, 12, 13, 14 }; int[] c = MergeList(a, b); foreach (int i in c) { Console.WriteLine(i); } Console.ReadLine(); } public static int[] MergeList(int[] a, int[] b) { if (checkSort(a) && checkSort(b)) { int len = a.Length + b.Length; int[] result = new int[len]; int i = 0, j = 0, k = 0; while (i < a.Length && j < b.Length) { if (a[i] < b[j]) { result[k++] = a[i++]; } else { result[k++] = b[j++]; } } while (i < a.Length) { result[k++] = a[i++]; } while (j < b.Length) { result[k++] = b[j++]; } return result; } else { return null; } } public static bool checkSort(int[] a) { for (int i = 0; i < a.Length - 1; i++) { for (int j = i + 1; j < a.Length; j++) { if (a[j - 1] > a[j]) { return false; } } } return true; } } }5. 有一列数1,1,2,3,5,........求第30个数.public class MainClass { public static void Main() { Console.WriteLine(Foo(12)); Console.ReadLine(); } public static int Foo(int i) { if (i <= 0) return 0; else if (i > 0 && i <= 2) return 1; else return Foo(i - 1) + Foo(i - 2); } }
6. 冒泡排序、选择排序、插入排序// 冒泡排序 bubble sort public static int[] BubbleSort(int[] array) { int temp; bool isDone = false; for (int i = 0; i < array.Length && !isDone; i++) { isDone = true; for (int j = 0; j < array.Length - i - 1; j++) { if (array[j] > array[j + 1]) { isDone = false; temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } return array; }// 插入排序 Insertion sort public static int[] InsertionSort(int[] array) { for (int i = 1; i < array.Length; i++) { int current = array[i]; int j = i; while (j > 0 && current < array[j - 1]) { array[j] = array[j - 1]; j--; } array[j] = current; } return array; }
// 选择排序 Selection sort public static int[] SelectionSort(int[] array) { for (int i = 0; i < array.Length; i++) { int min = i; for (int j = i + 1; j < array.Length; j++) { if (array[j] < array[min]) { min = j; } } int temp = array[i]; array[i] = array[min]; array[min] = temp; } return array; }
7. 找第二大的数// 获得数组中的第二大数 public static int SecondMax(int[] array) { int max = array[0]; int secondMax = array[0]; for (int i = 0; i < array.Length; i++) { if (array[i] > max) { secondMax = max; max = array[i]; } else if (array[i] > secondMax && array[i] != max) { secondMax = array[i]; } } if (secondMax == max) { throw new Exception("Have no second max!"); } return secondMax; }
8. 有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"
// 反转单词顺序 public static string WordReverse(string str) { char[] array = str.ToArray(); CharArrayReverse(array, 0, array.Length - 1); int start = -1; int end = -1; for (int i = 0; i < array.Length; i++) { if (!((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z'))) { if (start < end) { CharArrayReverse(array, start + 1, end); } start = i; } else { end = i; } } return new string(array); } public static void CharArrayReverse(char[] array, int start, int end) { if (array != null && start < array.Length && end < array.Length) while (start < end) { char temp = array[start]; array[start] = array[end]; array[end] = temp; start++; end--; } }
9. A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些 人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加;
(4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。
public static void Main() { char[] name = { 'A', 'B', 'C', 'D', 'E' }; int[] value = new int[5]; for (value[0] = 0; value[0] < 2; value[0]++) { for (value[1] = 0; value[1] < 2; value[1]++) { for (value[2] = 0; value[2] < 2; value[2]++) { for (value[3] = 0; value[3] < 2; value[3]++) { for (value[4] = 0; value[4] < 2; value[4]++) { if ((value[1] >= value[0]) && (value[1] + value[2] == 1) && (value[2] == value[3]) && (value[3] + value[4] == 1) && (value[4] == 0 || value[4] == 1 && value[0] == 1 && value[3] == 1)) { for (int i = 0; i < 5; i++) { if (value[i] == 1) { Console.WriteLine("{0}Join", name[i]); } else { Console.WriteLine("{0}Not Join", name[i]); } } } } } } } } Console.ReadLine(); }10. 一个用户输入一个整数的值在一个文本框。不使用内置的图书馆,将数字串的整数表示.
public static int StringToInt(string s) { int sum = 0; for (int i = 0; i < s.Length; i++) { sum = sum * 10 + (s[i] - '0'); } return sum; }