Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- #include <Windows.h>
- using namespace std;
- int main()
- {
- setlocale(LC_ALL, "Russian");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string W;
- int sq = 0;
- cout << "Введите слово: ";
- cin >> W;
- cout << "Длина: " << W.length() << endl;
- int le = W.length();
- for (int i = 0; i < le * le; i++) {
- if (le < pow(i, 2)) {
- sq = i*i;
- break;
- }
- }
- sq = sqrt(sq);
- cout <<"Сторона получившегося квадрата: " << sq <<endl;
- int f = W.length();
- while (W.length() < sq*sq) {
- W += 'а' + rand() % 33;
- }
- cout << "Новое слово: " << W <<endl;
- int sq2 = sq;
- int** mat;
- mat = new int* [sq]; //////////двумерный динамический массив
- for (int i = 0; i < sq; i++) {
- mat[i] = new int [sq2];
- }
- int sh = 1;
- int l = 0;
- for (int i = 0; i < sq; i++) {
- for (l = 0; l < sq; l++) {
- mat[i][l] = 0;
- }
- }
- l = 0;
- for (int sh = 0; sh < sq * sq;) {
- for (int i = 0; i < sq;) {
- mat[i][l] = sh;
- sh++;
- i++;
- for (l; l < sq;) {
- if (mat[i][l] != 0) {
- break;
- }
- mat[i][l] = sh;
- sh++;
- while (i > 0) {
- i--;
- l++;
- mat[i][l] = sh;
- sh++;
- }
- l++;
- mat[i][l] = sh;
- sh++;
- while (l > 0) {
- l--;
- i++;
- mat[i][l] = sh;
- sh++;
- }
- }
- if (i == sq - 1) {
- l++;
- mat[i][l] = sh;
- sh++;
- while (l < sq - 1) {
- i--;
- l++;
- mat[i][l] = sh;
- sh++;
- }
- i++;
- mat[i][l] = sh;
- sh++;
- while (i < sq - 1) {
- l--;
- i++;
- mat[i][l] = sh;
- sh++;
- }
- if ((i == sq - 1) && (l == sq - 1)) {
- break;
- }
- }
- }
- }
- for (int i = 0; i < sq; i++) {
- for (l = 0; l < sq; l++) {
- cout << mat[i][l] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement