-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogtest5.c
130 lines (108 loc) · 3 KB
/
progtest5.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
129
130
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#define BLOCK_DARR_SIZE 1000
int inputCheck(double a) {
if (fabs(a) < DBL_EPSILON) {
return 2; //is zero
}
double b = round(a);
if (fabs(a - b) < DBL_EPSILON) {
return 0; //ok
} else {
return 1; //is not an int num
}
}
int gcd(unsigned long long a, unsigned long long b) //Greatest common divisor
{
while (b) b ^= a ^= b ^= a %= b;
return a;
}
int lcm(unsigned long long a, unsigned long long b) //Least common multiple
{
return (a / gcd(a, b)) * b;
}
int main() {
char c1 = 0;
double input = 0;
int * road_map = 0;
int odd_run = 1;
int input_counter = 0;
double from_in = 0;
double to_in = 0;
int from = 0;
int to = 0;
unsigned long long result = 1;
unsigned long long road_map_ll;
int input_test;
printf("Pocty pruhu:\n");
if (scanf(" %c", & c1) != 1 || c1 != 123) {
printf("Nespravny vstup.\n");
return 1;
}
road_map = (int * ) malloc(sizeof(int) * BLOCK_DARR_SIZE);
while (1) {
if (odd_run) //an int num is expected
{
if (scanf("%lf", & input) != 1 || inputCheck(input) || input <= 0) {
printf("Nespravny vstup.\n");
free(road_map);
return 1;
}
road_map[input_counter] = round(input);
if (input_counter % BLOCK_DARR_SIZE == (BLOCK_DARR_SIZE - 1)) {
road_map = (int * ) realloc(road_map, sizeof(int) * (input_counter + BLOCK_DARR_SIZE));
}
input_counter++;
odd_run = 0;
} else //even run (a char is expected)
{
if (scanf(" %c", & c1) != 1) {
printf("Nespravny vstup.\n");
free(road_map);
return 1;
}
if (c1 == 44) {
odd_run = 1;
continue;
}
if (c1 == 125) {
odd_run = 1;
break;
}
if (c1 != 125 && c1 != 44) {
printf("Nespravny vstup.\n");
free(road_map);
return 1;
}
}
}
printf("Trasy:\n");
while (1) {
input_test = scanf("%lf %lf", & from_in, & to_in);
if (input_test != 2 || inputCheck(from_in) == 1 || inputCheck(to_in) == 1) {
if (input_test == EOF) {
break;
}
printf("Nespravny vstup.\n");
free(road_map);
return 1;
}
from = from_in;
to = to_in;
if (from >= to || from < 0 || to > input_counter) {
printf("Nespravny vstup.\n");
free(road_map);
return 1;
}
for (int j = from; j < to; j++) {
road_map_ll = road_map[j];
result = lcm(result, road_map_ll);
}
printf("Vozidel: %llu\n", result);
result = 1;
}
free(road_map);
return 0;
}