Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
joseph.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
252#include "tpl_dynDlist.H"
253#include <tclap/CmdLine.h>
254#include <cstdio>
255#include <iostream>
256
257using namespace Aleph;
258
260{
261 for (unsigned i = 0; i < s; i++)
262 {
263 if (not itor.has_curr())
264 itor.reset_first();
265
266 itor.next();
267 }
268
269 if (not itor.has_curr())
270 itor.reset_first();
271}
272void orden_ejecucion(unsigned num_personas, unsigned paso)
273{
275
276 if (paso == 0)
277 {
278 printf("Step size must be >= 1\n");
279 return;
280 }
281
282 for (unsigned i = 1; i <= num_personas; i++)
283 list.append(i);
284
286 for (/* nothing */; num_personas > 1; num_personas--)
287 {
288 avanceItor(itor, paso - 1);
289 printf("%u ", itor.get_curr());
290 itor.del();
291 }
292
293 printf("\nSurvivor is %u\n", list.get_first());
294}
295
296int main(int argc, char** argv)
297{
298 try {
299 TCLAP::CmdLine cmd("Josephus problem", ' ', "1.0");
300
301 TCLAP::ValueArg<unsigned> numPerArg("n", "num-persons",
302 "Number of persons",
303 false, 20, "unsigned");
304 cmd.add(numPerArg);
305
306 TCLAP::ValueArg<unsigned> pasoArg("p", "paso",
307 "Step size",
308 false, 7, "unsigned");
309 cmd.add(pasoArg);
310
311 cmd.parse(argc, argv);
312
313 unsigned numPer = numPerArg.getValue(),
314 paso = pasoArg.getValue();
315
317 } catch (TCLAP::ArgException &e) {
318 std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
319 }
320 return 0;
321}
int main()
Iterator dynamic list.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
T & get_first() const
Return a modifiable reference to first item in the list.
void avanceItor(DynDlist< unsigned >::Iterator &itor, unsigned s)
Definition joseph.C:259
void orden_ejecucion(unsigned num_personas, unsigned paso)
Definition joseph.C:272
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Dynamic doubly linked list implementation.