optiver笔试

发布于:2023-03-12 ⋅ 阅读:(95) ⋅ 点赞:(0)

笔试基础题

  1. df
  2. sed
  3. awk
  4. top
  5. netstat
  6. bash基本操作,seq
  7. head
  8. 打印文件前10行
  9. 查看网络路由信息(route)
  10. 查看load average信息(w或者top)
  11. 权限分配、私钥权限
  12. 路由器和交换机的区别
  13. swap分区如何挂载
  14. grep命令
  15. 挂载

算法1

  1. 读入文件 host_access_log_00.txt, 将输出结果存在 bytes_host_access_log_00.txt 中返回。读入文件每一行包括
hostname   timestamp    Request   HTTP Response code  Bytes
a.b.x  [01/Jul/1995:00:00:06 -0400] "GET /shuttle/countdown/ HTTP/1.0" 200 3985
  • 然后输出所有bytes > 5000 的文件行数,并输出文件行数相加的结果

思路&难点

  • 文件输入输出流的处理
  • 文件名称的定义
  • for循环找到bytes的值,用stol转换
int main()
{
    // read the string filename
     freopen("data", "r", stdin);
     string filename;
     cin >> filename;
     int cnt = 0;
     long long bytesCnt = 0;
     std::ifstream fin(filename, std::ios::in); //读入整个文件
     char line[1024] = {0};
     string num = "";
     while (fin.getline(line, sizeof(line))) {
         std::stringstream word(line);
         for (int i = 0; i < 10; ++i) {
             word >> num;
         }
         if (stol(num) > 5000) {
            cnt++;
            bytesCnt += stol(num);
         }
         num.clear();
     }
     string OutPutPath = "bytes_" + filename;
     ofstream outfile;
     outfile.open(OutPutPath, ios::out | ios::app);
     if (!outfile) {
            return 0;
     }
     outfile << cnt << "n" << bytesCnt << "n";
     outfile.close();
     cout << cnt << endl<< bytesCnt << endl;
     return 0;
}

算法2

同上,输入host_access_log_00.txt结尾的文件,输出存在records_host_access_log_00.txt 的输出文件中. 记录每一个host总共发起的请求数目。

思路&解法

  • 同1, 注意用map记录出现次数即可
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
using namespace std;
int main() {
    freopen("data", "r", stdin);
     string filename;
     cin >> filename;
     ifstream fin(filename, ios::in);
     char bytes[1024];
     string host = "";
     map<string, int> m;
     while (fin.getline(bytes, sizeof(bytes))) {
         stringstream word(bytes);
         word >> host; //host名称
         m[host]++;
         host.clear(); //注意对它清空
     }
     string path = "records_" + filename;
     ofstream outfile(path, ios::out || ios::app);
     for (auto x : m) {
            outfile << x.first << "t" << x.second << "n";
     }
     outfile.close();
     return 0;
}
本文含有隐藏内容,请 开通VIP 后查看