杨氏矩阵定义及其查找的实现C++
生活随笔
收集整理的這篇文章主要介紹了
杨氏矩阵定义及其查找的实现C++
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先介紹一下這個(gè)數(shù)據(jù)結(jié)構(gòu)的定義,Young Tableau有一個(gè)m*n的矩陣,然后有一數(shù)組?a[k],?其中?k<=m*n?,然后把a[k]中的數(shù)填入?m*n?的矩陣中,填充規(guī)則為:
1.??每一行每一列都嚴(yán)格單調(diào)遞增(有其他的版本是遞減,其原理相同)。
2.??如果將a[k]中的數(shù)填完后,矩陣中仍有空間,則填入?∞。
舉例:
這里主要給出楊氏矩陣的定義和查找
方法:理由每一列,沒一行都是遞增的,我們從左上角開始查找,不斷的縮小矩陣的大小,最后只剩一1*1的矩陣。
C++代碼:
1 #pragma once 2 //YoungTableau.h 3 #include <iostream> 4 class YoungTableau 5 { 6 public: 7 YoungTableau(int rows, int columns,int**newTable) 8 { 9 x = rows; //行數(shù) 10 y = columns; //列數(shù) 11 12 if (newTable!=NULL) 13 { 14 table = (int **)((new int[rows*columns])); 15 for (int i = 0; i < rows*columns; i++) 16 17 { 18 ((int*)table)[i] = ((int*)newTable)[i]; 19 std::cout << ((int*)table)[i] << std::endl; 20 } 21 22 } 23 24 }; 25 ~YoungTableau(); 26 private:int x; 27 private:int y; 28 private: int **table; 29 30 public: 31 void print_matrix(void) 32 { 33 std::cout <<x<<y << std::endl; 34 if (table != NULL) 35 { 36 for (int i = 0; i < x; i++) 37 for (int j = 0; j < y; j++) 38 std::cout << ((int*)table)[i*y+j]<<std:: endl; 39 } 40 } 41 public:int get_element(int rows, int columns,int*res) 42 { 43 if (res != NULL) 44 { 45 if (rows>=this->x || columns >= this->y) 46 { 47 *res = 0; 48 return -1; 49 } 50 else 51 { 52 *res = ((int*)table)[rows*y + columns]; 53 return 0; 54 } 55 } 56 else 57 { 58 *res = 0; 59 return -2; 60 } 61 } 62 private:bool IsEqualAtLeftTop(int data, int rownum, int columnnum) 63 { 64 if (this->table != NULL) 65 { 66 if (rownum >= x || columnnum >= y || (rownum == x - 1 && columnnum == 0 && data != ((int*)this->table)[rownum*y + columnnum])) 67 { 68 std::cout << "illgal column" << std::endl; 69 return false; 70 } 71 else if (data == ((int*)this->table)[rownum*y + columnnum]) 72 { 73 return true; 74 } 75 else if (data < ((int*)this->table)[rownum*y + columnnum]) 76 { 77 std::cout << "next column"<< std::endl; 78 if (columnnum == 0) 79 return false; 80 else 81 return IsEqualAtLeftTop(data, rownum, columnnum - 1); 82 } 83 else 84 { 85 std::cout << "next row" << std::endl; 86 if (rownum == x-1) 87 return false; 88 else 89 return IsEqualAtLeftTop(data, rownum+1, columnnum); 90 } 91 92 } 93 else 94 { 95 return false; 96 } 97 } 98 public:bool IsExistence(int data) 99 { 100 if (this->table != NULL) 101 { 102 /*for (int i = 0; i < x; i++) 103 for (int j = 0; j < y; j++) 104 { 105 if (data == ((int*)table)[i*y + j]) 106 return true; 107 } 108 return false;*/ 109 return IsEqualAtLeftTop(data, 0, y - 1); 110 } 111 return false; 112 } 113 114 }; 1 // Young_Tableau.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。 2 // 3 4 #include "stdafx.h" 5 #include "YoungTableau.h" 6 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 int tmp_table[3][3] = 10 { 11 {1, 3, 4 }, 12 {2, 3, 6}, 13 {5, 8, 10} 14 }; 15 YoungTableau matrix(3,3,(int**)tmp_table); 16 matrix.print_matrix(); 17 int tmp = 0; 18 matrix.get_element(2,2,&tmp); 19 std::cout << tmp << std::endl; 20 21 if (matrix.IsExistence(5)==true) 22 std::cout <<"true"<< std::endl; 23 else std::cout << "false" << std::endl; 24 while (1); 25 return 0; 26 }?
1 #include "stdafx.h" 2 #include "YoungTableau.h" 3 // YoungTableau.cpp : 類的實(shí)現(xiàn) 4 // 5 6 7 YoungTableau::~YoungTableau() 8 { 9 10 }?
轉(zhuǎn)載于:https://www.cnblogs.com/guiguzhixing/p/5862808.html
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的杨氏矩阵定义及其查找的实现C++的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 712C. Mem
- 下一篇: 118. Pascal's Triang