-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmario's training camp.cpp
272 lines (271 loc) · 9.37 KB
/
mario's training camp.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// main.cpp
// Mario's Training camp
//
// Created by Daniel Shterev on 2.12.19.
// Copyright © 2019 Daniel Shterev. All rights reserved.
//
#include <iostream>
#include <cstdlib>
#include <time.h>
using std::cin ;
using std::cout ;
using std::endl ;
const int Max_Array_Size = 10000 ;
bool primeCheck (int Number ) //function for checking whether or not a number is prime
{
bool isPrime = true ;
for (unsigned int i = 2 ; i <= Number / 2 ; ++i)
{
if ( Number % i == 0 )
{
isPrime = false ;
break ;
}
}
return isPrime ;
}
void primeLengthsF ( int array[], int size )
{
int platformSize = 0 ;
for (int i = 0 ; i <= size ; ++i )
{
if ( array [i] == 1 )
{
++platformSize ;
}
if ( array [i] != 1 )
{
if ( primeCheck (platformSize) && platformSize > 1 )
{
cout << platformSize << " " ;
}
platformSize = 0 ;
}
}
}
int primeLengthPlatformsF ( int array[], int size )
{
int platformSize = 0, primeLengthPlatforms = 0 ;
for (int i = size ; i >= 0 ; --i )
{
if ( array [i] == 1 )
{
++platformSize ;
}
if ( array [i] != 1 )
{
if ( primeCheck (platformSize) && platformSize > 1 )
{
++primeLengthPlatforms ;
}
platformSize = 0 ;
}
}
return primeLengthPlatforms ;
}
int rowOfZerosF ( int array[], int i, int rowOfZeros )
{
if ( array [i] == 0)
{
++rowOfZeros ;
}
if ( array [i] == 1 )
{
rowOfZeros = 0 ;
}
return rowOfZeros ;
}
int startingPointF (int array[], int i, int startingPoint)
{
if ( startingPoint == -1 && array[i] == 1 )
{
startingPoint = i ;
}
return startingPoint ;
}
int longestRunF ( int array[], int size, unsigned i, int startingPoint, int rowOfZeros,int& longestRun )
{
if (array[size] == 0)
{
--longestRun ;
}
if (startingPoint == -1 && array[i] == 0)
{
--longestRun ;
}
if (startingPoint != -1 && rowOfZeros <= 3)
{
++longestRun ;
}
return longestRun ;
}
int longestPlatformF (int array[], int i, int rowOfZeros, int longestPlatform , int &previousLongestPlatform)
{
if ( array [i] == 1 )
{
++longestPlatform ;
}
if ( rowOfZeros == 1 )
{
longestPlatform = 0 ;
}
if (longestPlatform > previousLongestPlatform)
{
previousLongestPlatform = longestPlatform ;
}
return longestPlatform ;
}
int longestPlatformStartF ( int longestPlatformStart, int array[], int i, int& previousLongestPlatformStart, int longestPlatform, int previousLongestPlatform, int rowOfZeros )
{
if ( rowOfZeros == 1 )
{
longestPlatformStart = -1 ;
}
if ( longestPlatformStart == -1 && array[i] == 1 )
{
longestPlatformStart = i ;
}
return longestPlatformStart ;
}
int main()
{
int trainingMap [Max_Array_Size], N, rowOfZeros = 0, longestRun = 1, startingPoint = -1 , previousLongestRun = 0, previousStartingPoint = 0, longestPlatform = 0, previousLongestPlatform = 0, longestPlatformStart = -1, previousLongestPlatformStart = 0, primeLengthPlatforms = 0, additionalPlatform = 0, additionalLength = 1 ;
cin >> N ;
if (N > 10000)
{
cout << "Mario's training course should be no bigger than 10000 steps" << endl ;
}
if ( N == 0 )
{
int preSetTrainingMap[5] = { 1,0,1,1,1 } ;
for (int i = 0 ; i < 5 ; ++i )
{
cout << preSetTrainingMap[i] << " " ;
}
cout << endl ;
cout << "Best starting position is 0 and the run length is 5 steps." << endl ;
cout << "Longest platform starts from position 2 and is 3 steps long." << endl ;
cout << "There is 1 prime-length platform. It's length is: 3." << endl ;
cout << "Mario needs 0 new platforms with total length 0." << endl ;
}
else if ( N < 0 )
{
srand (time(0));
int Max = ( rand() % 10 ) + 10 ;
for ( int i = 0 ; i < Max ; ++i )
{
trainingMap[i] = rand() % 2 ;
rowOfZeros = rowOfZerosF ( trainingMap, i, rowOfZeros ) ;
startingPoint = startingPointF ( trainingMap, i, startingPoint );
longestRun = longestRunF ( trainingMap, N, i, startingPoint, rowOfZeros, longestRun ) ;
longestPlatform = longestPlatformF (trainingMap, i, rowOfZeros, longestPlatform, previousLongestPlatform ) ;
longestPlatformStart = longestPlatformStartF( longestPlatformStart, trainingMap, i, previousLongestPlatformStart, longestPlatform, previousLongestPlatform, rowOfZeros) ;
if ( rowOfZeros >= 4 )
{
if (previousStartingPoint < startingPoint)
{
previousStartingPoint = startingPoint ;
}
longestRun -= 3 ;
if (previousLongestRun < longestRun)
{
previousLongestRun = longestRun ;
}
startingPoint = -1 ;
longestRun = 0 ;
rowOfZeros = 0 ;
++additionalPlatform ;
for (int j = 0 ; j < rowOfZeros ; ++j)
{
if ( rowOfZeros - additionalLength > 3 )
{
++additionalLength ;
}
}
}
}
for (int i = 0; i < Max; ++i)
{
cout << trainingMap[i] << " " ;
}
if ( previousLongestRun > longestRun )
{
startingPoint = previousStartingPoint ;
longestRun = previousLongestRun ;
}
cout << "Best starting position is " << startingPoint <<" and the run length is " << longestRun << " steps." << endl ;
if (longestPlatform < previousLongestPlatform)
{
longestPlatform = previousLongestPlatform ;
longestPlatformStart = previousLongestPlatformStart ;
}
cout << "Longest platform starts from " << longestPlatformStart << " and has length of " << longestPlatform << endl ;
primeLengthPlatforms = primeLengthPlatformsF( trainingMap, N ) ;
cout << "There are " << primeLengthPlatforms << " prime-length platforms. " <<"Their lengths: " ;
primeLengthsF ( trainingMap, N ) ;
if ( additionalPlatform == 0 )
{
additionalLength = 0 ;
}
cout << "Mario needs " << additionalPlatform << " new platforms with total length " << additionalLength << endl ;
}
else if ( N > 0 && N < 10000 )
{
for ( int i = 0 ; i < N; ++i )
{
cin >> trainingMap [i] ;
if (trainingMap [i] != 0 && trainingMap [i] != 1 )
{
cout << "Numbers you enter shouldn't be anything different but 0 and 1" << endl ;
cin >> trainingMap[i] ;
}
rowOfZeros = rowOfZerosF ( trainingMap, i, rowOfZeros ) ;
startingPoint = startingPointF ( trainingMap, i, startingPoint );
longestRun = longestRunF ( trainingMap, N, i, startingPoint, rowOfZeros, longestRun ) ;
longestPlatform = longestPlatformF (trainingMap, i, rowOfZeros, longestPlatform, previousLongestPlatform ) ;
longestPlatformStart = longestPlatformStartF( longestPlatformStart, trainingMap, i, previousLongestPlatformStart, longestPlatform, previousLongestPlatform, rowOfZeros) ;
if ( rowOfZeros >= 4 )
{
if (previousStartingPoint < startingPoint)
{
previousStartingPoint = startingPoint ;
}
longestRun -= 3 ;
if (previousLongestRun < longestRun)
{
previousLongestRun = longestRun ;
}
startingPoint = -1 ;
longestRun = 0 ;
rowOfZeros = 0 ;
++additionalPlatform ;
if ( rowOfZeros - additionalLength > 3 )
{
++additionalLength ;
}
}
}
if ( previousLongestRun > longestRun )
{
startingPoint = previousStartingPoint ;
longestRun = previousLongestRun ;
}
cout << "Best starting position is " << startingPoint <<" and the run length is " << longestRun << " steps." << endl ;
if (longestPlatform < previousLongestPlatform)
{
longestPlatform = previousLongestPlatform ;
longestPlatformStart = previousLongestPlatformStart ;
}
cout << "Longest platform starts from " << longestPlatformStart << " and has length of " << longestPlatform << endl ;
primeLengthPlatforms = primeLengthPlatformsF( trainingMap, N ) ;
cout << "There are " << primeLengthPlatforms << " prime-length platforms. " <<"Their lengths: " ;
primeLengthsF ( trainingMap, N ) ;
if ( additionalPlatform == 0 )
{
additionalLength = 0 ;
}
cout << "Mario needs " << additionalPlatform << " new platforms with total length " << additionalLength << endl ;
}
return 0;
}