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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

基础数据结构【一】————数组

發(fā)布時(shí)間:2023/11/27 生活经验 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基础数据结构【一】————数组 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

二維數(shù)組相乘,矩陣相乘,

#include <iostream>
using namespace std;void MatrixMultiply(int*, int*, int*, int, int, int);
int main()
{int M, N, P;int i, j;//矩陣A部分 cout << "請(qǐng)輸入矩陣A的維數(shù)(M,N): " << endl;cout << "M= ";cin >> M;cout << "N= ";cin >> N;int* A = new int[M * N];cout << "[請(qǐng)輸入矩陣A的各個(gè)元素]" << endl;for (i = 0; i < M; i++)for (j = 0; j < N; j++){cout << "a" << i << j << "=";cin >> A[i * N + j];}//矩陣B部分 cout << "請(qǐng)輸入矩陣B的維數(shù)(N,P): " << endl;cout << "N= ";cin >> N;cout << "P= ";cin >> P;int* B = new int[N * P];cout << "[請(qǐng)輸入矩陣B的各個(gè)元素]" << endl;for (i = 0; i < N; i++)for (j = 0; j < P; j++){cout << "b" << i << j << "=";cin >> B[i * P + j];}int* C = new int[M * P];MatrixMultiply(A, B, C, M, N, P); //調(diào)用函數(shù) cout << "[AxB的結(jié)果是]" << endl;for (i = 0; i < M; i++){for (j = 0; j < P; j++)cout << C[i * P + j] << "\t";cout << endl;}system("pause");
}
//進(jìn)行矩陣相乘 
void MatrixMultiply(int* arrA, int* arrB, int* arrC, int M, int N, int P)
{if (M <= 0 || N <= 0 || P <= 0){cout << "[錯(cuò)誤:維數(shù)M,N,P必須大于0]" << endl;return;}for (int i = 0; i < M; i++)for (int j = 0; j < P; j++){int Temp;Temp = 0;for (int k = 0; k < N; k++)Temp = Temp + arrA[i * N + k] * arrB[k * P + j];arrC[i * P + j] = Temp;}
}

二維數(shù)組,矩陣相加,函數(shù)調(diào)用

#include <iostream>
using namespace std;const int  ROWS = 3;
const int COLS = 3;
void MatrixAdd(int*, int*, int*, int, int);   //函數(shù)原型
int main()
{int A[ROWS][COLS] = { {1,3,5},{7,9,11},{13,15,17} };int B[ROWS][COLS] = { {9,8,7},{6,5,4},{3,2,1} };int C[ROWS][COLS] = { 0 };cout << "[矩陣A的各個(gè)元素]" << endl;  //輸出矩陣A的內(nèi)容for (int i = 0; i < ROWS; i++){for (int j = 0; j < COLS; j++)cout << A[i][j] << "\t";cout << endl;}cout << "[矩陣B的各個(gè)元素]" << endl;	//輸出矩陣B的內(nèi)容for (int i = 0; i < ROWS; i++){for (int j = 0; j < COLS; j++)cout << B[i][j] << "\t";cout << endl;}MatrixAdd(&A[0][0], &B[0][0], &C[0][0], ROWS, COLS);cout << "[顯示矩陣A和矩陣B相加的結(jié)果]" << endl;	//輸出A+B的內(nèi)容for (int i = 0; i < ROWS; i++){for (int j = 0; j < COLS; j++)cout << C[i][j] << "\t";cout << endl;}system("pause");
}
void MatrixAdd(int* arrA, int* arrB, int* arrC, int dimX, int dimY)
{if (dimX <= 0 || dimY <= 0){cout << "矩陣維數(shù)必須大于0" << endl;return;}for (int row = 1; row <= dimX; row++)for (int col = 1; col <= dimY; col++)arrC[(row - 1) * dimY + (col - 1)] = arrA[(row - 1) * dimY + (col - 1)] + arrB[(row - 1) * dimY + (col - 1)];
}

數(shù)組壓縮降維

