用AspJpeg组件,按宽高比例,真正生成缩略图

laifangsong's .NET blog

取长补短,精益求精。 (打个广告,想做手机网站和asp/asp.net网站的可以跟我联系.QQ:25313644)
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

二分查找算法

Posted on 2006-11-24 15:59  laifangsong  阅读(667)  评论(0编辑  收藏  举报

c语言:
/*
二分法查找
*/

#define N 6
main()
{
    
int a[N]={1,2,3,5,7,10};
    
int searchNum=5;
    
int index;

    index
=search(a,searchNum);
    
if(index!=-1)
        printf(
"a[%d]=%d",index,searchNum);
    
else
        printf(
"not found!");

    getch();
}

int search(int a[],int num)
{
    
int begin,middle,end;
    begin
=0;
    end
=N-1;
    middle
=(begin+end)/2;

    
while(a[middle]!=num && !(middle==begin&&middle==end))
    {
         
if(a[middle]<num)
             begin
=middle+1;
         
else
             end
=middle;
         middle
=(begin+end)/2;
    }
    
if(a[middle]==num)
        
return middle;
    
else
        
return -1;
}

c#:
using System;

namespace suafa
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    class Class1
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
//
            
// TODO: 在此处添加代码以启动应用程序
            
//
            int[] a = new int[]{1,2,3,5,7,10};
            
int searchNum = 100;
            
int index = search(a, searchNum);
            
if(index != -1)
            
{
                Console.Write(
"a[" + index + "]=" + searchNum);    
            }

            
else
            
{
                Console.Write(
"找不到");
            }

            Console.ReadLine();
        }


        
static private int search(int[] a, int num)
        
{
            
int length = a.Length-1;
            
int begin, middle, end;
            begin 
= 0;
            end 
= length;
            middle
= Convert.ToInt32((begin+end)/2);

            
while(a[middle]!=num && !(middle==begin && middle==end))
            
{
                
if(a[middle] < num)
                
{
                    begin 
= middle + 1;
                }

                
else
                
{
                    end 
= middle;
                }

                middle
=Convert.ToInt32((begin+end)/2);
            }

            
            
if(a[middle] == num)
            
{
                
return middle;
            }

            
else
            
{
                
return -1;
            }

        }

    }

}


asp:
<!--二分查找法-->
<%
    
dim a
dim index
dim searchNum
= array(1,2,3,5,7,10)
searchNum 
= 5

index 
= search(a,searchNum)
if index <> -1 then
    response.write 
"a(" & index & ")=" & searchNum 
else
    response.write 
"找不到"
end if

function search(a(), num)
    
dim bound
    bound 
= ubound(a)
    
dim begin, middle, ending
    begin 
= 0
    ending 
= bound
    middle 
= int((begin+ending)/2)
    
    
do while a(middle)<>num and not(begin=middle and ending=middle)
        
if a(middle) < num then
            begin 
= middle + 1
        
else
            ending 
= middle
        
end if
        middle 
= int((begin+ending)/2)
        
    
loop
    
if a(middle) = num then
        search 
= middle
    
else
        search 
= -1
    
end if
end function

    

%
>