首页 > 代码库 > Google笔试(2015年8月)

Google笔试(2015年8月)

华电北风吹
天津大学认知计算与应用重点实验室
日期:2015/8/21

这三道题目的PDF能够在这里下载
https://github.com/ncepuzhengyi/jobHuntingExam/tree/master/jobExam/Problem_20150815_google

Problem 1:
问题1是眼下须要将阻止分成两块,因为组织内有些人之间有矛盾不能分到同一组内。问你是否存在这种划分。


问题一是二分图推断问题,仅仅须要推断无向图是否是二分图就可以。

最简单的方法是採用广度优先搜索+染色法就可以。

#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <deque>
using namespace std;

#define size 5
int graph[size][size];
int visited[size];
int color[size];

bool  GraphJudge(int nodeNum)
{
    memset(visited, 0, sizeof(visited));
    memset(color, 0, sizeof(color));
    for (int k = 0; k < nodeNum; k++)
    {
        if (visited[k] == 0)
        {
            visited[k] = 1;
            color[k] = 1;
            deque<int> q;
            q.push_back(k);
            while (q.empty()==false)
            {
                int i = q.front();
                q.pop_front();
                for (int j = 0; j < nodeNum; j++)
                {
                    if (graph[i][j] > 0 && i!=j)
                    {
                        if (visited[j] == 0)
                        {
                            q.push_back(j);
                            visited[j] = 1;
                            color[j] = 1 - color[i];
                        }
                        else
                        {
                            if (color[j] == color[i])
                                return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}

int main(int argc, char* argv[])
{
    ifstream in(".\\input.txt");
    cin.rdbuf(in.rdbuf());

    int T;
    cin >> T;
    cin.ignore();
    for (int caseNum = 0; caseNum<T; caseNum++)
    {
        int M;
        cin >> M;
        memset(graph, 0, sizeof(&graph));
        map<string, int> nameIndex;
        map<string, int>::iterator it;
        int nodecount = 0;
        for (int i = 0; i < M; i++)
        {
            int start, end;
            string p;
            cin >> p;
            it = nameIndex.find(p);
            if (it == nameIndex.end())
            {
                nameIndex[p] = nodecount;
                nodecount++;
            }
            start = nameIndex[p];
            cin >> p;
            it = nameIndex.find(p);
            if (it == nameIndex.end())
            {
                nameIndex[p] = nodecount;
                nodecount++;
            }
            end = nameIndex[p];
            graph[start][end] = 1;
        }
        if (GraphJudge(nodecount))
            cout << "Case #" << caseNum + 1 << ":" << "Yes" << endl;
        else
            cout << "Case #" << caseNum + 1 << ":" << "No" << endl;
    }
    system("pause");
    return 0;
}

Problem 2:
问题二确切的说应该算作一个高中物理题,给出斜抛初速度和距离,计算斜抛初始角度。

#include<iostream>
#include <math.h>
#include <iomanip>
#include <ostream> 
#include <fstream> 
using namespace std;

int main(int argc, char* argv[])
{
    //ifstream in("C:\\Users\\zhengyi\\Desktop\\ConsoleApplication1\\Debug\\input.txt");
    //streambuf *cinbuf = cin.rdbuf(); //save old buf
    //cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!

    //ofstream out("C:\\Users\\zhengyi\\Desktop\\ConsoleApplication1\\Debug\\out.txt");
    //streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    //cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!

    //std::cin.rdbuf(cinbuf);   //reset to standard input again
    //std::cout.rdbuf(coutbuf); //reset to standard output again

    int T;
    cin >> T;
    int k = 0;
    while (k < T)
    {
        int V, D;
        cin >> V >> D;
        double theta = asin(9.8 * D / (V * V)) * 90 / 3.14159265;
        cout << "Case #" << k + 1 <<":"<< fixed << setprecision(7) << theta << endl;
        k++;
    }
    return 0;
}

Problem 3:
第三题操作过程是一种相似于插入排序的排序机制,对于接下来的元素。须要往前插入的话就耗费1$。以此计算总共花费。

#include <iostream>
#include <string>
#include <vector>
#include <ostream> 
#include <fstream> 
using namespace std;

int main(int argc, char* argv[])
{
    //ifstream in("C:\\Users\\zhengyi\\Desktop\\ConsoleApplication1\\Debug\\input.txt");
    //streambuf *cinbuf = cin.rdbuf(); //save old buf
    //cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!

    //ofstream out("C:\\Users\\zhengyi\\Desktop\\ConsoleApplication1\\Debug\\out.txt");
    //streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    //cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!

    //std::cin.rdbuf(cinbuf);   //reset to standard input again
    //std::cout.rdbuf(coutbuf); //reset to standard output again

    int N;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        int n;
        cin >> n;
        int count = 0;
        cin.sync();
        string str;
        vector<string> v1;
        while (count<n) 
        {
            getline(cin, str);
            v1.push_back(str);
            count++;
        }
        //v1.erase(v1.begin());
        int cost = 0;
        for (int k = 1; k < n; k++)
        {
            string key = v1[k];
            int t = k - 1;
            if (t >= 0 && v1[t].compare(key) > 0)
            {
                v1[t + 1] = v1[t];
                t--;
                cost++;
            }
            v1[t + 1] = key;
        }
        cout << "Case #" << i + 1 << ":" << cost << endl;
    }
    return 0;
}

三道题目的代码
https://github.com/ncepuzhengyi/jobHuntingExam

以下这个是另外一次google的笔试题目。仅仅收集到了这一道
Problem 4
S0<script type="math/tex" id="MathJax-Element-1">S_0</script> = ”
S1<script type="math/tex" id="MathJax-Element-2">S_1</script> = ‘0’
S2<script type="math/tex" id="MathJax-Element-3">S_2</script> = ‘001’
S3<script type="math/tex" id="MathJax-Element-4">S_3</script> = ‘0010011’
S4<script type="math/tex" id="MathJax-Element-5">S_4</script> = ‘001001100011011’
……
SN<script type="math/tex" id="MathJax-Element-6">S_N</script> = SN<script type="math/tex" id="MathJax-Element-7">S_N</script> + ‘0’ + not(reverse( SN<script type="math/tex" id="MathJax-Element-8">S_N</script> ))
reverse是字符串反转
not是0,1转换
S1010000<script type="math/tex" id="MathJax-Element-9">S_{10^{10000}}</script> 的第k个数
small data set: k<105<script type="math/tex" id="MathJax-Element-10">10^5</script>
large data set: k<1018<script type="math/tex" id="MathJax-Element-11">10^{18}</script>

from math import log
from math import ceil

def Nlen(n):
    return pow(2,n)-1

def LineNum(k):
    return ceil(log(k+1,2))

r=True
def func(x):
    global r
    lastNum=LineNum(x)
    if x==pow(2,lastNum-1):
        if r:
            return ‘0‘
        else:
            return ‘1‘
    if x==1:
        if r:
            return ‘1‘
        else:
            return ‘0‘
    if r:
        r=False
    else:
        r=True
    return func(pow(2,lastNum)-x)

s4=‘‘
for i in range(1,16):
    r=True
    s4+=func(i)
print(s4)
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

Google笔试(2015年8月)