#include <iostream>
using namespace std;
#define ARRAY_SIZE 5  //矩陣的維數(shù) 
int getValue(int ,int);
int A[ARRAY_SIZE][ARRAY_SIZE]={ //上三角矩陣的內(nèi)容 {7, 8, 12, 21, 9}, {0, 5, 14,  17, 6}, {0, 0, 7, 23, 24}, {0, 0, 0,  32, 19}, {0, 0, 0,  0,  8}};  
//一維數(shù)組的數(shù)組聲明 
int B[ARRAY_SIZE*(1+ARRAY_SIZE)/2];  
int main()
{int i=0,j=0;int index;    cout<<"=========================================="<<endl;cout<<"上三角形矩陣:"<<endl;for ( i = 0 ; i < ARRAY_SIZE ; i++ ) {for ( j = 0 ; j < ARRAY_SIZE ; j++ ) cout<<"\t"<<A[i][j];cout<<endl;    }//將右上三角矩陣壓縮為一維數(shù)組 index=0;for ( i = 0 ; i < ARRAY_SIZE ; i++ ) {for ( j = 0 ; j < ARRAY_SIZE ; j++ ) {if(A[i][j]!=0) B[index++]=A[i][j];}}cout<<"=========================================="<<endl;cout<<"以一維數(shù)組的方式表示:"<<endl;cout<<"\t[";for ( i = 0 ; i < ARRAY_SIZE ; i++ ) {for ( j = i ; j < ARRAY_SIZE ; j++ ) cout<<" "<<getValue(i,j);}cout<<" ]";cout<<endl;    system("pause");
}
int getValue(int i, int j) {int index = ARRAY_SIZE*i - i*(i+1)/2 + j;return B[index];
}

稀疏矩陣,二維數(shù)組,內(nèi)存壓縮?

?

#include <iostream>
#include <ctime>
#include <cstdlib>using namespace std;
const int _ROWS = 8;		//定義行數(shù)
const int _COLS = 9;		//定義列數(shù)
const int _NOTZERO = 8;		//定義稀疏矩陣中不為0的個(gè)數(shù)int main ()
{  int i,j,tmpRW,tmpCL,tmpNZ;int temp=1;int Sparse[_ROWS][_COLS];		//聲明稀疏矩陣int Compress[_NOTZERO][3];		//聲明壓縮矩陣srand(time(NULL));for (i=0;i<_ROWS;i++)			//將稀疏矩陣的所有元素設(shè)為0for (j=0;j<_COLS;j++)Sparse[i][j]=0;tmpNZ=_NOTZERO;for (i=1;i<tmpNZ+1;i++){tmpRW = rand()%_ROWS;tmpCL = rand()%_COLS;if(Sparse[tmpRW][tmpCL]!=0)//避免同一個(gè)元素設(shè)定兩次數(shù)值而造成壓縮矩陣中有0tmpNZ++;Sparse[tmpRW][tmpCL]=i; //隨機(jī)產(chǎn)生稀疏矩陣中非零的元素值}cout<<"[稀疏矩陣的各個(gè)元素]"<<endl; //輸出稀疏矩陣的各個(gè)元素for (i=0;i<_ROWS;i++){  for (j=0;j<_COLS;j++)cout<<"["<<Sparse[i][j]<<"] ";cout<<endl;}//開始?jí)嚎s稀疏矩陣Compress[0][0] = _ROWS;Compress[0][1] = _COLS;Compress[0][2] = _NOTZERO;for (i=0;i<_ROWS;i++)for (j=0;j<_COLS;j++)if (Sparse[i][j] != 0){  Compress[temp][0]=i;Compress[temp][1]=j;Compress[temp][2]=Sparse[i][j];temp++;}cout<<"[稀疏矩陣壓縮后的內(nèi)容]"<<endl; //輸出壓縮矩陣的各個(gè)元素for (i=0;i<_NOTZERO+1;i++){  for (j=0;j<3;j++)cout<<"["<<Compress[i][j]<<"] ";cout<<endl;}system("pause");
}

?

指針數(shù)組與二維字符串?dāng)?shù)組 :

