日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@value 数组_数据结构与算法:12 数组与稀疏矩阵

發布時間:2024/2/28 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @value 数组_数据结构与算法:12 数组与稀疏矩阵 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

12 數組與稀疏矩陣

知識結構:

圖1 知識結構

1. 數組

1.1 數組的定義

數組是具有一定順序關系的若干對象組成的集合,組成數組的對象稱為數組元素。

例如:

  • 向量對應一維數組
  • 矩陣對應二維數組

數組名表示群體的共性,即具有同一種數據類型;

下標表示個體的個性,即各自占有獨立的單元。

1.2 數組的存儲

(1)維數組的定義

下標由個數組成的數組稱為維數組。

例如:

//一維數組(線)
int[] a = new int[10];

//二維數組 (面)
int[ , ] a = new int[2,3];

//三維數組 (體),類比:書(體)【2.頁碼 3.行 4.列】
int[ , , ] a = new int[2,3,4];

(2)數組存儲的特點

  • 數組元素在內存中按順序連續存儲。
  • 數組的存儲分配按照行(C、C++、C#等)或列(Forturn等)進行。
  • 數組名表示該數組的首地址,是常量。

(3)常用數組的存儲

一維數組a[n]

各元素按下角標依次存放。

例:int[] a = new int[5];

圖2 一維數組存儲

二維數組a[m,n]

例:int[ , ] a = new int[2,3];

圖3 二維數組存儲

三維數組a[m,n,l]

第一維下標變化最慢,第三維(最后一維)下標變化最快。

例:int[ , , ] a = new int[2,3,4];

圖4 三維數組的存儲

注意:

C#中int[,]與int[][]定義數組的區別

  • int[,] 行數,列數確定
static void Main(string[] args){

int[,] Arr = new int[2, 5] { { 1, 2, 3, 5, 6 }, { 1, 2, 3, 4, 5 } };

for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 5; ++j)
{
Console.Write(Convert.ToString(Arr[i, j]) + " ");
}
Console.WriteLine();
}
// 1 2 3 5 6
// 1 2 3 4 5
}
  • int[][] 行數確定,列數不定
static void Main(string[] args){

int[][] Arr = new int[3][]; //表示含有三個一維數組的數組
Arr[0] = new int[5] { 1, 2, 3, 4, 5 };
Arr[1] = new int[2] { 0, 1 };
Arr[2] = new int[0] { };

for (int i = 0; i < Arr.Length; ++i)
{
foreach (int j in Arr[i])
{
Console.Write(j + " ");
}
Console.WriteLine();
}
// 1 2 3 4 5
// 0 1
//
}

1.3 數組的分類

數組可分為靜態數組和動態數組兩類。

(1)靜態數組

在程序編譯時分配空間的數組。

例:

//靜態數組(聲明之后數組長度不可改變)
int[] a = new int[10];

(2)動態數組

在程序運行時分配空間的數組(聲明之后數組長度可根據問題而調整)。

圖5 動態數組類圖using System;

namespace LinearStruct
{
///
/// 動態數組的抽象數據類型實現
///
/// 動態數組中元素的類型
public class DArraywhere T : IComparable
{private T[] _array;/// /// 獲取動態數組的當前長度/// public int Size { get; private set; }/// /// 初始化DArray類的新實例/// /// 動態數組的初始長度public DArray(int size){if (size <= 0)throw new ArgumentOutOfRangeException();
Size = size;
_array = new T[Size];
}/// /// 改變動態數組的長度/// /// 動態數組的新長度public void ReSize(int newSize){if (newSize <= 0)throw new ArgumentOutOfRangeException();if (Size != newSize)
{
T[] newArray = new T[newSize];int n = newSize < Size ? newSize : Size;for (int i = 0; i < n; i++)
{
newArray[i] = _array[i];
}
_array = newArray;
Size = newSize;
}
}/// /// 獲取或設置指定索引處的元素/// /// 要獲得或設置的元素從零開始的索引/// 指定索引處的元素public T this[int index]
{
get
{if (index < 0 || index > Size - 1)throw new IndexOutOfRangeException();return _array[index];
}set
{if (index < 0 || index > Size - 1)throw new IndexOutOfRangeException();
_array[index] = value;
}
}
}
}

1.4 動態數組的應用

編寫一段代碼,要求輸入一個整數N,用動態數組A來存放2~N之間所有5或7的倍數,輸出該數組。

例如:N=100

則輸出:5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 55 56 60 63 65 70 75 77 80 84 85 90 91 95 98 100

參考界面如下:

圖6 動態數組實驗界面static void Main(string[] args){
Console.WriteLine("N=");
string s = Console.ReadLine();
int n;
if (int.TryParse(s, out n))
{
DArray<int> arr = new DArray<int>(10);
int j = 0;
for (int i = 2; i <= n; i++)
{
if (i % 5 == 0 || i % 7 == 0)
{
if (j == arr.Size)
arr.ReSize(arr.Size + 10);

arr[j] = i;
j++;
}
}
for (int i = 0; i < j; i++)
{
Console.Write(arr[i] + " ");
}
}
}

2. 稀疏矩陣

2.1 稀疏矩陣的定義與操作

(1)稀疏矩陣的定義

若矩陣中非零元素的個數遠遠小于零元素的個數,則稱為稀疏矩陣。

例:

若用二維數組存儲,則太浪費存儲空間。

(2)稀疏矩陣的操作

  • 獲取矩陣的行數
  • 獲取矩陣的列數
  • 獲取或設置指定索引處的元素
  • 矩陣加法
  • 矩陣轉置
  • 矩陣乘法
圖7 矩陣接口namespace LinearStruct
{
///
/// 矩陣的抽象數據類型
///
public interface IMatrix
{/// /// 獲取矩陣的行數/// int Rows { get; }/// /// 獲取矩陣的列數/// int Cols { get; }/// /// 獲取或設置指定索引處的元素/// /// 要獲取或設置的元素從零開始的行索引/// 要獲取或設置的元素從零開始的列索引///
T this[int i, int j] { get; set; }/// /// 矩陣加法/// /// 與之相加的另一個矩陣/// 相加后的新矩陣
IMatrix Add(IMatrix b);/// /// 矩陣轉置/// /// 轉置后的新矩陣
IMatrix Transpose();/// /// 矩陣乘法/// /// 與之右乘的另一個矩陣/// 相乘后的新矩陣
IMatrix Multiply(IMatrix b);
}
}

2.2 稀疏矩陣的存儲與實現

可以利用(Row,Col,Value)的方式存儲和定位稀疏矩陣中的非零元素,這種存儲稀疏矩陣結點的方式稱為三元組(Triple)。

圖8 稀疏矩陣結點的存儲

利用順序的方式進行存儲:

圖9 順序方式存儲稀疏矩陣

利用鏈式的方式進行存儲:

圖10 鏈式方式存儲稀疏矩陣

(1)對結點的封裝

圖11 稀疏矩陣結點類圖using System;

namespace LinearStruct
{
///
/// 稀疏矩陣結點
///
public class Triple : IComparable
{/// /// 獲取結點所在矩陣中的行索引/// public int Row { get; }/// /// 獲取結點所在矩陣中的列索引/// public int Col { get; }/// /// 獲取或設置結點在矩陣中的值/// public double Value { get; set; }/// /// 初始化Triple類的新實例/// /// 結點所在矩陣中的行索引/// 結點所在矩陣中的列索引/// 結點在矩陣中的值public Triple(int i, int j, double value){if (i < 0 || j < 0)throw new Exception("數組元素位置無效.");
Row = i;
Col = j;
Value = value;
}/// /// Triple類的輸出字符串/// /// Triple類的輸出字符串public override string ToString(){return string.Format("({0},{1},{2})", Row, Col, Value);
}/// /// 比較三元組中數據的大小/// /// 被比較的三元組/// public int CompareTo(Triple other){if (Value < other.Value) return -1;if (Value > other.Value) return 1;return 0;
}
}
}

(2)對稀疏矩陣的封裝

圖12 稀疏矩陣類圖using System;

