华为笔试第三题,喝咖啡

用一个优先队列存储咖啡机。。。然后遍历。。。。但是时间来不及了,没交上。不知道思路对不对。

// 本题为考试多行输入输出规范示例,无需提交,不计分。

#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))
# 第一题我忘了题目是啥了。。直接在网页上撸的。本地没代码。。。

```

#华为##笔试题目#
全部评论
第三题为什么新加入队列的杯子不考虑杯子清洗完的时间呢?
点赞 回复 分享
发布于 2019-04-16 17:15
题目是啥
点赞 回复 分享
发布于 2019-04-03 21:29
大佬你前两题输入拿什么写的?求教啊
点赞 回复 分享
发布于 2019-04-03 21:28
# coding=utf-8 # 本题为考试多行输入输出规范示例,无需提交,不计分。 import sys # def check(s): # tmp = [] # for x in range(ord('0'), ord('9') + 1): # tmp.append(x) # for x in range(ord('a'), ord('z') + 1): # tmp.append(x) # for x in range(ord('a'), ord('Z') + 1): # tmp.append(x) # print(tmp) # for c in s: # c = ord(c) # print(c) # if c in tmp: # continue # else: # return False # return True 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))
点赞 回复 分享
发布于 2019-04-03 21:28
以前清华夏令营做过,那时候就不会,也懒得看了😂
点赞 回复 分享
发布于 2019-04-03 21:27
前2题分享下?
点赞 回复 分享
发布于 2019-04-03 21:27

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
2
9
分享

创作者周榜

更多
牛客网
牛客企业服务