日常刷題——youkia

每個人都是單獨的世界,每個人都有着一段傳奇。在沒有維度的電子世界裏,0和1組成的一切,都被不確定的雲彩所籠罩着,仰望天空,我們一無所獲;閉上雙眼,用生命與心靈來感受。在愈來愈強的共鳴中,我們知道自己置身於一個真實的世界 

ps:真是想不到,有機會進這家公司。幾年前可一直當個籤呢。

1.[1,2,3,4,5,6,7,8,9,10]隨機重排

考察程序基礎。

實現方式多種多樣,可用隨機數,可用數組內元素本身做隨機值,看各位腦洞了。

//C# Solution
int[] array = { 1,2,3,4,5,6,7,8,9,10};
Random rd = new Random();
for (int i=0;i<array.Length;i++) { 
    int a = rd.Next(0, array.Length);
    array[a] ^= array[i];
    array[i] ^= array[a];
    array[a] ^= array[i];
}
foreach (var i in array) {
    Console.Write(i+" ");
}
Console.ReadLine();

2.判斷一個數是不是2的n次方

這題主要考察位運算,考察進制運算。
https://blog.csdn.net/ap114/article/details/106928807

3.今天是2015年x月x日星期日,13個月x天后是星期幾?什麼時間?

我覺得這題要讓你用程序寫,就考察你時間api用的熟不熟了,要是讓你說思路,得考慮上平年閏年,注意到2月份,大月小月的區別,要讓我手算,我肯定得翻車。還不如出點大衆點的基礎題來的實在。做到一半我突然在想,他要是問你個1天內時針和分鐘重疊多少次該怎麼算,你該怎麼答。

//計算星期幾
Calendar c = Calendar.getInstance();
c.set(2015,2,1);//月份從0開始
Date d = c.getTime();
c.add(Calendar.MONTH ,13 );
c.add(Calendar.DAY_OF_YEAR, 6);
int week = c.get(Calendar.DAY_OF_WEEK) -1;
System.out.println(week);//0=星期天,1=星期1....
//計算秒數
long t1 = c.getTimeInMillis();
long t2 = d.getTime();
System.out.println((t1-t2)/1000);

 

4.寫一個排序算法

function selectionSort(arr) {
    var len = arr.length;
    var minIndex, temp;
    for (var i = 0; i < len - 1; i++) {
        minIndex = i;
        for (var j = i + 1; j < len; j++) {
            if (arr[j] < arr[minIndex]) {     // 尋找最小的數
                minIndex = j;                 // 將最小數的索引保存
            }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
    return arr;
} 

https://www.cnblogs.com/onepixel/articles/7674659.html

5.用OOP思想,兩個人,一個有雞蛋,一個有蘋果,交換着兩個人籃子裏的東西。

寫到是可以隨便亂寫,但要好好設計,真考驗你的功底,這就是這些題看似簡單,實則巧妙(陰險)的地方。

按照網上的寫法,你把交換數據的處理寫在main地方,就不符合oop思想了。雖然看似是在get、set有那個意思,但是味道不對。

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            People A = new People(new Basket("雞蛋"));
            People B = new People(new Basket("蘋果"));
            Console.WriteLine($"A的籃子有:{A}   B的籃子有:{B}");
            A.Exchange(B);
            Console.WriteLine($"A的籃子有:{A}   B的籃子有:{B}");
        }

    }

    //籃子
    public class Basket {
        private object thing;//{get;set;}
        public Basket(){}
        public Basket(object obj){
            this.thing = obj;
        }
        public object GetThing() {
            return this.thing;
        }
        public void SetThing(object thing){
            this.thing = thing;
        }
        public override string ToString(){
            return GetThing().ToString();
        }
    }
    //人
    public class People 
    {
        public Basket basket = new Basket();
        public People(Basket obj) {
            basket = obj;
        }
        public void Exchange(People man){
            //空籃子
            Basket tmp = new Basket();
            //別人的東西先放空籃子
            tmp.SetThing(man.basket.GetThing());
            //我的東西給別人
            man.basket.SetThing(basket.GetThing());
            //我從空籃子拿東西
            this.basket.SetThing(tmp.GetThing());
        }
        public override string ToString() {
            return basket.GetThing().ToString();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章