华为笔试第三题,喝咖啡
用一个优先队列存储咖啡机。。。然后遍历。。。。但是时间来不及了,没交上。不知道思路对不对。
// 本题为考试多行输入输出规范示例,无需提交,不计分。
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct Machine{
int end;
int time;
Machine(int time, int end) : time(time), end(end) {}
bool operator < (Machine m) const {
return (end + time) < (m.end + m.time);
}
bool operator > (Machine m) const {
return (end + time) > (m.end + m.time);
}
};
int main() {
int T, ans;
cin >> T;
for (int i = 0; i < T; i++) {
ans = 0;
priority_queue<Machine, vector<Machine>, greater<Machine >> q1; //保存咖啡机
int m, n, x, y; // x是洗,y是挥发。
scanf("%d %d %d %d", &n, &m, &x, &y);
for (int j = 0; j < m; j++) {
int v;
scanf("%d", &v);
q1.push(Machine(v,0));
}
int clear = 0; //清洗机的时间
for(int i = 0; i < n; i++){
auto machine = q1.top();
q1.pop();
int end = machine.time + machine.end;
q1.push(Machine(machine.time, end)); // 第i个人做咖啡需要的时间。
if (clear == 0 || clear < end){
if ((end + x) < (end + y)){
clear = end + x;
ans = max(end + x, ans);
}else
ans = max(end + y, ans);
}else{
if ((clear + x) < (end + y)){
clear = clear + x;
ans = max(clear , ans);
} else
ans = max(end + y, ans);
}}
cout << ans << endl;
}
return 0;
}
# 第二题
coding=utf-8
本题为考试多行输入输出规范示例,无需提交,不计分。
import sys
def check(s):
for c in s:
if (c <= '9' and c >= '0') or (c <= 'z' and c >= 'a') or (c <= 'Z' and c >= 'A'):
continue
else:
return False
return True
if name == "main":
# 读取第一行的n
ok_str = []
err_str = []
for line in sys.stdin:
line = line.strip()
if len(line):
if check(line):
if line not in ok_str:
ok_str.append(line)
else:
err_str.append(line.strip()) # 获取所有的数据
print(" ".join(ok_str))
print(" ".join(err_str))
data = []
for line in ok_str:
l = len(line)
k = 10 % l # 右移十位
data.append(line[k:] + line[:k])
line = line[-k:] + line[:k]
# data.append(line)
print(" ".join(data))
data = sorted(data)
print(" ".join(data))
# 第一题我忘了题目是啥了。。直接在网页上撸的。本地没代码。。。
```
#华为##笔试题目#