1. 最大子序列和问题
int MaxSubsquenceSum(const int A[],int N){
int ThisSum,MaxSum,j;
ThisSum = MaxSum = 0;
for(j=0;j<N;j++){
ThisSum += A[j];
if(ThisSum > MaxSum)
MaxSum = ThisSum;
else if(ThisSum < 0)
ThisSum = 0;
}
return MaxSum;
}
2. 二分查找法
int BinarySearch(const ElementType A[], ElementType X, int N){
int Low,Mid,High;
Low=0;High=N-1;
while(Low <= High){
Mid = (Low + High)/2;
if(a[Mid] < X)
Low = Mid + 1;
else if(a[Mid] > X)
High = Mid -1;
else
return Mid;
}
return NotFound;
}
3.欧几里德算法(求最大公因数)
unsigned int
Gcd(unsigned int M,unsigned int N){
unsigned int Rem;
while(N > 0){
Rem = M % N;
M = N;
N = Rem;
}
return M;
}
4.幂运算(计算X的n次方)
long int Pow(long int X,unsigned int N){
if(N == 0) return 1;
if(N == 1) return X;
if(IsEven(N)) return Pow(X * X,N/2);
else return Pow(X * X,N/2) * X;
}