This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab09.cpp
More file actions
94 lines (80 loc) · 1.67 KB
/
lab09.cpp
File metadata and controls
94 lines (80 loc) · 1.67 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "lab.h"
// c 语言版
//#include <stdio.h>
//#include <stdlib.h>
//
//int max(int a, int b) {
// return a > b ? a : b;
//}
//
//int min(int a, int b) {
// return a < b ? a : b;
//}
//
//int calc_max_capacity(int h[], int len) {
// if (len <= 1) return 0;
//
// int l = 0, r = len - 1, c = 0;
//
// while (l < r) {
// c = max(c, (r - l) * min(h[l], h[r]));
//
// if (h[l] < h[r]) l++;
// else r--;
// }
//
// return c;
//}
//
//void lab09() {
// int temp, len = 0, cap, c = 100;
// int* heights = (int*)malloc(sizeof(int) * 100);
//
// while (scanf("%d", &temp) == 1) {
// heights[len++] = temp;
//
// if (len >= c) {
// heights = (int*)realloc(heights, sizeof(int) * (c *= 2));
// }
//
// if (getchar() == '\n') {
// break;
// }
// }
//
// cap = calc_max_capacity(heights, len);
//
// printf("%d", cap);
//
// free(heights);
//}
int calc_max_capacity(std::vector<int>& h) {
int len = h.size();
if (len <= 1) return 0;
int l = 0, r = len - 1, c = 0;
while (l < r) {
c = std::max(c, (r - l) * std::min(h[l], h[r]));
if (h[l] < h[r]) l++;
else r--;
}
return c;
}
void lab09() {
std::vector<int> heights;
int temp;
// oj 不能这样写, 不知为何...
//while (true) {
// std::cin >> temp;
// heights.push_back(temp);
// if (getchar() == '\n') {
// break;
// }
//}
while (scanf_s("%d", &temp) == 1) {
heights.push_back(temp);
if (getchar() == '\n') {
break;
}
}
std::cout << calc_max_capacity(heights);
}