码迷,mamicode.com
首页 > 其他好文 > 详细

Google笔试(2015/8/6)

时间:2015-08-16 16:41:36      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

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

Problem 1:

Problem 2:

Problem 3:
Moist has a hobby -- collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs to sort the cards in alphabetical order, so that he can find the cards that he wants on short notice whenever it is necessary.
The problem is -- Moist can’t actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.
Moist has figured out that the robot’s sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card, it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.
As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to sort his deck of cards.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each one starts with a line containing a single integer, N. The next N lines each contain the name of a figure skater, in order from the top of the deck to the bottom.
Output
For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the number of dollars it would cost Moist to use the robot to sort his deck of trading cards.
Limits
1 ≤ T ≤ 100.
Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.
Lexicographically, the space character comes first, then come the upper case letters, then the lower case letters.
Small dataset
1 ≤ N ≤ 10.
Large dataset
1 ≤ N ≤ 100.
Sample
Input
2
2
Oksana Baiul
Michelle Kwan
3
Elvis Stojko
Evgeni Plushenko
Kristi Yamaguchi
Output
Case #1: 1
Case #2: 0

#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.clear();
        string str;
        vector<string> v1;
        while (getline(cin, str)) {
            count++;
            v1.push_back(str);
            if (count > n)
                break;
        }
        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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Google笔试(2015/8/6)

标签:

原文地址:http://blog.csdn.net/zhangzhengyi03539/article/details/47703191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!