面试算法题汇总

1. 编码实现:数组奇数在前面,偶数在后面  

  static void Main(string[] args)
        {
            //定义一个数组
            int[] arr = new int[] { 1, 23, 2, 34, 21, 45, 26, 22, 41, 66, 3 };

            //声明一个新数组,用于保存排序之后的内容
            int[] newArr = new int[arr.Length];
            //定义一个最小索引
            int begin = 0;
            //定义一个最大索引
            int end = arr.Length;
            for (int i = 0; i < arr.Length; i++)
            {
                if (begin >= end)
                    break;

                if (arr[i] % 2 == 0)
                {
                    //如果是偶数,则从最小索引位置开始添加
                    newArr[begin] = arr[i];
                    begin++;
                }
                else
                {
                    //如果是奇数,则从最大索引位置开始添加
                    newArr[end] = arr[i];
                    end--;
                }
            }
            Console.Read();
        }
View Code

 2.有一列数1,1,2,3,5,........求第30个数. 

 public static int GetNumber(int i)
        {
            if (i < 0)
                return 0;
            else if (i == 1 || i == 2)
                return 1;
            else
                return GetNumber(i - 1) + GetNumber(i - 2);
        }
View Code

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

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

namespace Demo_Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "I am a good man";

            Console.WriteLine(Reverse1(str));
            Console.WriteLine(Reverse2(str));
            Console.Read();
        }

        /// <summary>
        /// 方法1
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Reverse1(string str)
        {
            string[] strArr = str.Split(' ');

            if (strArr == null || strArr.Length <= 0)
                return str;

            int beginIndex = 0;    
            int endIndex = strArr.Length - 1;  
                        
            string strTemp = string.Empty;
            while (beginIndex < endIndex)
            {
                strTemp = strArr[endIndex];
                strArr[endIndex] = strArr[beginIndex];
                strArr[beginIndex] = strTemp;

                beginIndex++;
                endIndex--;
            }

            return string.Join(" ", strArr);
        }

        /// <summary>
        /// 方法2
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Reverse2(string str)
        {
            string[] strArr = str.Split(' ');

            if (strArr == null || strArr.Length <= 0)
                return str;

            StringBuilder sb = new StringBuilder();
            for (int i = strArr.Length - 1; i >= 0; i--)
            {
                sb.Append(strArr[i]);
                sb.Append(" ");
            }

            string result = sb.ToString().TrimEnd();

            return result;
        }
    }
}
View Code

4.A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛:
   (1)A参加时,B也参加;
   (2)B和C只有一个人参加;
   (3)C和D或者都参加,或者都不参加;
   (4)D和E中至少有一个人参加;
   (5)如果E参加,那么A和D也都参加。

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

namespace Demo_Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] stu = new Student[]
            { 
                new Student("A"),
                new Student("B"),
                new Student("C"),
                new Student("D"),
                new Student("E")
            };

            games(stu);
            for (int i = 0; i < stu.Length; i++)
            {
                Console.Write(stu[i].Name);
                if (stu[i].join)
                    Console.WriteLine("去了");
                else
                    Console.WriteLine("没去");
            }

            Console.Read();
        }

        static void games(Student[] stu)
        {
            if (stu[0].join)
                stu[1].join = true;

            if (stu[1].join)
                stu[2].join = false;
            else
                stu[2].join = true;

            stu[3].join = stu[2].join;

            if (!stu[3].join)
                stu[4].join = true;

            if (stu[4].join)
            {
                if (stu[0].join == stu[3].join)
                    return;
                else
                {
                    stu[0].join = false;
                    games(stu);
                }
            }
        }
    }

    public class Student
    {
        public string Name { get; set; }

        public bool join { get; set; }

        public Student(string name)
        {
            this.Name = name;
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/qianxingdewoniu/p/5345433.html