算法导论(problems 6-3 Young tableaus)
来源:互联网 发布:巫师3和老滚5 知乎 编辑:程序博客网 时间:2024/06/05 14:51
Here is the solution for the problem.As for the (e) of this problem,the process is easy to implement.We just insert the orginal element into the tableaus and then extract the minimum.When the tableaus is empty,we could get the sorted lists.
#include<iostream>#include<vector>#include<string>#include<set>#include<map>#include<unordered_set>#include<unordered_map>#include<algorithm>#include<xfunctional>using namespace std;typedef struct tableau{int row;int col;int t_size;vector<vector<int>> elem;}tableau;pair<int,int> search(tableau& tab,int data){int i = tab.row - 1;int j = 0;pair<int, int> res;while (i >= 0 && j < tab.col){if (tab.elem[i][j] == data){res.first = i;res.second = j;return res;}else if (tab.elem[i][j]>data) i--;else j++;}res.first = -1;res.second = -1;return res;}void read_data(tableau tab){for (int i = 0; i < tab.row; i++){for (int j = 0; j < tab.col; j++){if (tab.elem[i][j] == INT_MAX){cout << "inf" << " ";}else cout << tab.elem[i][j] << " ";}cout << endl;}}int Extract_min(tableau &tab){if (tab.t_size == 0) return -1;tab.t_size--;int i = 0;int j = 0;int temp = tab.elem[i][j];while (i<tab.row-1&&j<tab.col-1){if (tab.elem[i + 1][j]<tab.elem[i][j + 1]){tab.elem[i][j] = tab.elem[i+1][j];i++;}else{tab.elem[i][j] = tab.elem[i][j+1];j++;}}if (i == tab.row - 1){while (j < tab.col - 1){tab.elem[i][j] = tab.elem[i][j+1];j++;}}if (j == tab.col - 1){while (i < tab.row - 1){tab.elem[i][j] = tab.elem[i+1][j];i++;}}tab.elem[i][j] = INT_MAX;return temp;}void insert(tableau& tab,int t){if (tab.t_size >= tab.col*tab.row) return;int i = tab.row - 1;int j = tab.col - 1;tab.elem[i][j] = t;while (i >0 && j >0){if (tab.elem[i - 1][j] > tab.elem[i][j - 1]){if (t <= tab.elem[i - 1][j]){ tab.elem[i][j] = tab.elem[i - 1][j];i--; }else break;}else{if (t <= tab.elem[i][j - 1]){tab.elem[i][j] = tab.elem[i][j-1];j--;}else break;}}if (i == 0){while (j > 0){if (t < tab.elem[i][j-1]){tab.elem[i][j] = tab.elem[i][j-1];j--;}else break;}}if (j == 0){while (i > 0){if (t < tab.elem[i-1][j]){tab.elem[i][j] = tab.elem[i-1][j];i--;}else break;}}tab.elem[i][j] = t;}int main(){tableau tab;cout << "Input the row and col of the matrix:";cin >> tab.row >> tab.col;tab.elem = vector<vector<int>>(tab.row, vector<int>(tab.col, INT_MAX));cout << "Input the amount of the data:";int amount;cin >> amount;tab.t_size = amount;vector<int> temp;for (int i = 0; i < amount; i++){int t;cin >> t;temp.push_back(t);}sort(temp.begin(),temp.end());int cur = 0;for (int i = 0; i < tab.row; i++){for (int j = 0; j < tab.col; j++){tab.elem[i][j] = temp[cur];cur++;if (cur == amount) break;}if (cur == amount) break;}cout << endl;cout << "before extract:" << endl;read_data(tab);cout << endl;int a = Extract_min(tab);cout << "after extract:" << endl;read_data(tab);cout << endl;cout << "after insert 100"<< endl;insert(tab,100);read_data(tab);cout << endl;cout << "Input the number you want to search:";int data;cin >> data;pair<int, int> res;res = search(tab,data);if (res.first == -1){cout << "Not found" << endl;}else{cout << "position: " << res.first << " " << res.second << endl;}system("pause");return 0;}
0 0
- 算法导论(problems 6-3 Young tableaus)
- 算法导论 6-3 Young tableaus 杨氏矩阵
- 6-3 Young tableaus
- young tableaus
- 算法导论 6-3(Young氏矩阵)
- 算法导论思考题:6-3Young 矩阵
- 算法导论 6-3 Young 氏矩阵
- 算法导论 6-3 Young氏矩阵
- 算法导论 6-3 Young氏矩阵
- 算法导论习题6—3 Young氏矩阵
- 算法导论思考题6-3:Young氏矩阵
- 算法导论习题 6-3 Young氏矩阵
- Young tableaus 的一种实现方法
- 《算法导论》 6.3 Young氏矩阵
- 算法导论(Problems 6-2) d-ary heap(多叉树实现堆)
- 《算法导论》第6章 堆排序 (4)Young氏矩阵
- 算法导论Problems 2-4: Inversions
- 据传是google的面试题 young tableaus
- Compile、Make和Build的区别(转载)
- PyCharm设置护眼背景色
- IntelliJ IDEA 导入 第三方jar包 图解
- startActivityForResult的使用
- Activity跳转切换动画
- 算法导论(problems 6-3 Young tableaus)
- 策略模式+工厂模式优化if...else if...else if结构
- Spring 3之MVC & Security简单整合开发(一)
- C#中DataSet类的使用
- poj 3624 01背包
- IIS 访问网站 错误消息 401.3
- APP切换语言
- android 软键盘显示与隐藏
- js断点调试心得