虎牙C++B卷全A答案(状态机,最大堆,贪心)
第一题删除C++代码注释:
思路:状态机,词法分析,向前看一个字符,根据题意,不考虑有错误的代码。
第二题服务器负载均衡:
思路:计算权重,用一个最大堆优化,设计仿函数。
第三题求字符串里所有整数的和:
思路:贪心匹配。
第一题
#include <iostream>
using namespace std;
enum State
{
NORMAL,
IN_SINGLE_COMMENT,
IN_MULTI_COMMENT,
IN_STRING,
IN_CHAR
};
int main()
{
State state = NORMAL;
int c;
while ((c = getchar()) != EOF)
{
if (c == '\n')
{
if (state != IN_MULTI_COMMENT)
{
state = NORMAL;
cout << '\n';
}
}
else
{
if (state == NORMAL)
{
if (c == '"')
{
state = IN_STRING;
cout << '"';
}
else if (c == '\'')
{
state = IN_CHAR;
cout << '\'';
}
else if (c == '/')
{
c = getchar();
if (c == '/')
{
state = IN_SINGLE_COMMENT;
}
else if (c == '*')
{
state = IN_MULTI_COMMENT;
}
else
{
cout << '/' << (char)c;
}
}
else
{
cout << (char)c;
}
}
else if (state == IN_SINGLE_COMMENT)
{
// do nothing.
}
else if (state == IN_MULTI_COMMENT)
{
if (c == '*')
{
c = getchar();
if (c == '/')
{
state = NORMAL;
}
}
}
else if (state == IN_STRING)
{
if (c == '\\')
{
cout << '\\' << (char)getchar();
}
else if (c == '"')
{
cout << '"';
state = NORMAL;
}
else
{
cout << (char)c;
}
}
else if (state == IN_CHAR)
{
if (c == '\\')
{
cout << '\\' << (char)getchar();
}
else if (c == '\'')
{
cout << '\'';
state = NORMAL;
}
else
{
cout << (char)c;
}
}
}
}
return 0;
}
第二题。
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <limits>
#include <utility>
#include <algorithm>
struct ServerSelector
{
// 下游服务器编号(从0起)
typedef int server_index_t;
// 权重
typedef size_t weight_t;
struct MyLess
{
bool operator()(const std::pair<server_index_t, double> &a, const std::pair<server_index_t, double> &b)
{
return a.second < b.second;
}
};
std::vector<weight_t> servers;
std::vector<int> counts;
std::vector<std::pair<server_index_t, double>> scores;
// 下游服务器列表或权重变更或初始化时被调用
// @servers:下游服务器权重列表 (第一个元素代表编号为0的服务器权重, 第二个元素代表编号为1的服务器权重, 以此类推)
void updateServerList(std::vector<weight_t> &servers)
{
this->servers = servers;
counts = std::vector<int>(servers.size());
scores = std::vector<std::pair<server_index_t, double>>(servers.size());
for (size_t i = 0; i < servers.size(); i++)
{
scores[i].first = i;
scores[i].second = std::numeric_limits<double>::max();
}
std::make_heap(scores.begin(), scores.end(), MyLess());
}
// 每次发送请求前调用这个函数获取应该指向的下游服务器编号
server_index_t selectServer()
{
server_index_t index = scores.front().first;
counts[index]++;
scores.front().second = 1.0 * servers[index] / counts[index];
std::pop_heap(scores.begin(), scores.end(), MyLess());
std::push_heap(scores.begin(), scores.end(), MyLess());
return index;
}
};
int main()
{
// 处理输入
int numServer = 0;
std::cin >> numServer;
std::vector<ServerSelector::weight_t> servers(numServer);
for (int i = 0; i < numServer; ++i)
{
std::cin >> servers[i];
}
int numRequests = 0;
std::cin >> numRequests;
// 模拟服务器间调用
std::vector<int> requests(numServer);
ServerSelector ss;
ss.updateServerList(servers);
for (int i = 0; i < numRequests; ++i)
{
ServerSelector::server_index_t serverIndex = ss.selectServer();
if (serverIndex >= numServer)
return 1;
requests[serverIndex]++;
}
// 输出结果
for (int v : requests)
{
std::cout << v << std::endl;
}
return 0;
}
第三题。
#include <iostream>
#include <sstream>
#include <cctype>
using namespace std;
int main()
{
string str;
while (cin >> str)
{
long long sum = 0;
size_t strLength = str.length();
for (size_t i = 0; i < strLength; i++)
{
char c = str[i];
if (!(c == '-' || c >= '0' && c <= '9'))
{
continue;
}
if (c != '-')
{
size_t j;
for (j = 1; i + j < strLength && isdigit(str[i + j]); j++)
;
stringstream ss;
ss << str.substr(i, j) << endl;
long long tmp;
ss >> tmp;
sum += tmp;
i += j - 1;
}
else
{
size_t j;
for (j = 1; i + j < strLength && isdigit(str[i + j]); j++)
;
if (j == 1)
{
continue;
}
stringstream ss;
ss << str.substr(i, j) << endl;
long long tmp;
ss >> tmp;
sum += tmp;
i += j - 1;
}
}
cout << sum << endl;
}
return 0;
}
#笔试题目##虎牙直播#