当前位置:网站首页>PTA 7-2 Summation and Counting of Diagonal Elements of Square Matrices Solution
PTA 7-2 Summation and Counting of Diagonal Elements of Square Matrices Solution
2022-08-10 10:03:00 【An Ran_】
For an input square matrix of
N*N
, find the sum of the elements on the two diagonals and the number of non-zero elements.input format:
Enter a positive integer
N
not exceeding 20 in the first line,
In the nextN
lines, enter theN
of each line of the square matrix in turn.code>N elements, the elements of the square matrix are integers whose absolute value does not exceed 1000, separated by spaces.Output format:
Output the sum of the elements on the two diagonals of the square matrix and the number of non-zero elements in the format
sum =
in one line, where, count =. and
represent the sum of
elements
andnumber of non-zero elements
respectively, and are replaced with the actual number when outputting.
Tips: (1) The intersecting elements of the two diagonals are not repeatedly calculated; (2) The output is strictly in the specified format, and characters such as spaces and line breaks are not allowed to be added or deleted at will.Input Sample 1:
41 2 3 41 2 3 40 1 2 30 1 2 3
Example output 1:
sum = 16, count = 7.
Input Sample 2:
51 2 3 2 11 2 3 2 11 2 3 2 11 2 3 2 11 2 3 2 1
Example output:
sum = 15, count = 9.
topic analysis
1. The title requires to calculate the sum of the main diagonal elements, and requires no repetition; requires to calculate the number of non-zero elements (on the diagonal)
2. Diagonal element coordinate feature - Main diagonal element-i=j——Subdiagonal element feature-i=n-1-j
3. Special requirements: Do not add or delete characters such as spaces, newlines, etc. at will; Diagonal cross-repetitive elements cannot be counted repeatedly
#include #define N 21int main(){int a[N][N];int n,i,j;int sum=0,count=0;scanf("%d",&n);for(i=0;i
边栏推荐
- ESP8266-Arduino编程实例-MQ-8氢气传感器驱动
- ZZULIOJ 1124: 两个有序数组合并
- [Internet of Things Architecture] The most suitable open source database for the Internet of Things
- 09 【Attributes继承 provide与inject】
- 【STL】位图的介绍使用以及代码的模拟实现
- 【数据架构】概念数据模型和逻辑数据模型有什么区别
- VBA: 遍历文件抓取指定条件的数据
- 91.(cesium之家)cesium火箭发射模拟
- JS高级 之 使用 Iterator - Generator
- 效率开发目录
猜你喜欢
随机推荐
多租户技术
LCD DRM驱动框架分析一
【数据仓库】什么是 Azure Synapse,它与 Azure Data Bricks 有何不同?
单例模式基类
shell iterates over folders and outputs
JS高级 之 Promise 详解
2022.8.7-----leetcode.636
LeetCode Algorithm 914. 卡牌分组
UE4 Sequence添加基础动画效果 (05-蓝图触发Sequence)
「敏捷建模」纪律:敏捷设计理念
效率开发目录
Shell脚本数组
LiveNVR操作日志页面快速筛选上级国标平台的调用记录直播观看录像回看等操作
"Microservice Architecture" Arrangement and Choreography - Different Models for Making Systems Work Together
【Software Exam System Architect】System Reliability Analysis and Design ① System Reliability Analysis
[Metaverse Omi Says] Listen to how Rabbit Fan Rabbit creates a new era of trendy play from virtual to reality
用高质量图像标注数据加速AI商业化落地
Plot temperature curves; QChart,
「应用架构」TOGAF建模:企业可管理性图
数据中台解决方案及未来发展方向