由以下運(yùn)行結(jié)果可知,指針數(shù)組內(nèi)存連續(xù)不存在內(nèi)存浪費(fèi)問題,char name1[4][10]數(shù)組缺點(diǎn)就是所有字符類型的內(nèi)存空間長(zhǎng)度都會(huì)開辟出來

#include <iostream>
#include <cstdlib>using namespace std;int main()
{const char* name[4] = { "J", "Mo", "Bec", "Bush" };	//一維指針數(shù)組 char name1[4][10] = { "J", "Mo", "Bec", "Bush" };//二維字符串?dāng)?shù)組  int i;cout << "---------- 一維指針數(shù)組的存儲(chǔ)方式 --------------" << endl;for (i = 0; i < 4; i++){cout << "name[" << i << "] = \"" << name[i] << "\"\t";cout << "所占地址:" << (int*)name[i] << endl; //輸出name[i]所占地址   }cout << "------------ 二維字符串?dāng)?shù)組的存儲(chǔ)方式--------------" << endl;for (i = 0; i < 4; i++){cout << "name1[" << i << "] = \"" << name1[i] << "\"\t";cout << "所占地址:" << (int*)name1[i] << endl; //輸出name1[i]所占地址  }cout << "------------ 數(shù)據(jù)類型長(zhǎng)度 --------------" << endl;cout << "char = " << sizeof(char) << endl;cout << "int = " << sizeof(int) << endl;cout << "double = " << sizeof(double) << endl;cout << "float = " << sizeof(float) << endl;cout << "uint8_t = " << sizeof(uint8_t) << endl;cout << "uint16_t = " << sizeof(uint16_t) << endl;cout << "size_t = " << sizeof(size_t) << endl;system("pause");return 0;
}

?win_x64數(shù)據(jù)類型? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?win_x86? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

一維數(shù)組:記錄5個(gè)學(xué)生分?jǐn)?shù),并且輸出(一維數(shù)組的構(gòu)建與訪問)

int main()
{int Score[5] = { 87,66,90,65,70 };          //定義整數(shù)數(shù)組Score[5], 并設(shè)置5個(gè)成績(jī) int count, Total_Score = 0;for (count = 0; count < 5; count++)	     	// 執(zhí)行 for 循環(huán)讀取學(xué)生成績(jī) {cout << "第" << count + 1 << "位學(xué)生的分?jǐn)?shù):" << Score[count] << endl;Total_Score += Score[count];	       	//由數(shù)組中讀取分?jǐn)?shù)并計(jì)算總分}cout << "-------------------------" << endl;cout << "5位學(xué)生的總分:" << Total_Score << endl;    //輸出成績(jī)總分system("pause");return 0;
}

二維數(shù)組:記錄3個(gè)銷售員半年的銷售成績(jī),并且求取每個(gè)銷售員的半年總成績(jī)與每個(gè)月3個(gè)銷售員的月總銷售成績(jī)(2維數(shù)組的操作與訪問)

