C#的常见算法(面试)(转)

一、求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m


    //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性。

static int F1(int m)    
   {    
       int sum =0;    
       bool flag =true;    
       for (int i = 1; i <= m; i++)    
       {    
           if (flag)  //一次是默认是True,下下也为True    
               sum += i;    
           else    
               sum -= i;    
           flag = !flag;    
       
       }    
       return sum;    
   }    
       
   //通过奇偶性    
   static int F2(int m)    
   {    
       int sum = 0;    
       for (int i = 1; i <= m; i++)    
       {    
           if (i % 2 >0)  //即为奇数    
               sum += i;    
           else    
               sum -= i;    
       }    
       return sum;    
   }
View Code

二,有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"。

static string Reverse()    
         {    
             string s = "I am a good man";    
             string[] arr = s.Split(' ');    
             string res = "";    
             for (int i = arr.Length - 1; i >= 0; i--)    
             {    
                 res += arr[i];    
                 if (i > 0)    
                     res += " ";    
             }    
             return res;    
         }
View Code

三.冒泡排序

namespace BubbleSorter    
   {    
       class BubbleSorter    
       {    
           private static int[] myArray;    
           private static int arraySize;    
           public static void Sort(int[] a)    
           {    
               myArray = a;    
               arraySize = myArray.Length;    
               BubbleSort(myArray);    
           }    
       
           public static void BubbleSort(int[] myArray)    
           {    
               for (int i = 0; i < myArray.Length-1; i++)   //由于数组的特点,从0开始,但myArray的长度为5,所以需要减1,实际进行了(0~3)4趟循环    
               {    
                   for (int j =0; j < myArray.Length -1- i; j++)  //内层循环的要点是相邻比较。当j=4的时候,就推出循环了    
                   {    
                       if (myArray[j] > myArray[j + 1])    
                       {    
                           Swap(ref myArray[j], ref myArray[j + 1]);    
                       }    
                   }    
               }    
           }    
       
           private static void Swap(ref int left, ref int right)    
           {    
               int temp;    
               temp = left;    
               left = right;    
               right = temp;    
           }    
       
           static void Main(string[] args)    
           {    
               int[] a = { 2, 1, 5, 10, 9 };    
               BubbleSorter.Sort(a);    
               foreach (int i in a)    
               {    
                   Console.WriteLine(i);    
               }    
               Console.Read();    
           }    
       }    
   }
View Code

四.选择排序

选择排序是一种简单直观的排序算法。它的工作原理如下。

首先在未排序列中找到最小的元素,存放到排序序列的起始位置。然后,在从剩余未排序元素中继续寻找最小的元素,放到排序序列末尾。以此类推,直到所有元素均排序完毕。

class SelectSorter    
   {    
       private static int[] myArray;    
       private static int arraySize;    
       public static void Sort(int[] a)    
       {    
           myArray = a;    
           arraySize = myArray.Length;    
           SelectSort(myArray);    
       }    
       public static void SelectSort(int[] myArray)     
       {    
           int i, j, smallest;    
           for(i=0;i<myArray.Length-1;i++)  //数据起始位置,从0到倒数第二个数据    
           {    
               smallest = i;            //记录最小数的下标    
               for (j = i + 1; j < myArray.Length; j++)    //在剩下的数据中寻找最小数    
               {    
                   if (myArray[j] < myArray[smallest]) {    
                       smallest = j;    //如果有比它更小的,记录下标    
                   }    
               }    
               Swap(ref myArray[i], ref myArray[smallest]);   //将最小数据和未排序的第一个数交换    
           }    
       }    
       
       private static void Swap(ref int left, ref int right)    
       {    
           int temp;    
           temp = left;    
           left = right;    
           right = temp;    
       }    
       
       static void Main(string[] args)    
       {    
           int[] a = new int[] { 4, 2, 1, 6, 3 };    
           SelectSorter.Sort(a);    
           for (int i = 0; i < a.Length; i++)    
           {    
               System.Console.WriteLine(a[i]);    
           }    
           System.Console.Read();    
       }    
   }
View Code

五.有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

class Program    
   {    
       static void Main(string[] args)    
       {    
       
           //有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?    
           //分解题目    
           //条件:四个数字1、2、3、4  ;三位数:百位、十位、个位    
           //要求:互不相同;无重复数字:每个数字在三位中只出现一次    
           //结果:多少个? 都是多少?    
       
           int count = 0; //统计个数    
           for (int bw = 1; bw <= 4; bw++)    
           {    
               for (int sw = 1; sw <= 4; sw++)    
               {    
                   if (sw!= bw)  //很显然,只有百位和十位不同的情况下才能谈个位。    
                   {    
                       for (int gw = 1; gw <= 4; gw++)    
                       {    
                           if (gw != sw && gw != bw)   //百位用过的,十位就不能用;百位和十位都用过的,个位就不能用    
                           {    
                               count++;    
                               Console.WriteLine("{0}{1}{2}", bw, sw, gw);    
                           }    
                       }    
                   }    
               }    
           }    
           Console.WriteLine("一共有{0}个", count);    
           Console.Read();    
       
       }    
   }
View Code

 选择排序方法2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 选择排序
{
    //选择排序与冒泡排序比较的次数是一样的
    //选择排序的交换次数要比冒泡排序的交换次数少

    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 15, 0, 10, 50, 55, 35, 15, 20 }; //待排序数组
            SelectSort(arr);   //调用选择排序函数      
        }

        private static void SelectSort(int[] arr)
        { 
            int temp = 0;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                int minVal = arr[i]; //假设 i 下标就是最小的数
                int minIndex = i;  //记录我认为最小的数的下标

                for (int j = i + 1; j < arr.Length; j++)   //这里只是找出这一趟最小的数值并记录下它的下标
                {
                    //说明我们认为的最小值,不是最小
                    if (minVal > arr[j])    //这里大于号是升序(大于是找出最小值) 小于是降序(小于是找出最大值)
                    {
                        minVal = arr[j];  //更新这趟最小(或最大)的值 (上面要拿这个数来跟后面的数继续做比较)
                        minIndex = j;    //记下它的下标
                    }
                }
                //最后把最小的数与第一的位置交换
                temp = arr[i];    //把第一个原先认为是最小值的数,临时保存起来
                arr[i] = arr[minIndex];   //把最终我们找到的最小值赋给这一趟的比较的第一个位置
                arr[minIndex] = temp;  //把原先保存好临时数值放回这个数组的空地方,  保证数组的完整性
            }
            //控制台输出
            foreach (int item in arr)
            {
                Console.WriteLine("C#遍历:{0}", item);
            }
        }
    }
}
View Code

转载至:https://www.cnblogs.com/HiWord-ToFutureGKY/p/7742559.html

原文地址:https://www.cnblogs.com/wsl2011/p/10219336.html