求c++输出矩阵,用二维数组和循环!
来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/06/25 19:36:05
求c++输出矩阵,用二维数组和循环!
![](http://img.wesiedu.com/upload/b/38/b38e9535a00780c9e13cfef31ec0137c.jpg)
#include <iostream>
using namespace std;
int main()
{
\x09int s;
\x09int a[100][100]={0};
\x09int M;
\x09cin>>M;
\x09for(int i=1;i<=M;i++)
\x09{
\x09\x09s=i;
\x09\x09for(int j=1;j<=M;j++)
\x09
\x09\x09{\x09\x09\x09
\x09\x09\x09if(i+j<=M+1)
\x09\x09\x09{\x09\x09
\x09\x09\x09\x09a[i][j]=i;
\x09\x09\x09
\x09\x09 cout<<a[i][j];
\x09\x09\x09}
\x09\x09 \x09
\x09\x09else
\x09\x09{\x09
\x09\x09\x09
\x09\x09\x09while(s<0)
\x09\x09\x09{
\x09\x09\x09\x09a[i][j]=s;
\x09\x09\x09\x09s--;
\x09\x09\x09}
\x09\x09\x09
\x09\x09 cout<<a[i][j];
\x09\x09}
\x09\x09\x09
\x09\x09}
\x09\x09
\x09\x09 \x09
\x09\x09\x09
\x09
\x09\x09\x09
\x09\x09cout<<endl;
\x09}
}
输出对角线以后全是0,而不是图中那样子的
![](http://img.wesiedu.com/upload/b/38/b38e9535a00780c9e13cfef31ec0137c.jpg)
#include <iostream>
using namespace std;
int main()
{
\x09int s;
\x09int a[100][100]={0};
\x09int M;
\x09cin>>M;
\x09for(int i=1;i<=M;i++)
\x09{
\x09\x09s=i;
\x09\x09for(int j=1;j<=M;j++)
\x09
\x09\x09{\x09\x09\x09
\x09\x09\x09if(i+j<=M+1)
\x09\x09\x09{\x09\x09
\x09\x09\x09\x09a[i][j]=i;
\x09\x09\x09
\x09\x09 cout<<a[i][j];
\x09\x09\x09}
\x09\x09 \x09
\x09\x09else
\x09\x09{\x09
\x09\x09\x09
\x09\x09\x09while(s<0)
\x09\x09\x09{
\x09\x09\x09\x09a[i][j]=s;
\x09\x09\x09\x09s--;
\x09\x09\x09}
\x09\x09\x09
\x09\x09 cout<<a[i][j];
\x09\x09}
\x09\x09\x09
\x09\x09}
\x09\x09
\x09\x09 \x09
\x09\x09\x09
\x09
\x09\x09\x09
\x09\x09cout<<endl;
\x09}
}
输出对角线以后全是0,而不是图中那样子的
![求c++输出矩阵,用二维数组和循环!](/uploads/image/z/2158074-18-4.jpg?t=%E6%B1%82c%2B%2B%E8%BE%93%E5%87%BA%E7%9F%A9%E9%98%B5%2C%E7%94%A8%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%92%8C%E5%BE%AA%E7%8E%AF%21)
#include
#include
using namespace std;
int i,j,m;
int main()
{
cin>>m;
for (i=1;i
#include
using namespace std;
int i,j,m;
int main()
{
cin>>m;
for (i=1;i