C#常見演算法題目(面試準備)

來源:互聯網
上載者:User

1.寫出冒泡,選擇,插入排序演算法。

    //冒泡排序
    public class bubblesorter
    {

        public void sort(int[] list)
        {
            int length = list.Length;
            for (int i = 0; i < length; i++)
            {

                for (int j = length - 1; j > i; j--)
                {
                    if (list[j - 1] > list[j])
                    {
                        int temp;
                        temp = list[j];
                        list[j] = list[j - 1];
                        list[j - 1] = temp;
                    }
                }
            }
        }

        

    }
    //選擇排序
    public class selectionsorter
    {
        private int min;
        public void sort(int[] list)
        {
            for (int i = 0; i < list.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (list[j] < list[min])
                        min = j;
                }
                int t = list[min];
                list[min] = list[i];
                list[i] = t;
            }
        }
    }
    //插入排序
    public class insertionsorter
    {
        public void sort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > t))
                {
                    list[j] = list[j - 1];
                    --j;
                }
                list[j] = t;
            }
        }
    }
2.有一列數1,1,2,3,5,........求第30個數.

public class MainClass
{
    public static void Main()
    {
        Console.WriteLine(Foo(30));
    }
    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);
    }
}

3. 程式設計: 貓大叫一聲,所有的老鼠都開始逃跑,主人被驚醒。

    public delegate void SubEventHandler();
    public abstract class Subject 
    {
        public event SubEventHandler SubEvent;
        protected void FireAway() 
        {
            if (this.SubEvent != null)
                this.SubEvent();
        }  
    }
    public class Cat : Subject 
    { 
        public void Cry() 
        {
            Console.WriteLine(cat cryed.);
            this.FireAway();
        }
    }
    public abstract class Observer 
    {
        public Observer(Subject sub) 
        {
            sub.SubEvent += new SubEventHandler(Response);
        }
        public abstract void Response();   
    }
    public class Mouse : Observer 
    {
        private string name;
        public Mouse(string name, Subject sub) : base(sub) 
        {  
            this.name = name;
        }
        public override void Response() 
        {
            Console.WriteLine(name +  attempt to escape!);
        }
    }
    public class Master : Observer 
    {
        public Master(Subject sub) : base(sub){} 
        public override void Response() 
        {
            Console.WriteLine(host waken);
        }
    }
    class Class1 
    {
        static void Main(string[] args) 
        {
            Cat cat = new Cat();
            Mouse mouse1 = new Mouse(mouse1, cat);
            Mouse mouse2 = new Mouse(mouse2, cat);
            Master master = new Master(cat);
            cat.Cry();
        }

    }
4.有一個字串 "I am a good man",設計一個函數,返回 "man good a am I"。

5.A、B、C、D、E五名學生有可能參加電腦競賽,根據下列條件判斷哪些
  人蔘加了競賽:

   (1)A參加時,B也參加;

   (2)B和C只有一個人蔘加;

   (3)C和D或者都參加,或者都不參加;

   (4)D和E中至少有一個人蔘加;

   (5)如果E參加,那麼A和D也都參加。

        static void Main(string[] args)
        {

            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}參加", name[i]);
                                        }
                                        else
                                        {
                                            Console.WriteLine("{0}不參加", name[i]);
                                        }
                                    }
                                }
                            }
}
6.題目:
a user entered an integer value into a text box. Without using a buit-in library, convert the numeric string to its integer representation.

static int StringTolnt(string s)
        {
            int sum = 0;
            for (int i = 0; i < s.Length; i++)
                sum = sum * 10 + (s[i] - '0');
            return sum;
        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.