• 百度2017暑期实习生编程题


     1、单词接龙

     拉姆刚开始学习英文单词,对单词排序很感兴趣。
    如果给拉姆一组单词,他能够迅速确定是否可以将这些单词排列在一个列表中,使得该列表中任何单词的首字母与前一单词的为字母相同。
    你能编写一个程序来帮助拉姆进行判断吗?

    输入描述:

    输入包含多组测试数据。

    对于每组测试数据,第一行为一个正整数n,代表有n个单词。

    然后有n个字符串,代表n个单词。

    保证:

    2<=n<=200,每个单词长度大于1且小于等于10,且所有单词都是由小写字母组成。

    输出描述:

    对于每组数据,输出"Yes"或"No"

    输入例子:

    3

    abc

    cdefg

    ghijkl

    4

    abc

    cdef

    fghijk

    xyz

    输出例子:

    Yes

    No

    代码如下:

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
     
    int main()
    {
        int n;
        while (cin >> n)
        {
            bool flag=true;
            vector<string> a(n);
            for (int i = 0; i<n; i++){
                cin >> a[i];
            }
             
            for(int i=0;i<n-1;i++){
                int len=a[i].length();
                string str1=a[i];
                string str2=a[i+1];
                if(str1[len-1] != str2[0]){
                    flag=false;
                    break;
                }
            }
            if(flag) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        return 0;
    }
    

    2、页面调度算法 

    在计算机中,页式虚拟存储器实现的一个难点是设计页面调度(置换)算法。其中一种实现方式是FIFO算法。
    FIFO算法根据页面进入内存的时间先后选择淘汰页面,先进入内存的页面先淘汰,后进入内存的后淘汰。
    假设Cache的大小为2,有5个页面请求,分别为 2 1 2 3 1,则Cache的状态转换为:(2)->(2,1)->(2,1)->(1,3)->(1,3),其中第1,2,4次缺页,总缺页次数为3。
    现在给出Cache的大小n和m个页面请求,请算出缺页数。

    输入描述:

    输入包含多组测试数据。
    对于每组测试数据,第一行两个整数n,m。
    然后有m个整数,代表请求页编号。
    保证:
    2<=n<=20,1<=m<=100,1<=页编号<=200.

    输出描述:

    对于每组数据,输出一个整数,代表缺页数

    输入例子:

    2 5

    2 1 2 3 1

    输出例子:

    3

    代码如下:

    #include <iostream>
    #include <vector>
    using namespace std;
     
    //缺页定义为所有内存块最初都是空的,所以第一次用到的页面都产生一次缺页。这个概念必须先理解
     
    int IsExist(vector<int> &vec,int n){
        int len=vec.size();
        for(int i=0;i<len;i++){
            if(n==vec[i]) return true;
        }
        return false;
    }
     
    int main()
    {
        int n,m;
        while (cin >> n >> m)
        {
            vector<int> Cache;
            vector<int> Page(m);
            int count=0;
            for (int i = 0; i<m; i++){
                cin >> Page[i];
            }
             
            for(int i=0;i<m;i++){
                if(IsExist(Cache,Page[i])) continue;
                else{
                    if(Cache.size()<n){
                        Cache.push_back(Page[i]);
                    }
                    else{
                        Cache.erase(Cache.begin());
                        Cache.push_back(Page[i]);
                    }
                    ++count;
                }
            }
            cout<<count<<endl;  
        }
        return 0;
    }  

    3、进程调度算法

    短作业优先(SJF, Shortest Job First)又称为“短进程优先”SPN(Shortest Process Next);是对FCFS算法的改进,其目标是减少平均周转时间。
    短作业优先调度算法基于这样一种思想:
    运行时间短的优先调度;
    如果运行时间相同则调度最先发起请求的进程。
    等待时间:一个进程从发起请求到开始执行的时间间隔。
    现在有n个进程请求cpu,每个进程用一个二元组表示:(p,q),p代表该进程发起请求的时间,p代表需要占用cpu的时间。
    请计算n个进程的平均等待时间。

    输入描述:

    输入包含多组测试数据。
    对于每组测试数据,第一行为一个整数n。
    然后有n行,每行两个整数,代表上述的二元组(p,q).
    保证:
    2<=n<=2000,1<=p<=300,1<=q<=100.

    输出描述:

    对于每组数据,输出一个浮点数,代表平均等待时间,请保留4位有效数字

    输入例子:

    4

    1 4

    1 3

    1 5

    2 1

    输出例子:

    5.2500

    第一种解法:

    一开始我是这么折腾的:用个二维数组搞定

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <stdio.h>
    //#include <iomanip>
    using namespace std;
    
    bool cmp(vector<int> a, vector<int> b){  
    	if (a[0] == b[0]) return a[1]<b[1];
    	else return a[0]<b[0];
    }
    
    int main()
    {
    	int n;
    	vector<vector<int> > couple;
    	vector<int> node;
    	while (cin >> n)
    	{
    		int res = 0;
    		couple.clear();		
    		for (int i = 0; i<n; i++){
    			int s_time, w_time;
    			cin >> s_time >> w_time;
    			node.push_back(s_time);
    			node.push_back(w_time);
    			couple.push_back(node);
    			node.clear();
    		}
    
    		sort(couple.begin(), couple.end(), cmp);
    		int end = couple[0][0] + couple[0][1];
    		for (int i = 1; i<couple.size(); i++){
    			res += (end>couple[i][0] ? end - couple[i][0] : 0);
    			end = (end>couple[i][0] ? end + couple[i][1] : couple[i][0] + couple[i][1]);
    		}
    		//double result=(double)res;
    		//cout<<setiosflags(ios::fixed)<<setprecision(4)<<result<<endl;
    		printf("%.4f
    ", (double)res / n);
    	}
    	return 0;
    }  

    注: setprecision是控制输出流显示浮点数的有效数字个数,如果和fixed合用的话,可以控制小数点右面的位数!对应的头文件为#include <iomanip>

    第二种方法如下:

    使用结构体要稍微简单点

    #include<iostream>
    #include<vector>
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
     
    struct node{
        int s_time;
        int w_time;
    };
     
    bool cmp(node a, node b){
        if (a.s_time == b.s_time) return a.w_time<b.w_time;
        else return a.s_time<b.s_time;
    }
     
    int main(){
        int n;
        while (cin >> n){
            node tmp;
            vector<node> vec;
            int res = 0;
            for (int i = 0; i<n; i++){
                cin >> tmp.s_time >> tmp.w_time;
                vec.push_back(tmp);
            }
            sort(vec.begin(), vec.end(), cmp);
            int end = vec[0].s_time + vec[0].w_time;
            for (int i = 1; i<vec.size(); i++){
                res += (end>vec[i].s_time ? end - vec[i].s_time : 0);
                end = (end>vec[i].s_time ? end + vec[i].w_time : vec[i].s_time + vec[i].w_time);
            }
            printf("%.4f
    ", (double)res / n);
        }
        return 0;
    }
    

    题目所给示例求解:(3+(3+4)+(3+4+4))/4=5.2500(保留四位有效数字)  

      
      
  • 相关阅读:
    在ant编译java文件时产生debug信息
    Ant里面神奇的fork
    在ant中将依赖jar包一并打包的方法
    Java通过class文件得到所在jar包
    Bat脚本:通过端口号查找进程号
    使用emma时遇到的一些问题
    python之路-day18-反射
    python之路-day17-类与类之间的关系
    python之路-day16-类的成员
    python之路-day15-初识面向对象
  • 原文地址:https://www.cnblogs.com/carsonzhu/p/5544915.html
Copyright © 2020-2023  润新知