aboutsummaryrefslogtreecommitdiff
path: root/challenge-240/paulo-custodio/cpp/ch-2.cpp
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-11-20 15:40:49 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-11-20 15:40:49 +0800
commit72dc5412e640e26601ffdebc4f0247db4c4bc7fe (patch)
treed405aa98925dfed0b745e5ec331e9498005b1de7 /challenge-240/paulo-custodio/cpp/ch-2.cpp
parent35d79358f3d12607da66ffefefed5e93c469d396 (diff)
parentfda51162ad0e1e8b4489fd2c73ef7dfdc8f0df5e (diff)
downloadperlweeklychallenge-club-72dc5412e640e26601ffdebc4f0247db4c4bc7fe.tar.gz
perlweeklychallenge-club-72dc5412e640e26601ffdebc4f0247db4c4bc7fe.tar.bz2
perlweeklychallenge-club-72dc5412e640e26601ffdebc4f0247db4c4bc7fe.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-240/paulo-custodio/cpp/ch-2.cpp')
-rw-r--r--challenge-240/paulo-custodio/cpp/ch-2.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-240/paulo-custodio/cpp/ch-2.cpp b/challenge-240/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..9487ed0fea
--- /dev/null
+++ b/challenge-240/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,53 @@
+/*
+Challenge 240
+
+Task 2: Build Array
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to create an array such that new[i] = old[old[i]] where 0 <= i < new.length.
+Example 1
+
+Input: @int = (0, 2, 1, 5, 3, 4)
+Output: (0, 1, 2, 4, 5, 3)
+
+Example 2
+
+Input: @int = (5, 0, 1, 2, 3, 4)
+Output: (4, 5, 0, 1, 2, 3)
+*/
+
+#include <iostream>
+#include <vector>
+using namespace std;
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ cerr << "Usage: ch-2 n n n ..." << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ vector<int> olds;
+ vector<int> news;
+
+ // parse args
+ for (int i = 1; i < argc; i++) {
+ int n = atoi(argv[i]);
+ olds.push_back(n);
+ }
+
+ // process
+ for (size_t i = 0; i < olds.size(); i++) {
+ int n = olds[olds[i]];
+ news.push_back(n);
+ }
+
+ // output
+ const char* sep = "";
+ for (size_t i = 0; i < news.size(); i++) {
+ cout << sep << news[i];
+ sep = " ";
+ }
+ cout << endl;
+}