/
ch-2.cpp
53 lines (41 loc) · 1001 Bytes
/
ch-2.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
/*
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;
}