问题描述:给定一个由0或1组成的数组,判断该数组:
1)如果全为1,且大小为偶数,则返回0;
2)如果全为1,且大小为奇数,则返回1;
3)如果不全为1,则返回-1;
算法:
proc f(A,low,high) {
if low<high then
mid = floor((low+high)/2);
left = f(A,low,mid-1);
right = f(A,mid+1,high);
if A(mid)=0 then
return -1;
else
if left = -1 or right = -1 then return -1;
if left = 1 and right = 1 then retrun 1;
if left = 1 and right = 0 then return 0;
if left = 0 and right = 1 then return 0;
if left = 0 and right = 0 then return 1;
endif
endif
}