0

我试图解决一个算法问题,我是新手,我正在尝试大量练习编程问题。所以我想构造一个单位矩阵 n*n。我想出了一个愚蠢的解决方案,它适用于 4*4 矩阵,但不适用于 5*5。我知道它奇怪的解决方案,当我看到它时,问题的解决方案真的很容易。我需要知道我做错了什么以便我可以学习,我的解决方案真的很愚蠢,在解决这些问题之后我会变得更好吗?

#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip>  // for setw, setfill

using namespace std;

int binary(int number);

int main()
{

    vector<vector<int> > matrix;

    cout<<"Please enter the size of the identity matrix"<<endl;
    int n;
    cin>>n;

    matrix.resize(n);
    for (int i=0; i<n;i++)
    {

        matrix[i].resize(n);
    }

    int steps = 1<<n-1;

    int bin  = binary(steps);
    ostringstream binString;

    binString <<bin;
    if(binString.str().size()<n)
    {
        std::string dest = binString.str();
        int nPaddings = n-binString.str().size();
        if (nPaddings==0) nPaddings=1;
        dest = std::string( nPaddings, '0').append( binString.str());
        binString.str("");
        binString<<dest;
    }
    for (int col = 0; col<n; col++)
    {
        if(col>=1)
        {
            steps= (int)steps/2;
            int bin = binary(steps);
            binString.str("");
            binString << bin;
            if(binString.str().size()<n)
            {
                std::string dest = binString.str();
                int nPaddings = n-steps;
                if (nPaddings==0) nPaddings=1;
                dest = std::string( nPaddings, '0').append( binString.str());
                binString.str("");
                binString<<dest;
            }
        }
        for (int row=0; row<n; row++)
        {
            matrix[col][row] =binString.str().at(row)-'0';
        }
    }


    return 0;
}

int binary(int number) {
    long rem,i=1,sum=0;
    do
    {
        rem=number%2;
        sum=sum + (i*rem);
        number=number/2;
        i=i*10;
    }while(number>0);

    return sum;
}
4

2 回答 2

3

有一种更简单的方法可以做到这一点。

首先,您应该分配具有指定大小的矩阵。那么,你知道只有对角线是1s:

vector<vector<int> > matrix;
int n;

cout << "Please enter the size of the identity matrix" << endl;
cin >> n;

// Initialize the matrix as a n x n array of 0.
matrix = vector<vector<int> >(n, vector<int>(n,0));

// Set the diagonal to be 1s
for(unsigned int t = 0; t < n; t++)
    matrix[t][t] = 1;

你可以在这里看到一个活生生的例子。

编辑:

您的错误来自这一行:

int nPaddings = n-steps;

实际上,您没有使用 的大小dest来计算填充,这是不正确的。看到这里,我添加了一些调试 printfs 来查看变量的状态。您可以看到nPaddings == -3,因此出现错误。

你的想法:

for each column
    get the representation of the column as a string
    set the i-th value of the column as the i-th character of the string

因此,这是一个使用您的想法的更简单的程序。将代码分成几个函数有很大帮助。此外,std::ostringstream这里std::string只是纯粹的矫枉过正。

#include <iostream>
#include <vector>
#include <iomanip>  // for setw, setfill

using namespace std;
std::string binStr(unsigned int exponent, unsigned int size);

int main()
{

    vector<vector<int> > matrix;

    cout<<"Please enter the size of the identity matrix"<<endl;
    int n;
    cin>>n;

// Initialize the matrix
    matrix.resize(n);
    for (int i=0; i<n;i++)
        matrix[i].resize(n);

// Fill the matrix
    for (int col = 0; col<n; col++)
    {
        std::string bin = binStr(n-col,n);
        for (int row=0; row<n; row++)
            matrix[col][row] = bin[row]-'0';
    }


// Print the matrix and return
    for(unsigned int y = 0; y < n; y++)
    {
        for(unsigned int x = 0; x < n; x++)
            cout << "\t" << matrix[y][x];
        cout << "\n";
    }
    return 0;
}

std::string binStr(unsigned int exponent, unsigned int size)
{
    // You do not need a string stream (which is like using a bazooka to kill a fly...)
    // Instead, just create a string of the required length
    // 'str' will contain the binary representation of 2^exponent
    std::string str(size,'0');
    if(exponent <= size && exponent > 0)
        str[size - exponent] = '1';
    return str;
}

您可以在这里看到它的实际效果。

于 2012-12-17T20:09:54.480 回答
3
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int> > make_idty_matrix( int n )
{
    vector<vector<int> > idty( n, vector<int>( n, 0 ));
    for( int i = 0; i < n; ++i )
        idty[i][i] = 1;
    return idty;
}

int main()
{
    vector<vector<int> > matrix = make_idty_matrix( 5 );

    // your code here
    // ...

    return 0;
}
于 2012-12-17T20:23:05.963 回答