【问题标题】:How to use loop to display looped information all at once? Functionality Issue如何使用循环一次显示所有循环信息?功能问题
【发布时间】:2020-06-01 00:16:04
【问题描述】:

我正在学习 C++,希望对下面的代码获得一些功能方面的帮助。 我的代码的快速总结/用法:程序是显示随机(x,y)坐标,然后在网格中打印出坐标。

关于随机化 (x,y) 坐标,然后显示它们的网格位置,我完成了所有工作。

问题我遇到的是我的代码为每个坐标显示一个单独的网格,而不是在同一个网格上显示所有坐标。 [我在下面附上了我当前输出的图片]。

我知道这是一个功能问题..但是我无法考虑如何操作我的循环以便首先显示坐标,然后是一个带有所有坐标的网格...我希望这能让感觉。

我的代码片段:

//Note: value of n and k is given by user earlier in the code
vector<vector<int> > vec( n , vector<int> (n));
cout << "\nGrid with city locations:\n";
for(i=0; i<k; i++) {
    //random select int coordinates (x,y) for each K(cities) 
    x = rand() % n + 0; 
    y = rand() % n + 0;
    arrCity[i] = i;

    //display coordinates for city 1..city2.. etc
    cout << "City " << arrCity[i] <<": (" << x << "," << y << ")" << endl;

    //display cities on grid
    for (int rows=0; rows < n; rows++) {
        for (int columns=0; columns < n; columns++) {
            if ((rows == y) && (columns == x)) {
                cout << "|" << (i);
            } else {
                cout << "|_";
            }

        }
        cout << "\n";
    }
    cout << "\n";
}

电流输出:

如您所见,每个“城市坐标”都有一个单独的网格

【问题讨论】:

  • 您在循环内绘制网格,在其中随机化城市,这就是它多次绘制的原因。您需要随机化城市并存储城市编号和 x、y、坐标,例如在向量或结构数组中。然后在绘制网格时检查该单元格位置是否与城市匹配。如果是,则绘制正确的城市编号

标签: c++ loops random grid coordinates


【解决方案1】:

您需要存储所有城市坐标,以便在单个网格打印上显示它们。 在下面的代码中,我更改了一些内容,希望能解决您的问题。

  • 我已将所有与城市相关的数据移动到一个结构中
  • 然后在网格输出之前初始化所有城市
  • 打印网格时,我们必须搜索所有城市,如果它们的坐标与当前位置匹配,则打印相应的索引。

Live Demo

#include <vector>
#include <iostream>

struct City
{
    int index;
    int x, y;

    City(int index_, int x_, int y_)
        : index(index_), x(x_), y(y_)
    { }
};

int main()
{
    int n = 10;
    int k = 6;

    std::vector<City> arrCity;
    arrCity.reserve(k);

    for(int i = 0; i < k; i++)
        arrCity.emplace_back(i, rand() % n, rand() % n);

    std::cout << "\nGrid with city locations:\n";

    for (int k = 0; k < arrCity.size(); k++)
        std::cout << "City " << arrCity[k].index << ": (" << arrCity[k].x << "," << arrCity[k].y << ")" << std::endl;

    //display cities on grid
    for (int i=0; i < n; i++) {
        for (int j=0; j < n; j++) {
            int w = -1;
            for (int k = 0; k < arrCity.size(); k++)
                if ((i == arrCity[k].y) && (j == arrCity[k].x))
                    w = k;

            if (w >= 0)
                std::cout << "|" << arrCity[w].index;
            else
                std::cout << "|_";
        }
        std::cout << "\n";
    }

    return 0;
}

【讨论】:

    【解决方案2】:

    您需要跟踪已访问过哪些单元格。这就是为什么你需要使用另一个数组来存储已经访问过的单元格以及访问的值。

    int vis[n][n];
    memset(vis, -1, sizeof vis);
    
    for(i=0; i<k; i++) {
        //random select int coordinates (x,y) for each K(cities)
        x = rand() % n + 0;
        y = rand() % n + 0;
        arrCity[i] = i;
        vis[x][y] = i;
    
        //display coordinates for city 1..city2.. etc
        cout << "City " << arrCity[i] <<": (" << x << "," << y << ")" << endl;
    
        //display cities on grid
        for (int rows=0; rows < n; rows++) {
            for (int columns=0; columns < n; columns++) {
                if (vis[rows][columns] != -1) {
                    cout << "|" << (vis[rows][columns]);
                } else {
                    cout << "|_";
                }
    
            }
            cout << "\n";
        }
        cout << "\n";
    }
    

    输出:

    【讨论】:

    • 这不是一个非常理想的解决方案,因为使用int vis[n][n] 会浪费内存用于未使用的元素。此外,它仍然没有在单个网格上打印所有内容。
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2016-06-27
    • 2019-05-27
    • 2012-11-15
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多