iterative and recursive implementations of binary lookups:
#include <stdio.h>
#include <stdlib.h>
int find1(int *a, int low , int high , int key)//迭代二分查找
{
int mid = 0;
while (low <= high)
{
mid = (low+high)/2;
if (a[mid] == key)
return mid;
else if (a[mid] < key)
low = mid + 1;
else
high = mid -1;
}
return -1;
}
int find2(int *a, int low , int high , int key) //递归二分查找
{
if (low > high)
return -1;
int mid = (low+high)/2;
if (a[mid] == key)
return mid;
else if (a[mid] < key)
find1(a,mid+1,high,key);
else
find1(a,low,mid-1,key);
}
int main()
{ /code>
int array Span class= "pun" >[ 13 ] = { 2 6 8 12 32 64 67 78 98 104 120 134 140
int key = find2(array,0,12,140);
printf("%d\n",array[key]);
return 0;
}
gcc Common options
How does gcc know the file type? Determine file types by file extension
Makefile rules follow the general form
target:dependency [dependency[...] Dependency is the meaning of dependenceCommandCommand[...]The first character of each command must be the TAB key, not the space key, or make will error and stop.
a slightly more complex makefile, which reads as follows:
start:hello.ogcc-o Hello hello.o
hello.o:gcc-o hello.o-c hello.c
The hello.o behind target start stands for the command dependent on the target with HELLO.O. so make first executes the command under the target of HELLO.O.
use variables in makefile:
cc=gccsrcs=hello.cobjs=$ (SRCS:.C=.O) Exec=hello
START:HELLO.O $ (CC)-O $ (EXEC) $ (OBJS) @echo '---------------OK---------------'
HELLO.O: $ (CC)-O $ (OBJS)-C $ (SRCS)
Clean:rm-f hello.o
add Variable cc,SRCs, Objs, EXEC,Each reference variable is expanded to a variable value in the place of the CC. objs=$ (SRCS:.C=.O), which means to replace. C in the SRCS variable with. O.
Makefile Pattern Rules
. Suffixes:. C. o indicates that any x.c file is associated with X.O. C.O: Indicates that make defines a rule, and any X.O files are compiled from X.C make defines some useful pre-defined variables
examples of automatic variables and pattern rules are used in makefile and multiple source files can be compiled
. Suffixes:.c. OCC=GCCsrcs=hello.c\PUB.C
objs=$ (SRCS:.C=.O)Exec=hello
start:$ (OBJS)$ (CC)-O $ (EXEC) $ (OBJS)@echo '---------------OK---------------'
. C.O:$ (CC)-o [email protected]-C $<
Clean :rm-f $ (OBJS)
GDB debugging must be added to the-G option:
GDB program name [Corefile] The corefile is optional, but it enhances GDB's ability to debug. Linux does not generate Corefile by default, so it needs to be added in the user profile
vi. ProfileUlimit-c Unlimited.. Profile
gdb App Core
From for notes (Wiz)
9.10 binary Find gcc makefile gdb debug