zoukankan      html  css  js  c++  java
  • C#:算法题

    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.I have two strings the ending substring of first is the starting substring of the second,ex

    string left : ONESTRING
    string right : STRINGTWO
    

    I have to merge them so that the resulting string is

    result string : ONESTRINGTWO
    
    using System;
    
    namespace ConsoleApplication58
    {
        class Program
        {
            static void Main(string[] args)
            {
                string left = "OneStart";
                string right = "StartTwo";
                string result = FindAndMerge(left, right);
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
            public static string FindAndMerge(string left,string right)
            {
                int i=0;
                string result=string.Empty;
    
                for (i = right.Length; i >= 0; i--)
                {
                    if (left.Contains(right.Substring(0, i)))
                    {
                        break;
                    }
                }
    
                if (i < right.Length)
                {
                    result = right.Substring(i);
                }
    
                result = left + result;
                return result;
            }
        }
    }

    4. 删除重复元素

    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;
                        }
                    }
                }
            }
        }
    }

    另外几种种写法:

    public static void DeleteDuplicateNumbers(int[] array)
            {
                if (array != null)
                {
                    List<int> result = new List<int>();
                    if (array.Length > 1)
                    {
                        for (int i = 0; i < array.Length; i++)
                        {
                            if (!result.Contains(array[i]))
                            {
                                result.Add(array[i]);
                            }
                        }
                    }
                    else
                    {
                        result.Add(array[0]);
                    }
                    foreach (int i in result)
                    {
                        Console.WriteLine(i);
                    }
                    Console.ReadLine();
                }
            }
    public static void DeleteDuplicateNumbersUsingIndexOf(int[] array)
            {
                if (array != null)
                {
                    List<int> result = new List<int>();
                    if (array.Length > 1)
                    {
                        for (int i = 0; i < array.Length; i++)
                        {
                            if (result.IndexOf(array[i]) < 0)
                            {
                                result.Add(array[i]);
                            }
                        }
                    }
                    else
                    {
                        result.Add(array[0]);
                    }
                    foreach (int i in result)
                    {
                        Console.WriteLine(i);
                    }
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("The array is null");
                }
            }
            public static void DeleteDuplicateNumbersUsingInsert(int[] array)
            {
                if (array != null)
                {
                    List<int> result = new List<int>();
                    result.Add(array[0]);
                    if (array.Length > 1)
                    {
                        for (int i = 1; i < array.Length; i++)
                        {
                            if (array[i] < result[0])
                            {
                                result.Insert(0, array[i]);
                            }
                            else if (array[i] > result[result.Count - 1])
                            {
                                result.Add(array[i]);
                            }
                            else
                            {
                                for (int j = 1; j < result.Count-1; j++)
                                {
                                    if (array[i] > result[j-1] && array[i] < result[j])
                                    {
                                        result.Insert(j, array[i]);
                                    }
                                }
                            }
                        }
                    }
                    foreach (int i in result)
                    {
                        Console.WriteLine(i);
                    }
                    Console.ReadLine();
                }
            }
            public static int[] DeleteDuplicateNumbersUsingArrayList(int[] list)
            {
                ArrayList arr = new ArrayList();
                for (int i = 0; i < list.Length - 1; i++)
                {
                    if (list[i] != list[i + 1])
                    {
                        arr.Add(list[i]);
                    }
                }
                arr.Add(list[list.Length - 1]);
                int[] getarr = new int[arr.Count];
                for (int i = 0; i < arr.Count; i++)
                {
                    getarr[i] = Convert.ToInt32(arr[i]);
                }
    
                return getarr;
            }

    5. 合并有序数组

    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;
            }
        }
    }

     6. 有一列数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);
            }
        }


    7. 冒泡排序、选择排序、插入排序

       // 冒泡排序 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;
            }


    8. 找第二大的数

    // 获得数组中的第二大数
            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;
            }

    9. 有一个字符串 "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--;
                    }
            }

    10.  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();
            }

    11. a user entered an integer value into a text box. Without using a buit-in library, convert the numeric string to its integer representation.

            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;
    
            }
  • 相关阅读:
    Python爬虫入门教程 59-100 python爬虫高级技术之验证码篇5-极验证识别技术之二
    CouchDB简介
    零成本打造抖音IP,轻松实现月入过万,90%的人都不懂
    couchdb集群搭建
    汽车测评详细操作流程,一篇赚300+
    基于docker部署的微服务架构: docker环境下的zookeeper和kafka部署
    零成本的互联网赚钱项目,都是怎么做的?
    SQuirrel连接hive配置
    本人有8万启动资金,做点什么生意好呢?
    PHP实现自己活了多少岁
  • 原文地址:https://www.cnblogs.com/LilianChen/p/3036063.html
Copyright © 2011-2022 走看看