循环不变式
来源:互联网 发布:鞋店需要软件吗 编辑:程序博客网 时间:2024/05/22 00:18
定义:如果某个命题初始为真,并且每次更改后仍然保持该命题为真,则若干次更改后该命题仍然为真;
应用:
假如有三个变量:begin、current、end,将一个数组分成四个区域:
[0,begin):所有数据假设都是1
[beigin,current):所有数据假设都是2
(end,length-1]:所有数据假设都是3
[current,end):未知数据
利用循环不变式:
初始值为:begin=current=0,end=length-1,此时前三个区间都为空集,满足条件;
遍历current,根据current处的元素值做相应的处理,直到区间[current,end)为空,即current=end时循环退出;
上述问题可以延伸为同颜色小球排列的问题:
现有红,白,蓝三种颜色的小球若干,彼此之间混乱排列,先要求将这些小球,按照同颜色排列在一起的规则将它们重新排列;
进而延伸为荷兰国旗问题;
//improvement1void AllAlgorithms::hollandNationalFlag2(int *a, int length){ int begin = 0; int end = length - 1; int curr = 0; while (curr <= end) {//until the end is recolosing to the curr if (1 == a[curr]) { curr ++; } else if (2 == a[curr]){ swap(a[curr], a[end]); end --; } else{ if (curr != begin) swap( a[begin], a[curr]); begin ++; curr ++;//because when begin is not equal to the curr,the begin must be equal to 1,after swapping,both begin and curr are equal to 1,hence,curr ++ } }}
1 1
- 循环不变式
- 循环不变式
- 循环不变式
- 循环不变式
- 循环不变式
- 循环不变式
- 循环不变式
- 【循环不变式】【a^n】
- 循环不变式的理解
- 循环不变式的特征
- 循环不变式的理解
- 循环不变式的思想及其应用
- 循环不变式的思想及其应用
- 循环不变式的思想及其应用
- 循环不变式的思想及其应用
- 关于循环不变式的思考
- 循环不变式(Loop invariant)
- 对于循环不变式证明的总结
- CSS:display基本介绍
- centos 7.0 环境 php-fpm nginx
- UITableViewDataSource 和 UITableViewDelegate协议中常用方法
- <hdoj2544>最短路
- linux挂载usb
- 循环不变式
- 使用RequestDispatcher跳转页面后,JS与CSS样式都不见了的解决方法
- 电子商务软件nopCommerce3.80正式发布
- Android利用SpannableString实现简单的富文本需求。
- MappedByteBuffer以及ByteBufer的底层原理
- HDU 1869 六度分离 (最短路径---floyd)
- 多版本Qt程序的编译注意问题
- Google的Tesorflow成长迅速
- ActiveMQ5.13 安装与配置