ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo004.c
Go to the documentation of this file.
1
33
34
#include <stdio.h>
35
42
int
main
(
int
argc,
char
*argv[])
43
{
44
(void)argc;
45
(void)argv;
46
47
int
N
, R, K, Rmn, Rmx;
48
49
scanf(
"%d %d %d"
, &
N
, &R, &K);
50
51
/*
52
Mínimo:
53
Se logra maximizando x (tantos rojos como sea posible se van a azul).
54
x_max = min(K, R)
55
Rmn = R + K - 2*x_max
56
Esto equivale a |R - K|.
57
El código implementa |R-K| sin if usando el signo:
58
(2*(R>=K)-1) es +1 si R>=K, -1 si R<K.
59
*/
60
Rmn = (2*(R >= K) - 1) * (R - K);
61
62
/*
63
Máximo:
64
Se logra minimizando x (que se vayan lo menos posible del rojo).
65
x_min = max(0, K - (N - R))
66
Si K <= (N - R): Rmx = R + K
67
Si K > (N - R): Rmx = 2N - (R + K)
68
El código evita if usando booleanos.
69
*/
70
Rmx = ((
N
- R) < K) * 2 *
N
71
+ (2 * ((
N
- R) >= K) - 1) * (R + K);
72
73
printf(
"%d\t%d\n"
, Rmn, Rmx);
74
return
0;
75
}
N
#define N
Definition
012_aleatorio.c:5
main
int main(void)
Definition
Ejemplo_035.c:15
src
20261
src
Ejemplo004.c
Generated by
1.16.1