mirror of
https://github.com/nolongeractivemp3/shipthatcode-c.git
synced 2026-11-24 10:45:12 +01:00
lesson 8 clamp it
This commit is contained in:
1 parent
6d96a16d20
commit
70d8d3dc17
1 file changed
+21
-12
@@ -1,16 +1,25 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* Return value limited to the range lo..hi (lo <= hi is guaranteed). */
|
||||||
|
int clamp(int value, int lo, int hi) {
|
||||||
|
/* TODO: return lo, hi, or value itself, whichever applies. */
|
||||||
|
int result = value;
|
||||||
|
if (result > hi) {
|
||||||
|
result = hi;
|
||||||
|
} else if (result < lo) {
|
||||||
|
result = lo;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int n;
|
int lo, hi, n;
|
||||||
scanf("%d", &n);
|
scanf("%d %d", &lo, &hi);
|
||||||
long long result = 0;
|
scanf("%d", &n);
|
||||||
long imat = 1;
|
for (int i = 0; i < n; i++) {
|
||||||
while (!(imat>n)) {
|
int x;
|
||||||
result = result +imat;
|
scanf("%d", &x);
|
||||||
imat = imat+2;
|
printf("%d\n", clamp(x, lo, hi));
|
||||||
|
}
|
||||||
}
|
return 0;
|
||||||
|
|
||||||
printf("%lld", result);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user