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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
Challenge 204
Task 2: Reshape Matrix
Submitted by: Mohammad S Anwar
You are given a matrix (m x n) and two integers (r) and (c).
Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0.
Example 1
Input: [ 1 2 ]
[ 3 4 ]
$matrix = [ [ 1, 2 ], [ 3, 4 ] ]
$r = 1
$c = 4
Output: [ 1 2 3 4 ]
Example 2
Input: [ 1 2 3 ]
[ 4 5 6 ]
$matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
$r = 3
$c = 2
Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
[ 1 2 ]
[ 3 4 ]
[ 5 6 ]
Example 3
Input: [ 1 2 ]
$matrix = [ [ 1, 2 ] ]
$r = 3
$c = 2
Output: 0
*/
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
std::vector<int> data;
size_t rows = 0, cols = 0;
void parse_data() {
std::string line;
while (std::getline(std::cin, line)) {
if (line.find('[') != std::string::npos) {
size_t pos;
while ((pos = line.find('[')) != std::string::npos) line[pos] = ' ';
while ((pos = line.find(']')) != std::string::npos) line[pos] = ' ';
std::istringstream iss{ line };
int n;
while (iss >> n) {
data.push_back(n);
}
}
else {
std::istringstream iss{ line };
iss >> rows >> cols;
break;
}
}
}
void output_data() {
if (data.size() != rows*cols)
std::cout << "0" << std::endl;
else {
for (int i = 0; i < rows; i++) {
std::cout << "[ ";
for (int j = 0; j < cols; j++) {
std::cout << data[i*cols+j] << " ";
}
std::cout << "]" << std::endl;
}
}
}
int main() {
parse_data();
output_data();
}
|