aboutsummaryrefslogtreecommitdiff
path: root/challenge-119/paulo-custodio/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-07-01 16:05:09 +0200
committerAbigail <abigail@abigail.be>2021-07-01 16:05:09 +0200
commit2ab88a15e2e63d52dd5c90a558d673774fe3bdf5 (patch)
tree88ad04f1c7396a5b976853549716ee85a94f2881 /challenge-119/paulo-custodio/cpp/ch-2.cpp
parent20c14072c2b9a491fd414d275b6c64c6fec8e62f (diff)
parente5714129b83b9d88dbbe7b9bd58a7738228f7e2f (diff)
downloadperlweeklychallenge-club-2ab88a15e2e63d52dd5c90a558d673774fe3bdf5.tar.gz
perlweeklychallenge-club-2ab88a15e2e63d52dd5c90a558d673774fe3bdf5.tar.bz2
perlweeklychallenge-club-2ab88a15e2e63d52dd5c90a558d673774fe3bdf5.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-119/paulo-custodio/cpp/ch-2.cpp')
-rw-r--r--challenge-119/paulo-custodio/cpp/ch-2.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-119/paulo-custodio/cpp/ch-2.cpp b/challenge-119/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..be94768168
--- /dev/null
+++ b/challenge-119/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,59 @@
+/*
+Challenge 119
+
+TASK #2 - Sequence without 1-on-1
+Submitted by: Cheok-Yin Fung
+Write a script to generate sequence starting at 1. Consider the increasing
+sequence of integers which contain only 1's, 2's and 3's, and do not have any
+doublets of 1's like below. Please accept a positive integer $N and print the
+$Nth term in the generated sequence.
+
+1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ...
+
+Example
+Input: $N = 5
+Output: 13
+
+Input: $N = 10
+Output: 32
+
+Input: $N = 60
+Output: 2223
+*/
+
+#include <iostream>
+using namespace std;
+
+bool num_ok(int n) {
+ int last_digit, digit;
+
+ if (n <= 0)
+ return false;
+ while (n > 0) {
+ last_digit = digit;
+ digit = n % 10;
+ n /= 10;
+ if (digit < 1 || digit > 3 || (digit == 1 && last_digit == 1))
+ return false;
+ }
+ return true;
+}
+
+int next_seq(int seq) {
+ while (1) {
+ seq++;
+ if (num_ok(seq))
+ return seq;
+ }
+}
+
+int main(int argc, char* argv[]) {
+ int num = 0;
+ if (argc == 2) num = atoi(argv[1]);
+
+ int seq = 0;
+ for (int i = 0; i < num; i++)
+ seq = next_seq(seq);
+
+ cout << seq << endl;
+}