算法设计与分析- 分治策略实验.docVIP

  • 60
  • 0
  • 约 6页
  • 2016-04-19 发布于江西
  • 举报
算法设计与分析- 分治策略实验.doc

实验3 实验内容 1. 搜索。递归和递归算法实现搜索。一个查询的值所在位置,如果没有找到则返回-1 源代码(非递归算法): 源代码(递归算法): { if (lastfirst) index = -1; else { int mid=(first+last)/2; if(K== E[mid]) index = mid; else if(KE[mid]) index = binarySearch(E,first,mid-1,K); else index = binarySearch(E,mid+1,last,K); return index; } } 2. 棋盘覆盖问题编写一个程序,使用分治法实现棋盘覆盖问题,输入一个包含特殊方格的棋盘矩阵整型数组),输出覆盖之后的矩阵。 输入格式示例如下: -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 输出格式示例如下: -1 2 4 4 2 2 1 4 3 1 1 5 3 3 5 5 其中“-1”表示特殊方格。 源代码: 3. 归并排序和分治实现二路归并排序 源代码: template class T void MSort(T a[], int left, int right) { if (left right) { int center = (left + right) / 2; MS

文档评论(0)

1亿VIP精品文档

相关文档