fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void print_matrix(vector<vector <int>>& matrix){
  5. cout << "[\n";
  6. int row = matrix.size();
  7.  
  8. for(int i = 0 ; i < row; i++){
  9. cout << "[";
  10. int cur_row_size = matrix[i].size();
  11. for(int j = 0; j < cur_row_size; j++){
  12. cout << matrix[i][j] ;
  13.  
  14. if(j != cur_row_size-1){
  15. cout << ",";
  16. }
  17. }
  18. cout << "]";
  19. if(i != row-1){
  20. cout << ",";
  21. }
  22.  
  23. cout << "\n";
  24. }
  25.  
  26. cout << "]";
  27. }
  28.  
  29. vector<vector<int>> zoom(const vector<vector<int>>& matrix, int zoom_factor) {
  30. int n = matrix.size();
  31. int m = matrix[0].size();
  32.  
  33. vector<vector<int>> result(n * zoom_factor, vector<int>(m * zoom_factor));
  34.  
  35. for(int i = 0; i < n; i++){
  36. for(int j = 0; j < m; j++){
  37. for(int di = 0; di < zoom_factor; di++){
  38. for(int dj = 0; dj < zoom_factor; dj++){
  39. result[i*zoom_factor + di][j*zoom_factor + dj] = matrix[i][j];
  40. }
  41. }
  42. }
  43. }
  44.  
  45. return result;
  46. }
  47.  
  48. int main(){
  49. vector<vector<int>> numbers;
  50. vector<vector<int>> result;
  51. int zoom_factor;
  52. string input_str;
  53.  
  54. cout << "Input array of int that you want to zoom with no spacing. Format Example: [[1,2],[3,4]]\n> ";
  55. cin >> input_str;
  56.  
  57. if(!(input_str.empty())){
  58. input_str.erase(0,1);
  59. }
  60.  
  61. if(!(input_str.empty())){
  62. input_str.pop_back();
  63. }
  64.  
  65.  
  66. int cur_num = 0;
  67. bool is_neg = false;
  68. vector<int> cur_row;
  69. bool in_num = false;
  70.  
  71. for(char c: input_str){
  72.  
  73. if(c =='-'){
  74. is_neg = true;
  75. }
  76.  
  77. if(isdigit(c)){
  78. cur_num = cur_num*10 + (c - '0');
  79. in_num = true;
  80. }
  81.  
  82. else{
  83. if(in_num){
  84. if(is_neg) cur_num *= -1;
  85. cur_row.push_back(cur_num);
  86.  
  87. cur_num = 0;
  88. is_neg =false;
  89. in_num = false;
  90. }
  91.  
  92. if(c ==']'){
  93. numbers.push_back(cur_row);
  94. cur_row.clear();
  95. }
  96. }
  97.  
  98. }
  99.  
  100. cout << "\nInput the zoom amount (zoom factor). Example: 3\n> ";
  101. cin >> zoom_factor;
  102.  
  103. result = zoom(numbers, zoom_factor);
  104. cout << "\nResult:\n";
  105. print_matrix(result);
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0.01s 5312KB
stdin
[[1,2],[3,4]]
3
stdout
Input array of int that you want to zoom with no spacing. Format Example: [[1,2],[3,4]]
> 
Input the zoom amount (zoom factor). Example: 3
> 
Result:
[
[1,1,1,2,2,2],
[1,1,1,2,2,2],
[1,1,1,2,2,2],
[3,3,3,4,4,4],
[3,3,3,4,4,4],
[3,3,3,4,4,4]
]