Advertisement
Andites

оиб лаба 3 ver.2

Nov 2nd, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. #include <Windows.h>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. setlocale(LC_ALL, "Russian");
  11. SetConsoleCP(1251);
  12. SetConsoleOutputCP(1251);
  13. string W;
  14. int sq = 0;
  15. cout << "Введите слово: ";
  16. cin >> W;
  17. cout << "Длина: " << W.length() << endl;
  18. int le = W.length();
  19. for (int i = 0; i < le * le; i++) {
  20. if (le < pow(i, 2)) {
  21. sq = i*i;
  22. break;
  23. }
  24. }
  25. sq = sqrt(sq);
  26. cout <<"Сторона получившегося квадрата: " << sq <<endl;
  27. int f = W.length();
  28. while (W.length() < sq*sq) {
  29. W += 'а' + rand() % 33;
  30. }
  31. cout << "Новое слово: " << W <<endl;
  32.  
  33.  
  34. int sq2 = sq;
  35. int** mat;
  36. mat = new int* [sq]; //////////двумерный динамический массив
  37. for (int i = 0; i < sq; i++) {
  38. mat[i] = new int [sq2];
  39. }
  40. int sh = 1;
  41. int l = 0;
  42. for (int i = 0; i < sq; i++) {
  43. for (l = 0; l < sq; l++) {
  44. mat[i][l] = 0;
  45. }
  46. }
  47.  
  48. l = 0;
  49. for (int sh = 0; sh < sq * sq;) {
  50. for (int i = 0; i < sq;) {
  51. mat[i][l] = sh;
  52. sh++;
  53. i++;
  54. for (l; l < sq;) {
  55. if (mat[i][l] != 0) {
  56. break;
  57. }
  58. mat[i][l] = sh;
  59. sh++;
  60. while (i > 0) {
  61. i--;
  62. l++;
  63. mat[i][l] = sh;
  64. sh++;
  65. }
  66. l++;
  67. mat[i][l] = sh;
  68. sh++;
  69. while (l > 0) {
  70. l--;
  71. i++;
  72. mat[i][l] = sh;
  73. sh++;
  74. }
  75.  
  76. }
  77. if (i == sq - 1) {
  78. l++;
  79. mat[i][l] = sh;
  80. sh++;
  81. while (l < sq - 1) {
  82. i--;
  83. l++;
  84. mat[i][l] = sh;
  85. sh++;
  86. }
  87. i++;
  88. mat[i][l] = sh;
  89. sh++;
  90. while (i < sq - 1) {
  91. l--;
  92. i++;
  93. mat[i][l] = sh;
  94. sh++;
  95. }
  96. if ((i == sq - 1) && (l == sq - 1)) {
  97. break;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. for (int i = 0; i < sq; i++) {
  104. for (l = 0; l < sq; l++) {
  105. cout << mat[i][l] << " ";
  106. }
  107. cout << endl;
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement