当前位置:网站首页>ACM. HJ35 蛇形矩阵 ●
ACM. HJ35 蛇形矩阵 ●
2022-06-21 19:45:00 【chenyfan_】
HJ35 蛇形矩阵 ●
描述
蛇形矩阵是由 1 开始的自然数依次排列成的一个矩阵上三角形。
例如,当输入5时,应该输出的三角形为:
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
输入描述:
输入正整数N(N不大于100)
输出描述:
输出一个N行的蛇形矩阵。
示例
输入:
4
输出:
1 3 6 10
2 5 9
4 8
7
题解
1. 数学
根据数学规律,一行一行的计算并输出。

#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
int matrix[n][n];
for(int i = 0; i < n; ++i){
int num = i+2;
matrix[i][0] = (i+1)*i/2 + 1; // 第一列
cout << matrix[i][0] << " ";
for(int j = 1; j < n-i; ++j){
// 某一行
matrix[i][j] = matrix[i][j-1] + num;
++num; // 加数
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
2. 模拟
模拟蛇形矩阵的生成方法。

#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
int matrix[n][n];
int num = 1;
for(int i = 1; i <= n; ++i){
// 蛇形矩阵生成
int row = i - 1;
int col = 0;
for(int j = 1; j <= i; ++j){
matrix[row--][col++] = num++;
}
}
for(int i = 0; i < n; ++i){
// 输出
for(int j = 0; j < n-i; ++j){
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
边栏推荐
- Class loading process of JVM
- libtorch显存管理示例
- Xshell7+Xftp7免费版下载
- Cluster I -- LVS load balancing cluster NAT mode and LVS load balancing actual deployment
- 数据库管理:Navicat Premium 15
- Data visualization tool software
- The latest simulation test questions and answers of Henan construction electrician (special construction operation) in 2022
- Implementation principle of global load balancing
- evaluating expression ‘ew. sqlSegment != null and ew. sqlSegment != ‘‘ and ew. Mybats plus problems
- 拖延患者自救指南|“我有不拖延的100种借口,却不愿意跨出一步”
猜你喜欢

MySQL learning (from getting started to mastering 1.2)
![[parallel and distributed computing] 10B_ MapReduce GFS Implementation](/img/f9/3ce3c129d08f4e291f87217aae8fe2.png)
[parallel and distributed computing] 10B_ MapReduce GFS Implementation

Unity 模拟手电筒光源探测器,AI攻击范围检测区域,视锥内检测物体,扇形区域检测,圆形区域检测,圆锥区域检测

2022年最新河南建筑施工电工(建筑特种作业)模拟试题及答案

【小程序】通过request实现小程序与后台asp.net的数据json传输(Post协议 图文+代码)

The final scheme of adding traceid at the C end

AXI_Bus_Matrix_4x4 设计 - 逻辑设计部分

Citus 11 for Postgres 完全开源,可从任何节点查询(Citus 官方博客)

Caricature scientifique | Vous pouvez apprendre l'EEG en regardant les images. Voulez - vous essayer?

集群一---LVS負載均衡集群NAT模式及LVS負載均衡實戰部署
随机推荐
AXI_Bus_Matrix_4x4 设计 - 逻辑设计部分
2022年最新河南建筑施工电工(建筑特种作业)模拟试题及答案
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
十一、美化界面
【CTF】攻防世界 MISC
【微服务七】Ribbon负载均衡策略之BestAvailableRule源码深度剖析
合并两个有序数组
微信小程序js把数字转化成字母
Unity 模拟手电筒光源探测器,AI攻击范围检测区域,视锥内检测物体,扇形区域检测,圆形区域检测,圆锥区域检测
有哪些新手程序员不知道的小技巧?
[Patents and papers-20]: Operation Guide for electronic information declaration in Nanjing, Jiangsu Province in 2022
ARP协议及ARP攻击
Leecode198 looting
Merge two ordered arrays
修改UE4缓存路径,缓解c盘压力
Cluster I - LVS Load Balancing Cluster Nat Mode and LVS Load Balancing Field Deployment
Intersection of vector and plane
30组户外旅行游玩VLOG记录LUTs调色预设Moody Travel LUTs
数据可视化工具软件
Vscode有什么好用的插件?