题目描述

模仿例题,打印出不同方向的正方形,然后打印三角形矩阵。中间有个空行。

输入格式

输入矩阵的规模,不超过 99。

输出格式

输出矩形和正方形

输入输出样例

输入 #1复制

4

输出 #1复制

01020304
05060708
09101112
13141516

      01
    0203
  040506
07080910

这个偏右的下三角很有意思,就等于把上面的三角输出空格即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			a++;
			if(a<10){
				cout<<'0'<<a;
			}
			else cout<<a;
		}
		cout<<endl;
	}
	cout<<endl;
	int b=0;
	for(int i=0;i<n;i++){
		for (int j = 0; j <= n-i-2; j++){//可以仔细琢磨一下j的取值
			printf("  ");
		}
		
		for (int j = 0; j < i + 1; j++)
		{
			b++;
			if(b<10){
				cout<<'0'<<b;
			}
			else cout<<b;
		}
		cout<<endl;
	}	
	return 0;
} 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