namespace LinearStruct
{
///
/// 矩陣抽象數據類型的實現--稀疏矩陣
///
public class SparseMatrix : IMatrix<double>
{
private readonly DLinkList _lst;/// /// 獲取稀疏矩陣的行數/// public int Rows { get; }/// /// 獲取稀疏矩陣的列數/// public int Cols { get; }/// /// 初始化SparseMatrix類的新實例/// /// 稀疏矩陣的行數/// 稀疏矩陣的列數public SparseMatrix(int rows, int cols){if (rows <= 0 || cols <= 0)throw new ArgumentOutOfRangeException();
Rows = rows;
Cols = cols;
_lst = new DLinkList();
}private DNode GetIndex(int i, int j)
{
DNode temp = _lst.PHead;while (temp != null)
{if (temp.Data.Row == i && temp.Data.Col == j)break;
temp = temp.Next;
}return temp;
}private void RemoveNode(DNode node){if (node.Next == null)
{
_lst.Remove(_lst.Length - 1);
}else if (node.Prior == null)
{
_lst.Remove(0);
}else
{
node.Prior.Next = node.Next;
node.Next.Prior = node.Prior;
}
}/// /// 獲取或設置指定索引處的元素/// /// 要獲取或設置的元素從零開始的行索引/// 要獲取或設置的元素從零開始的列索引/// public double this[int i, int j]
{
get
{if (i < 0 || i > Rows - 1 || j < 0 || j > Cols - 1)throw new IndexOutOfRangeException();
DNode node = GetIndex(i, j);return node == null ? 0.0 : node.Data.Value;
}set
{if (i < 0 || i > Rows - 1 || j < 0 || j > Cols - 1)throw new IndexOutOfRangeException();
DNode node = GetIndex(i, j);if (node == null)
{if (value != 0.0)
{
_lst.InsertAtRear(new Triple(i, j, value));
}
}else
{if (value != 0.0)
{
node.Data.Value = value;
}else
{
RemoveNode(node);
}
}
}
}/// /// 矩陣加法/// /// 與之相加的另一個矩陣/// 相加后的新矩陣public SparseMatrix Add(SparseMatrix b){if (b == null)throw new ArgumentNullException();if (b.Rows != Rows || b.Cols != Cols)throw new Exception("兩矩陣不能相加.");
SparseMatrix temp = new SparseMatrix(Rows, Cols);for (int i = 0; i < Rows; i++)for (int j = 0; j < Cols; j++)
temp[i, j] = this[i, j] + b[i, j];return temp;
}/// /// 矩陣轉置/// /// 轉置后的新矩陣public SparseMatrix Transpose(){
SparseMatrix temp = new SparseMatrix(Cols, Rows);for (int i = 0; i < temp.Rows; i++)for (int j = 0; j < temp.Cols; j++)
temp[i, j] = this[j, i];return temp;
}/// /// 矩陣乘法/// /// 與之右乘的另一個矩陣/// 相乘后的新矩陣public SparseMatrix Multiply(SparseMatrix b){if (b == null)throw new ArgumentNullException();if (Cols != b.Rows)throw new Exception("兩矩陣不能相乘.");
SparseMatrix temp = new SparseMatrix(Rows, b.Cols);for (int i = 0; i < Rows; i++)
{for (int j = 0; j < b.Cols; j++)
{double value = 0.0;for (int k = 0; k < Cols; k++)
value += this[i, k] * b[k, j];
temp[i, j] = value;
}
}return temp;
}/// /// 矩陣加法運算/// /// 第一個矩陣/// 第二個矩陣/// 相加后的新矩陣public static SparseMatrix operator +(SparseMatrix a, SparseMatrix b)
{if (a == null || b == null)throw new ArgumentNullException();return a.Add(b);
}/// /// 矩陣乘法運算/// /// 左邊的矩陣/// 右邊的矩陣/// 相乘后的新矩陣public static SparseMatrix operator *(SparseMatrix a, SparseMatrix b)
{if (a == null || b == null)throw new ArgumentNullException();return a.Multiply(b);
}/// /// SparseMatrix類的輸出字符串/// /// SparseMatrix類的輸出字符串public override string ToString(){string str = string.Empty;
DNode temp = _lst.PHead;while (temp != null)
{
str += temp.Data + "\n";
temp = temp.Next;
}return str;
}
IMatrix<double> IMatrix<double>.Add(IMatrix<double> b)
{return Add((SparseMatrix)b);
}
IMatrix<double> IMatrix<double>.Transpose()
{return Transpose();
}
IMatrix<double> IMatrix<double>.Multiply(IMatrix<double> b)
{return Multiply((SparseMatrix)b);
}
}
}

舉例:

static void Main(string[] args){
IMatrix<double> a = new SparseMatrix(2, 3);
IMatrix<double> b = new SparseMatrix(3, 2);
SparseMatrix c = new SparseMatrix(2, 2);
a[0, 2] = 1;
a[1, 0] = 1;
b[1, 1] = 4;
b[2, 0] = 1;
c[0, 1] = 1;
c[1, 0] = 1;
SparseMatrix d = (SparseMatrix)a * (SparseMatrix)b + c;
IMatrix<double> e = a.Multiply(b).Add(c);
Console.WriteLine("D:\n{0}", d);
Console.WriteLine("E:\n{0}", e);

// D:
// (0, 0, 1)
// (0, 1, 1)
// (1, 0, 1)
// E:
// (0, 0, 1)
// (0, 1, 1)
// (1, 0, 1)
}

總結

以上是生活随笔為你收集整理的@value 数组_数据结构与算法:12 数组与稀疏矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。