编程珠玑第四章

1.略
2.我们可以先用原来的二分搜索来寻找这个元素。如果他前一个与其值相等,那就使用前一个的下标;然后再考虑前一个的前一个,以此类推。**

package chapter4;

import java.util.Arrays;
import java.util.Random;

//二分搜素
public class BinarySearch{
    public static int binarySearch(int array[],int number){
        Arrays.sort(array);
        int resultIndex=-1;//返回number在array中的下标,不在就返回0
        if(number<array[0]||number>array[array.length-1]||array.length<=0){
            resultIndex=-1;
            return resultIndex;
        }
        else{
            int low=0,high=array.length-1;
            int middle;
            while (low<=high) {
                middle=(low+high)/2;
                if(array[middle]<number)
                    low=middle+1;
                if(array[middle]==number)
                {
                    resultIndex=middle;
                    if(middle>=1){
                        int temp=middle-1;
                        while(temp>=0&&array[resultIndex]==array[temp]){
                            resultIndex=temp--;
                        }
                    }
                    //                  resultIndex=middle;//这是原来的找到一个就返回出去
                    return resultIndex;
                }
                if(array[middle]>number)
                    high=middle-1;
            }
        }
        return resultIndex;

    }
    public static void main(String args[]){
        //      int array[]=new int[100];
        //      Random random=new Random();
        //
        //      for(int i=0;i<array.length;i++){
        //          array[i]=random.nextInt(array.length);
        //      }
        //      int number=random.nextInt(array.length);
        int array[]={2,3,4,3,3 ,5,6,6,7,6};
        int number=3;
        int resultIndex=binarySearch(array, number);
        if(resultIndex==-1){
            System.out.println(number+" doesn't exsit in array"+Arrays.toString(array));
        }
        else
        {
            System.out.println(number+" exists in array["+resultIndex+"]="+array[resultIndex]);
            System.out.println(Arrays.toString(array));
        }
    }
}

运行结果

3 exists in array[1]=3
[2, 3, 3, 3, 4, 5, 6, 6, 6, 7]

3.二分搜素的递归实现。

package chapter4;

import java.util.Arrays;

public class t3 {
    public static int recursiveBinarySearch(int array[],int number,int low,int high){
        if(low>high){
            return -1;
        }
        int middle=(low+high)/2;
        if(array[middle]<number)
        {   low=middle+1;
            return recursiveBinarySearch(array, number, low, high);
        }
        else if(array[middle]==number){
            return middle;
        }
        else{
            high=middle-1;
            return recursiveBinarySearch(array, number, low, high);
        }

    }
    public static void main(String []args){
        int array[]={1,1,2,3,4,3,3 ,5,6,6,7,6};
        int number=3;
        Arrays.sort(array);
        int resultIndex=recursiveBinarySearch(array, number, 0, array.length-1);
        if(resultIndex==-1){
            System.out.println(number+" doesn't exsit in array"+Arrays.toString(array));
        }
        else
        {
            System.out.println(number+" exists in array["+resultIndex+"]="+array[resultIndex]);
            System.out.println(Arrays.toString(array));
        }
    }

}

运行结果

3 exists in array[5]=3
[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7]

4.获取程序运行的时间差就好了
5.说实话不会证,看来是拿不到附近的博士学士了,哈哈哈
书后给的资料参考还没有看。。。。
**6.分析:色同,两颗都扔掉,再放一颗黑的–豆子少了一个
色异,白的放回,黑的扔掉–豆子还是少了一个
所以不管怎样当豆子数大于二时,每次咖啡罐里的豆子都会减少一个,因而最终肯定满足终止条件—豆子数为1。**
又因为白色每次可能减少0或2个,所以其奇偶性不变,所以要想最后结果是白色的,那么原来白色必须要有奇个才行。相反地要是白色为奇数个,必定是它剩下。
这道题,感觉就是要综合考虑最后导致的结果,不能停留程序的执行过程,换个思维方式吧
7.说一下我的想法,对给定的(x,y)当x确定的时候,得到所有直线的纵坐标值,然后将其排序,然后从纵坐标中找到比给定y小的当中最大的和比给定y大的当中最小的。当然为了方便找到对应直线的a,b值就用了一个类。

package chapter4;

import java.math.BigDecimal;
import java.util.Arrays;

class IndexValue implements Comparable<IndexValue>{
    public int index;
    public double value;

    @Override
    public int compareTo(IndexValue other) {
        // TODO Auto-generated method stub
        BigDecimal thisBigDecimal=new BigDecimal(this.value);
        BigDecimal otherDecimal=new BigDecimal(other.value);
        return thisBigDecimal.compareTo(otherDecimal);
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return value+" ";
    }
}
public class t7 {
    public static void getWrappedLines(double[] a,double []b,double x,double y){
        if(a.length!=b.length){
            System.out.println("a 数组长度必须和b数组长度相同");
        }
        IndexValue yValues[]=new IndexValue[a.length+1];
        for (int i=0;i<yValues.length-1;i++){
            yValues[i]=new IndexValue();
            yValues[i].value=a[i]*x+b[i];
            yValues[i].index=i;
        }
        yValues[yValues.length-1]=new IndexValue();
        //将要比较的y值也加入数组中,方便使用二分搜索
        yValues[yValues.length-1].value=y;
        yValues[yValues.length-1].index=yValues.length;
        Arrays.sort(yValues);
        System.out.println(Arrays.toString(yValues));
        int resultIndex=binarySearch(yValues, y);

        System.out.println(resultIndex);
        if(1<=resultIndex&&resultIndex<=a.length-2){
            int upIndex=yValues[resultIndex+1].index;
            int bottomeIndex=yValues[resultIndex-1].index;
            System.out.println("点("+x+" , "+y+")"+"在直线y="+a[upIndex]+"x+"+b[upIndex]+"和直线y="+a[bottomeIndex]+"x+"+b[bottomeIndex]+"之间");
        }
    }
    public static int binarySearch(IndexValue array[],double number){
        //      Arrays.sort(array);
        int resultIndex=-1;//返回number在array中的下标,不在就返回0

        if(number<array[0].value||number>array[array.length-1].value||array.length<=0){
            resultIndex=-1;
            return resultIndex;
        }
        else{
            //          System.out.println("O");
            BigDecimal numberDecimal=new BigDecimal(number);
            int low=0,high=array.length-1;
            int middle;
            BigDecimal middleDecimal;
            while (low<=high) {
                middle=(low+high)/2;
//              System.out.println("middle="+middle);
                middleDecimal=new BigDecimal(array[middle].value);
                if(middleDecimal.compareTo(numberDecimal)==-1)
                    low=middle+1;
                if(middleDecimal.compareTo(numberDecimal)==0)
                {
                    resultIndex=middle;
                    return resultIndex;
                }
                if(middleDecimal.compareTo(numberDecimal)==1)
                    high=middle-1;
            }
        }
        return resultIndex;
    }
    public static void main(String []args){
        double a[]=new double[10];
        double b[]=new double[10];
        double temp=0.1;
        for(int i=0;i<10;i++){
            a[i]=temp;
            b[i]=1-temp;
            temp+=.1;
        }
        double x=0.5,y=0.7;
        getWrappedLines(a, b, x, y);

    }
}

运行结果

[0.5 , 0.55 , 0.6000000000000001 , 0.65 , 0.7 , 0.7 , 0.75 , 0.8 , 0.85 , 0.9 , 0.9500000000000001 ]
5
点(0.5 , 0.7)在直线y=0.5x+0.5和直线y=0.6x+0.4之间

8.略 后面9.3会遇到
9.比较简单
10.略
11.略

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值