追车问题:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67//追车问题 #include <iostream> #include <vector> #include <algorithm> using namespace std; // Complete the MaximumArriveAtSameTime function below. class Solver{ public: static bool compare(const pair<double, double>& left, const pair<double, double>& right) { return left.first > right.first; } int MaximumArriveAtSameTime(vector<double>& speed, vector<double>& location, double destination) { int n = location.size(); //计算每个车到达终点所需时间 vector<double> times(n, 0); for (int i = 0; i < n; i++) { double time = (destination - location[i]) / speed[i]; times[i] = time; } //根据位置对时间进行排序 vector<pair<double, double>> loc_time(n); for (int i = 0; i < n; i++) { loc_time[i].first = location[i]; loc_time[i].second = times[i]; } sort(loc_time.begin(), loc_time.end(), compare); double maxTime = 0; int maxNum = 0; int rst = 1; for (int i = 0; i < n; i++) { double curTime = loc_time[i].second; if (curTime > maxTime) { maxNum = 1; maxTime = curTime; } else { maxNum++; } rst = max(maxNum, rst); } return rst; } }; // Non-editable part int main() { int speed_count; cin >> speed_count; vector<double> speed(speed_count); for (int i = 0; i < speed_count; i++) { double speed_item; cin >> speed[i]; } int location_count = speed_count; vector<double> location(location_count); for (int i = 0; i < location_count; i++) { double location_item; cin >> location[i]; } double destination; cin>>destination; Solver solver; int res = solver.MaximumArriveAtSameTime(speed, location, destination); std::cout << res << "n"; return 0; }
键盘数字问题:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <iostream> #include <vector> #include <memory> #include <map> #include <cmath> #include <algorithm> #include <limits.h> #include <unordered_map> // using namespace std; #include <bits/stdc++.h> using namespace std; int entryTime(string str, string keypad) { int rst = 0; int n = str.size(); //记录每个坐标 vector<vector<int>> locate(n, vector<int>(2, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < 9; j++) { if (str[i] == keypad[j]) { locate[i][0] = j / 3; locate[i][1] = j - locate[i][0] * 3; } } } //计算每次变化情况 vector<vector<int>> delta(n - 1, vector<int>(2, 0)); for (int i = 0; i < n - 1; i++) { delta[i][0] = locate[i + 1][0] - locate[i][0]; delta[i][1] = locate[i + 1][1] - locate[i][1]; } for (int i = 0; i < n - 1; i++) { int dist = sqrt(delta[i][0] * delta[i][0] + delta[i][1] * delta[i][1]); if (dist == 1) rst += 1; else if (dist == 2) rst += 2; } return rst; }
最后
以上就是舒服草莓最近收集整理的关于Autox笔试笔记(键盘距离&追车问题)的全部内容,更多相关Autox笔试笔记(键盘距离&追车问题)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复