按字典顺序升序对矩阵的行进行排序

时间:2019-06-22 16:12:19

标签: c++ algorithm sorting

嗨,我需要一项家庭作业任务的帮助。我需要提高所拥有代码的速度,请帮助我吗? enter image description here

那是我的代码:

int main(int argc, char** argv) {
    int n,m;
    scanf("%d %d",&n,&m);
    int table[n][m];

    for(int i=0;i<n;i++) {
        for(int j=0;j<m;j++)
        {
            cin>>table[i][j];
        }
    }

    bool isSorted = false;

    while (!isSorted) {
        isSorted = true;

        for(int i=0;i<n - 1;i++)
        {
            std::string str = "";
            std::string str2 = "";
            for(int j=0;j<m;j++) {
                str += table[i][j] + '0';
                str2 += table[i+1][j] + '0';
            }

            // cout << str2 << " < " << str << " Bool " << (str2 > str) << endl;

            if (str2 < str) {
                for (int k = 0; k < n; k++)
                {
                    int t = table[i][k];
                    table[i][k] = table[i + 1][k];
                    table[i + 1][k] = t;
                }
                isSorted = false;
                break;
            }

        }
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        cout<<table[i][j]<<" ";
        cout<<endl;
    }

    return 0;
}

任务是: 给出了具有M条线和N条梯子的矩阵。要按字典顺序升序对矩阵的行进行排序,即如果A = A(1),A(2),...,A(n)和B = B(1),B(2)..., B(n)是矩阵的两行,这样A(1)= B(1),A(2)= B(2)和A(k + 1)B(k +1)就是排序后的矩阵。 / p>

输入格式

以下各键应在下表中列出。

Constraints

3<M<1024, 3<N<1024

输出格式

您必须计算矩阵。

Sample Input

4 4
6 1 1 2
7 2 9 4
7 3 1 5
7 2 9 3

Sample Output

6 1 1 2
7 2 9 3
7 2 9 4
7 3 1 5

1 个答案:

答案 0 :(得分:1)

我想为您提供支持,并向您展示许多可能的解决方案之一。

您的程序仍有许多C样式的元素,例如纯数组或scanf。 我想切换到现代C ++。

强烈建议使用STL和算法。

而且,如今人们将始终使用std :: vector的std :: vector之类的STL容器来实现矩阵的概念。

我将所有内容都嵌入到一个类中。

读写值将通过iostream库完成。对于用户定义的类型,提取器和插入器运算符(>>和<<)将被覆盖。这样一来,便可以轻松使用现有的io工具。

算法可用于处理部分或整个容器。容器元素的元素明智操作将使用迭代器完成。正如您将看到的,我们将值从输入“复制”到矩阵,再返回到输出。

因此,在下面的清单中,您将看到用3行代码完成的主要任务。

还请注意:排序非常快。而且行之有效,因为std :: vectors还存在小于运算符(<)

请参阅:

编辑:代码已根据L.F.的评论

更新
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>


// Constraints for number of rows and columns
constexpr int ConstraintMin = 3;
constexpr int ConstraintMax = 1024;
// Custom Class to hold a matrix
class Matrix
{
public:
    // Explicit constructor. Make a matrix with the requested numbers of rows and columns
    explicit Matrix(const size_t numberOrRows, const size_t numberOfColumns) { matrix.resize(numberOrRows); std::for_each(matrix.begin(), matrix.end(), [numberOfColumns](Columns & c) { c.resize(numberOfColumns); });     }
    // Main Function of Matrix: Sort.  
    void sort() { std::sort(matrix.begin(), matrix.end()); }

    // Overload extratcor.  Read all data for Matrix from an istream
    friend std::istream& operator >> (std::istream& is, Matrix& m) {
        std::for_each(m.matrix.begin(), m.matrix.end(), [&is](Columns& columns) {std::copy_n(std::istream_iterator<int>(is), columns.size(), columns.begin()); });
        return is;
    }
    // Overload inserter. Write Matrix to ostream
    friend std::ostream& operator << (std::ostream& os, const Matrix& m) {
        std::for_each(m.matrix.begin(), m.matrix.end(), [&os](const Columns & columns) {std::copy_n(columns.begin(), columns.size(), std::ostream_iterator<int>(os, " ")); std::cout << '\n'; });
        return os;
    }
protected:
    // The columns in one row of the matrix
    using Columns = std::vector<int>;
    // The container for the data. A vector of columns
    std::vector<Columns> matrix{};
};

int main()
{
    // This will hold the number of rows and columns of our matrix
    int rows{ 0 }, columns{ 0 };

    std::cout << "\nMatrix Sort\n\nEnter the number of rows and columns of the matrix:\n";

    // Read number of rows and columns from user
    std::cin >> rows >> columns;
    // Keep values in limit constraints
    rows = std::min(std::max(ConstraintMin, rows), ConstraintMax);
    columns = std::min(std::max(ConstraintMin, columns), ConstraintMax);

    // Define the matrix with the given numbers of rows and columns
    Matrix matrix(rows, columns);

    std::cout << "\nPlease enter " << rows << " rows and " << columns << " columns:\n";

    // Read the complete matrix
    std::cin >> matrix;
    // Sort it
    matrix.sort();
    // And show the result
    std::cout << "\n\nResult. Sorted matrix:\n" << matrix;

    return 0;
}

我希望,我能给您对现代C ++的强大印象。 。

相关问题