百度2017暑期实习生编程题

1 单词接龙

/* 单词接龙 */
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int MAX_LIST_LENGTH = 200;

bool isRight(vector<string>& vec, int length)
{
    if(vec.size() < 2)
        return false;
    string tmpStr = vec[0];
    char tmpChar = tmpStr[tmpStr.length() - 1];

    string curStr;
    char curChar;
    for(int i = 0; i < length - 1; ++i)
    {
        curStr = vec[i];
        curChar = curStr[0];

        if(curChar != tmpChar)
            return false;

        tmpChar = tmpStr[tmpStr.length() - 1];
    }
    return true;
}

int main()
{
    string cur_word;
    int words_num;
    vector<string> words_list;
    while(cin >> words_num)
    {
        for(int i = 0; i < words_num; ++i)
        {
            cin >> cur_word;
            words_list.push_back(cur_word);
        }
        if(isRight(words_list, words_list.size()))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;

    }
}

2 页面调度算法

/* 页面调度算法 计算FIFO发生的总缺页数 */
#include <iostream>
#include <vector>
using namespace std;

bool not_find(vector<int>& vec, int value)
{
    int vec_size = vec.size();
    for(int i = 0; i < vec_size; ++i)
    {
        if(value == vec[i])
            return false;
    }
    return true;
}

int main()
{
    int n, m;
    while(cin >> n >> m)
    {
        vector<int> cur_list; // 当前已调入的页面列表
        vector<int> page_list(m); // 所有的页面请求列表
        for(int i = 0; i < m; ++i)
        {
            cin >> page_list[i];
        }

        int times = 1; // 缺页次数,一开始肯定缺页
        cur_list.push_back(page_list[0]);


        for(int i = 1; i < m; ++i)
        {
            if(not_find(cur_list, page_list[i])) // 缺页时
            {
                ++times;
                if(cur_list.size() == n)
                    cur_list.erase(cur_list.begin());
                cur_list.push_back(page_list[i]);

            }
        }
        cout << times << endl;
    }
}

3 短作业优先

/* 短作业优先
    两个优先级:先到先处理,同时到处理短作业
 */
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;



int main()
{
    int n;
    while(cin >> n)
    {
        vector<int> rts; // reach time
        vector<int> cts; // cost time
        for(int i = 0; i < n; ++i)
        {
            int r, t;
            cin >> r >> t;
            rts.push_back(r);
            cts.push_back(t);
        }

        int cur_time = 1;
        double wait_time = 0;

        while(!rts.empty())
        {
            // 确定要执行进程
            int cur_process = 0;
            for(int i = 0; i < rts.size(); ++i)
            {
                if(rts[i] < rts[cur_process] || (rts[i] == rts[cur_process] && cts[i] < cts[cur_process])) // 对应两个优先级
                    cur_process = i;

            }
            if(cur_time > rts[cur_process]) // 当前作业已等待,等待的时间是当前系统时间-当前作业到达时间
            {
                wait_time += cur_time - rts[cur_process];
                cur_time += cts[cur_process];
            }
            else
            {
                cur_time = rts[cur_process] + cts[cur_process]; // 当前作业未等待,处理完成后的当前系统时间
            }
            rts.erase(rts.begin() + cur_process);
            cts.erase(cts.begin() + cur_process);
        }
        printf("%.4f\n", wait_time / n);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值