aboutsummaryrefslogtreecommitdiff
path: root/challenge-102/paulo-custodio/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-03-05 20:20:08 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-03-05 20:20:08 +0000
commitca934ad2fd8208c9ce9d8a6761c09d7f7ce54194 (patch)
tree6bd752aaaf3f2e6c35d81156583c4ddc4cb2ae67 /challenge-102/paulo-custodio/cpp/ch-2.cpp
parent0821b332ee9c31f57ca94a56c491288f7b46c3e1 (diff)
downloadperlweeklychallenge-club-ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194.tar.gz
perlweeklychallenge-club-ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194.tar.bz2
perlweeklychallenge-club-ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194.zip
Add Basic, C and C++ solutions to challenge 102
Diffstat (limited to 'challenge-102/paulo-custodio/cpp/ch-2.cpp')
-rw-r--r--challenge-102/paulo-custodio/cpp/ch-2.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-102/paulo-custodio/cpp/ch-2.cpp b/challenge-102/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..24fbf2fb06
--- /dev/null
+++ b/challenge-102/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,54 @@
+/*
+Challenge 102
+
+TASK #2 › Hash-counting String
+Submitted by: Stuart Little
+
+You are given a positive integer $N.
+
+Write a script to produce Hash-counting string of that length.
+
+The definition of a hash-counting string is as follows:
+- the string consists only of digits 0-9 and hashes, ‘#’
+- there are no two consecutive hashes: ‘##’ does not appear in your string
+- the last character is a hash
+- the number immediately preceding each hash (if it exists) is the position
+of that hash in the string, with the position being counted up from 1
+
+It can be shown that for every positive integer N there is exactly one such
+length-N string.
+Examples:
+
+(a) "#" is the counting string of length 1
+(b) "2#" is the counting string of length 2
+(c) "#3#" is the string of length 3
+(d) "#3#5#7#10#" is the string of length 10
+(e) "2#4#6#8#11#14#" is the string of length 14
+*/
+
+#include <iostream>
+#include <string>
+
+std::string hash_count(int n) {
+ std::string str(n, ' ');
+ int i = n - 1;
+ while (i >= 0) {
+ int pos = i + 1;
+ str[i--] = '#';
+ while (i >= 0 && pos != 0) {
+ str[i--] = '0' + (pos % 10);
+ pos /= 10;
+ }
+ }
+ return str;
+}
+
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) {
+ std::cerr << "Usage: ch-1 N" << std::endl;
+ return EXIT_FAILURE;
+ }
+ else
+ std::cout << hash_count(atoi(argv[1])) << std::endl;
+}