2015-09-09 [一点资讯]--数据抓取和处理工程师--3面

时间:2015-09-09 13:40 ~ 14:40

地点:北京市海淀区王庄路1号 清华同方科技广场D座 西区 7层

 

1. A 和 B 有多少bit不一样

#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// Return the number of difference bits of a and b
// Return 1 if a = 0 and b = 1
// Return 2 if a = 0 and b = 3
int diff(int a, int b)
{
    int ans = 0;

    a = a ^ b;

    int i = 32;
    int j = 1;

    while (i--) {

        if (a & j)
            ans++;

        j <<= 1;
    }

    return ans;
}

int main()
{
    const struct TestCase {
        int a;
        int b;
        int ret;
    } test_cases[] = {
        { 0, 1, 1 },
        { 0, 2, 1 },
        { 0, 3, 2 },
        { 1, 3, 1 },
        { -1, 0, 32 },
        { -1, 3, 30 },
        { -1, 0x7FFFFFFF, 1 },
    };

    for (int iii = 0; iii < sizeof(test_cases) / sizeof(TestCase); iii++) {
        const TestCase &tc = test_cases[iii];

        const int actual_ret = diff(tc.a, tc.b);

        if (tc.ret != actual_ret) {
            cout << "Case #" << iii << ": FAILED" <<  endl;
            cout << "\tExpected ret=" << tc.ret << endl;
            cout << "\tAcutal   ret=" << actual_ret << endl;
        }
    }

    return 0;
}

 

2. grep 查找第一行

grep "what_you_want" target_file | head -n 1

 

3. 在100GB的文件中查找某一天的第一条日志

二分查找的实际应用,需要注意健壮性。

posted @ 2015-09-12 20:33 loverszhaokai 阅读(...) 评论(...) 编辑 收藏