跳到主要內容

Multiple without multication or addition operator

Given two binary strings, return their sum (also a binary string).
For example,
a = "1101" // 13
b = "1000" // 8
Return “1101000”. // 104
Solution: implement Binary add to perform multication

static int MultipleWithoutOperator(int num, int multiplier)
{
    StringBuilder sb = new StringBuilder();
    string binary = Convert.ToString(num, 2);
    string binaryMultiplier = Convert.ToString(multiplier, 2);

    string temp = "";
    for (int i = binaryMultiplier.Length-1, j=0;i >= 0;i--,j++){
        
        if (binaryMultiplier[i]=='1')
        {
            if (string.IsNullOrEmpty(temp))
            {
                temp = binary.PadRight(binary.Length + j, '0');
            } else
            {
                temp = BinaryAdd(temp, binary.PadRight(binary.Length + j, '0'));
            }
        }
    }
    return Convert.ToInt32(temp, 2);
}

static string BinaryAdd(string a, string b)
{
    int carryover = 0;
    StringBuilder stringBuilder = new StringBuilder();
    for (int startA = a.Length - 1, startB = b.Length - 1; startA >= 0 || startB >= 0; startA--, startB--)
    {
        int curA = startA >= 0 ? a[startA] - '0' : 0;
        int curB = startB >= 0 ? b[startB] - '0' : 0;
        int sum = curA + curB + carryover;

        if (sum > 1)
        {
            stringBuilder.Insert(0, sum % 2);
            carryover = 1;
        }
        else
        {
            stringBuilder.Insert(0, sum);
            carryover = 0;
        }
    }
    if (carryover == 1)
    {
        stringBuilder.Insert(0, '1');
    }
    return stringBuilder.ToString();
}

留言

這個網誌中的熱門文章

加密貨幣交易實戰心得筆記

加密貨幣交易實戰心得筆記 . 對於加密貨幣,雖然公司有個團隊已經在開發相關產品,但我自己只有大概了解什麼是加密貨幣,什麼是比特幣,以及什麼是區塊鍊。雖然稍微知道,但也只是知道,並沒有真的去購買跟投資。 . 幾天前公司舉辦了一場課程,邀請了史旺基到公司分享加密貨幣的交易實戰。課程需要自費,但其中包含了直接幫我們購買比特幣,並將購買的比特幣交易給我們。可以實際接觸到加密貨幣的交易讓我很期待,因此毫不猶豫的就參加了這堂課程。 . 這堂課程並不是在技術上來跟你說什麼是加密貨幣或是區塊鍊的技術是什麼,它著重在由淺入深的介紹什麼是比特幣、什麼是區塊鍊、什麼是去中心化、什麼是挖礦、誰是中本聰、什麼是以太坊以及以太幣、什麼是熱錢包以及冷錢包,等等這類的相關資訊。 . 學習並了解上面介紹的基本資訊之後,課程的重點放在了交易所的介紹與操作上面。我們一步一步的申請了台灣的兩個交易所,分別是 MaiCoin 以及 BitoEX。相信有在投資比特幣的人應該都已經知道,但對於沒有追上潮流的我來說,這個資訊真的是省了我很多研究的時間,立刻感覺前進了一大步,雖然手續費高常常可能卡幣,但中文介面,簡單的認證與交易方式,讓我感覺已經真的觸碰到了比特幣與區塊鍊的邊。 . 除了台灣交易所,更有國際交易平台的介紹與教學,這邊我就不一一介紹了,因為我自己也都還沒申請,而且這些國際平台很多是停止申請帳號的。 . 另外還有更多的資訊,包括怎麼開始投資,投資心法以及不少的中英文介紹資料,包括App的介紹。 . 這堂課除了讓我立刻從門外漢大躍進成初心者,更是直接開始打怪(購買)了,破除了這個最大靜摩擦力之後,我讓我的投資標的又多了一個可能,感覺非常值得。

[TDD之路] 與資源檔案相依時導致 CI 測試失敗

CI 上跑測試專案的指令如下 "MSTest.exe /testcontainer:.\bin\Debug\TargetTests.dll /resultsfile:TestResults.trx" 在本機測試都 all pass, 但是在 CI 上面卻失敗。主要原因是測試的物件中有一個跟Bitmap有關的 Image 找不到圖檔導致錯誤。 程式碼如下: public static string ImagePath = Directory.GetCurrentDirectory() + "\\img\\"; public static Image ClockRed = new Bitmap(ImagePath + "Clockred.png"); 看起來ImagePath就是取得當前位置,然後從當前位置的 img 資料夾中找出 Clockred.png 來產生 Bipmap 物件存到 Image裡面。 local 建置與測試一切正常。但執行了 CI 就吐錯誤。 為什麼呢? 主要原因是雖然已經有將 img\Clockred.png copy 到 output 資料夾(bin\debug),但是實際上MSTest.exe執行的目錄卻是在 .\bin\Debug\TestResults\kyo_TI02BT43 2018-03-07 23_46_45\Out 的資料夾。且裡面是沒有 img\Clockred.png 的。 為了解決這個問題,我嘗試了很多取得當前目錄的方法。但問題不在取得目錄,其實目錄是對的,錯的是檔案並不存在。因次找到了一個 deploy 資源檔案到 deploy out 目錄的方法。 就是 DeploymentItem 這個 attribute, 用法可以寫在 [TestClass] 下方 或是 [TestMethod] 下方。 [TestClass] [DeploymentItem(@"img\Clockgraypanel.png", "img")] 就這樣我的測試就可以在 CI 伺服器上跑過了。