如题所述
如果我没记错,上三角应该是指主对角线以下全是0的方阵,那这样的话,只要令主对角线以下置零就好了,主要分析点就是:主对角线以下,行数比列数大。
最终实现程序如下,键盘录入矩阵数据,最后打印出上三角:
using namespace std;
#define N 5 //可通过改变此值修改方阵行列数
int a[N][N]={0};
int main()
{
int row = 0;
int col = 0;
for(row=0;row<N;row++)
{
for(col=0;col<N;col++)
{
cout << "请输入第 " << row+1 << " 行,第 " << col << " 列数:" ;
cin >> a[row][col];
}
cout << endl;//换行区分录入下一行
}
for(row=0;row<N;row++)
{
for(col=0;col<N;col++)
{
/*行数大于列数输出0,否则输出实际值*/
if(row > col)
cout << "0 ";
else
cout << a[row][col] << " " ;
}
cout << endl;//打印完一行之后换行
}
return 0;
}
如理解有误欢迎提问,望采纳!
温馨提示:答案为网友推荐,仅供参考
第1个回答 2019-05-05
#include<iostream>
using namespace std;
int main()
{
const int w=2;
int i,j,a[5][5];
for(i=0;i<5;i++)
{
if(i>0)
{
cout.width(i*(w+1));
cout.fill(' ');
cout<<' ';
}
for(j=i;j<5;j++)
{
a[i][j]=i*5+j+1;
cout.width(2);
cout.fill('0');
cout<<a[i][j]<<(4!=j ? ' ' : '\n');
}
}
return 0;
}
using namespace std;
int main()
{
const int w=2;
int i,j,a[5][5];
for(i=0;i<5;i++)
{
if(i>0)
{
cout.width(i*(w+1));
cout.fill(' ');
cout<<' ';
}
for(j=i;j<5;j++)
{
a[i][j]=i*5+j+1;
cout.width(2);
cout.fill('0');
cout<<a[i][j]<<(4!=j ? ' ' : '\n');
}
}
return 0;
}