C++矩阵

C++矩阵【基本】(will循环)

cpp 复制代码
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int a[100][100]={0};
	int k = 1;
	int i = 0;
	int j = 0;
	while(k<=100)
	{
		if(j>=10)
		{
			j=0;
			i++;
		}
		a[i][j]=k;
		j++;
		k++;
	}
	i = 0;
	j = 0;
	while(true)
	{
		if(i == 9&&j==10)break;
		if(j>=10)
		{
			j = 0;
			i++;
			cout<<endl;
		}
			
		cout<<a[i][j]<<' ';
		j++;
	}
	return 0;
}

正对角线(for循环)

cpp 复制代码
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int a[100][100]={0};
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(i == j)
			{
				cout<<a[i][j]<<' ';
			}
		}
	}
	return 0;
}

反对角线(for循环)

cpp 复制代码
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int a[100][100]={0};
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(i+j==n-1)
			{
				cout<<a[i][j]<<' ';
			}
		}
	}
	return 0;
}
相关推荐
夜天炫安全10 小时前
数据结构中所需的C语言基础
c语言·数据结构·算法
2301_7890156211 小时前
DS进阶:AVL树
开发语言·数据结构·c++·算法
zyq99101_114 小时前
优化二分查找:前缀和降复杂度
数据结构·python·蓝桥杯
qyzm14 小时前
天梯赛练习(3月13日)
开发语言·数据结构·python·算法·贪心算法
逆境不可逃15 小时前
LeetCode 热题 100 之 64. 最小路径和 5. 最长回文子串 1143. 最长公共子序列 72. 编辑距离
算法·leetcode·动态规划
CoderCodingNo15 小时前
【GESP】C++五级练习题 luogu-P1182 数列分段 Section II
开发语言·c++·算法
放下华子我只抽RuiKe515 小时前
机器学习全景指南-直觉篇——基于距离的 K-近邻 (KNN) 算法
人工智能·gpt·算法·机器学习·语言模型·chatgpt·ai编程
kisshuan1239615 小时前
[特殊字符]【深度学习】DA3METRIC-LARGE单目深度估计算法详解
人工智能·深度学习·算法
sali-tec15 小时前
C# 基于OpenCv的视觉工作流-章33-Blod分析
图像处理·人工智能·opencv·算法·计算机视觉
Qt学视觉16 小时前
AI2-Paddle环境搭建
c++·人工智能·python·opencv·paddle