#include <iostream>
using namespace std;int main()
{int i, j, sum, max = 0, no = 1;int sale[3][6] = { {112,76,95,120,98,68},{90,120,88,112,108,120},{108,99,126,90,76,98} };for (i = 0; i < 3; i++){sum = 0;for (j = 0; j < 6; j++)sum += sale[i][j];//加上每月的業(yè)績(jī)金額  cout << "銷售員" << i + 1 << "的前半年銷售總金額為 " << sum << endl;cout << "------------------------------------" << endl;}for (i = 0; i < 6; i++){sum = 0;for (j = 0; j < 3; j++)sum += sale[j][i];// 加上每月的業(yè)績(jī)金額  cout << "所有銷售員" << i + 1 << "月的銷售總金額為 " << sum << endl;cout << "=====================================" << endl;}system("pause");return 0;
}

二階行列式(二維數(shù)組)?運(yùn)算

#include <iostream>
using namespace std;
int main()
{int arr[2][2];int sum;cout<<"|a1 b1|"<<endl;cout<<"|a2 b2|"<<endl;cout<<"請(qǐng)輸入a1:";cin>>arr[0][0];cout<<"請(qǐng)輸入b1:";cin>>arr[0][1];cout<<"請(qǐng)輸入a2:";cin>>arr[1][0];cout<<"請(qǐng)輸入b2:";cin>>arr[1][1];sum = arr[0][0]*arr[1][1]-arr[0][1]*arr[1][0];//求二階行列式的值 cout<<"|"<<arr[0][0]<<" "<<arr[0][1]<<"|"<<endl;cout<<"|"<<arr[1][0]<<" "<<arr[1][1]<<"|"<<endl;cout<<"sum="<<sum<<endl;system("pause");return 0;
}

三維數(shù)組,及其遍歷(求取滿足一定條件下的操作,求和,尋找最小值)

#include <iostream>
using namespace std;int main()
{int i, j, k;int sum = 0;int arr[4][3][3] = { {{1,-2,3},  {4,5,-6},   {8,9,2}},{{7,-8,9},  {10,11,12}, {-1,3,2}},{{-13,14,15},{16,17,18},{3,6,7}},{{19,20,21},{-22,23,24},{-6,9,12}} };	//聲明并設(shè)置數(shù)組的元素值  for (i = 0; i < 4; i++){for (j = 0; j < 3; j++){for (k = 0; k < 3; k++){sum += arr[i][j][k];if (arr[i][j][k] < 0)arr[i][j][k] = 0;		// 元素值為負(fù)數(shù),則歸零cout << arr[i][j][k] << "\t";if (min >= arr[i][j][k])min = arr[i][j][k];	}cout << endl;}cout << endl;}cout << "---------------------------" << endl;cout << "原數(shù)組的所有元素值總和=" << sum << endl;cout << "---------------------------" << endl;system("pause");return 0;
}

結(jié)構(gòu)體數(shù)組:允許用戶自定義數(shù)據(jù)類型,成為派生數(shù)據(jù)類型,總而言之,就是可以將一個(gè)相關(guān)的擁有不同數(shù)據(jù)類型的數(shù)據(jù)組合在一起成為一種新的數(shù)據(jù)類型?// 可聲明 變量 數(shù)組 ?指針 甚至其他結(jié)構(gòu)成員等

#include <iostream>
int main()
{struct student{char name[10];int score[3];// 可聲明 變量 數(shù)組  指針 甚至其他結(jié)構(gòu)成員等};struct student group1[3] = {{"a",81,82,83},{"b",91,92,93},{"name",71,72,73}};for (int i = 0; i < 3; ++i) {std::cout << group1[i].name << " \t" <<"chinese_score = " << group1[i].score[0] << "\t" <<"math_score = " << group1[i].score[1] << "\t" <<"english_score = " << group1[i].score[2] << "\t" << std::endl;std::cout << "-------------------------------" << std::endl;}system("pause");return 0;
}

字符串?dāng)?shù)組:

#include <iostream>
#include <cstdlib>
#include <cstring>	
#include <cctype>		//包括此判斷函數(shù)文件using namespace std;int main()
{char Str[6][30] = { "張繼    楓橋夜泊",       // 聲明并初始化二維字符串?dāng)?shù)組"================",   // 省略了每個(gè)元素之間的大括號(hào)"月落烏啼霜滿天","江楓漁火對(duì)愁眠","姑蘇城外寒山寺","夜半鐘聲到客船" };int i;for (i = 0; i < 6; i++)cout << Str[i] << endl;                   // 輸出字符串?dāng)?shù)組的內(nèi)容int lower = 0;char string[40];cout << "請(qǐng)輸入字符串:";cin.getline(string, 40);			//輸入的字符串有40個(gè)字符 int len = strlen(string);for (int i = 0; i <= len; i++)if (islower(string[i]) != 0)		//是小寫字符則加1	lower++;cout << string << "字符串的小寫字符有 " << lower << "個(gè)" << endl;system("pause");return 0;
}

?

總結(jié)

以上是生活随笔為你收集整理的基础数据结构【一】————数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。