我试图解决算法问题,我是新手,我正在尝试在编程问题上练习很多。所以我想构造一个单位矩阵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;
}
答案 0 :(得分:3)
有一种更简单的方法。
首先,您应该使用指定的大小分配矩阵。然后,你知道只有对角线是1
s:
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;
您可以看到实时示例here。
您的错误来自这一行:
int nPaddings = n-steps;
实际上,您没有使用dest
的大小来计算填充,这是不正确的。 See here,我添加了一些调试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;
}
您可以在行动here中看到它。
答案 1 :(得分: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;
}