1. 程式人生 > >Google筆試(2015年8月)

Google筆試(2015年8月)

染色 data- get nes 4.0 字符 itl number deb

華電北風吹
天津大學認知計算與應用重點實驗室
日期: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 = ”
S1 = ‘0’
S2 = ‘001’
S3 = ‘0010011’
S4 = ‘001001100011011’
……
SN = SN + ‘0’ + not(reverse( SN ))
reverse是字符串反轉
not是0,1轉換
S1010000 的第k個數
small data set: k<105
large data set: k<1018

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)

Google筆試(2015年8月)