-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
128 lines (118 loc) · 3.39 KB
/
main.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
// From https://stackoverflow.com/a/3437484
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
typedef enum invocation_type {
NTIMES,
NHOURS,
INVALID
} invocation_type;
long calculate_lambda(long s) {
long lambda = 0;
if(s <= 0) {
lambda = floor(abs((double)s/2.0)) + 1;
}
return lambda;
}
void train_n_times(long int s, long int n) {
long lambda = calculate_lambda(s);
printf("%ld\n", n + (n > lambda)*(n-lambda)*(n + s + lambda - 2));
}
long calculate_train_n_hours(long int s, long int h) {
long n = -1;
if(s < 1) {
long lambda = calculate_lambda(s);
long h_minus = min(h, lambda);
long h_plus = max(0, h - lambda);
long n_minus = h_minus;
long s_plus = s + 2*n_minus;
long n_plus = 0;
if(h_plus > 0) {
n_plus = calculate_train_n_hours(s_plus, h_plus);
}
n = n_minus + n_plus;
} else if(s == 1) {
n = floor(sqrt((long double)h/s));
} else { // s > 1
n = floor((-1.0 + sqrt(1 + 4.0*h/(powl(s-1.0, 2.0))))*(s-1.0)/2.0);
}
return n;
}
void train_n_hours(long int s, long int h) {
long n = calculate_train_n_hours(s, h);
printf("%ld\n", n);
}
int main(int argc, char** argv) {
if(argc != 4) {
fprintf(stderr, "Usage: rqt <current skill percentage> <number> <times|hours>\n");
exit(1);
}
char* arg_iter;
int minus_found = 0;
if(*argv[1] == '-') {
minus_found = 1;
}
for(arg_iter = argv[1] + minus_found; *arg_iter != '\0'; ++arg_iter) {
if(!isdigit(*arg_iter)) {
fprintf(stderr, "Current skill percentage must be an integer.\n");
exit(1);
}
}
long s = strtol(argv[1], NULL, 10);
if((errno == ERANGE && (s == LONG_MAX || s == LONG_MIN))
|| (errno != 0 && s == 0)) {
printf("Error %d (%s) converting string '%s' to number.\n", errno, strerror(errno), argv[1]);
return 1;
}
for(arg_iter = argv[2]; *arg_iter != '\0'; ++arg_iter) {
if(!isdigit(*arg_iter)) {
fprintf(stderr, "The second argument must be a non-negative integer, either number of times to train, or number of hours.\n");
exit(1);
}
}
long n = strtol(argv[2], NULL, 10);
if((errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))
|| (errno != 0 && n == 0)) {
printf("Error %d (%s) converting string '%s' to number.\n", errno, strerror(errno), argv[2]);
return 1;
}
invocation_type t = INVALID;
if(strncmp(argv[3], "times", 8) == 0) {
t = NTIMES;
} else if(strncmp(argv[3], "hours", 8) == 0) {
t = NHOURS;
}
if(t == INVALID) {
}
switch(t) {
case NTIMES:
{
train_n_times(s, n);
}
break;
case NHOURS:
{
train_n_hours(s, n);
}
break;
case INVALID:
{
fprintf(stderr, "The third argument needs to be either 'times' or 'hours'.\n");
exit(1);
}
break;
}
return 0;
}