简体   繁体   English

我怎么能把这个硬代码变成 C++ 中的 while 循环?

[英]How could I make this hard code into a while loop in C++?

So basically I'm trying to make this matrix (with a 2d array) in the format所以基本上我试图以这种格式制作这个矩阵(带有二维数组)

1 

1 1
1 1

1 1 1
1 2 1
1 1 1

1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1 

1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

depending on what size the user inputs.取决于用户输入的大小。 I can do this with hard code as shown:我可以用硬代码来做到这一点,如图所示:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int userSize;


    do {
        cin >> userSize;
        int userArray[userSize][userSize];
        int size = 1;
        int maxSize = 99;

        for (int i = 0; i < userSize; i++) {
            for (int j = 0; j < userSize; j++) {

                if ((j > 2 && j < userSize - 3) &&
                   (i > 2 && i < userSize - 3)){
                    userArray[i][j] = size + 3;
                }



                if ((j > 1 && j < userSize - 2) &&
                (i > 1 && i < userSize - 2)) {
                    userArray[i][j] = size + 2;
                }


                else if ((j > 0 && j < userSize - 1) &&
                    (i > 0 && i < userSize - 1)) {

                    userArray[i][j] = size + 1;
                }

                else {
                    userArray[i][j] = size;
                }

            }
        }

        for (int i = 0; i < userSize; i++) {
            for (int j = 0; j < userSize; j++) {
                cout << setw(3) << userArray[i][j];
            }
            cout << endl;
        }
        cout << endl;


    }while(userSize != 0);



    return 0;
}

however I need to be able to do this up to 99, which would obviously be a lot to write.但是我需要能够做到这一点高达 99,这显然会写很多。 Is there a way I can do this with a while loop instead?有没有办法用while循环来做到这一点? Below is my failed attempt that only outputs garbage:以下是我只输出垃圾的失败尝试:


#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int userSize;
    int maxSize = 99;
    int size = 1;

    do {
        cin >> userSize;

        int myMatrix[userSize][userSize];

        for (int i = 0; i < userSize; i++) {
            for (int j = 0; j < userSize; j++) {

                while (maxSize >= 0) {

                    if ((j > maxSize) && (j < userSize - (maxSize + 1)) &&
                        ((i > maxSize) && (i < userSize - (maxSize + 1)))) {
                            myMatrix[i][j] = size + maxSize;
                        }

                    /*
                    if ((j > 1 && j < userSize - 2) &&
                    (i > 1 && i < userSize - 2)) {
                        myMatrix[i][j] = size + 2;
                    }


                    else if ((j > 0 && j < userSize - 1) &&
                        (i > 0 && i < userSize - 1)) {

                        myMatrix[i][j] = size + 1;
                    }
                    */

                    else {
                        myMatrix[i][j] = size;
                     }



                    maxSize = maxSize - 1;
                }
            }
        }

        for (int i = 0; i < userSize; i++) {
            for (int j = 0; j < userSize; j++) {
                cout << setw(3) << myMatrix[i][j];
            }
            cout << endl;
        }
        cout << endl;
    }while(userSize != 0);

    return 0;
}

looks to me like you want the minimum steps from an outer edge.在我看来,您想要从外边缘开始的最小步长。 Here is some psuedo-code这是一些伪代码

int[,] ComputeMatrix(int m,int n)
{
     int[,] matrix = new int[m,n]
     for(int i = 0;i < m;i++)
     for(int j = 0;j < n;j++)
     {
          //Minimum distance from each of the 4 walls
          matrix[i,j] = 1 + Math.Min(Math.Min(Math.Min(i,j),m-i-1),n-j-1);
     }

      return matrix;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM