2.7.h
#ifndef _2_7_h
#define _2_7_h
#include
extern int RandInt(int i,int j);
clock_t start,finish;
extern void Substitution(int A[],int N);
#endif
RandInt.c
#include
#include
int RandInt(int i,int j)
{
int tmp;
if(i>j)
{
tmp=i;
i=j;
j=tmp;
}
srand((unsigned int)time(0));
return rand()%(j-i+1)+i;
}
alg1.c
#include
#include "2.7.h"
void Substitution(int A[],int N)
{
bool exist;
int i,j,tmp;
for(i=0;i<N;i++)
{
exist=true;
while(exist==true)
{
exist=false;
tmp=RandInt(1,N);
for(j=0;j<i;j++)
{
if(A[j]==tmp)
exist=true;
}
}
A[i]=tmp;
}
}
alg2.c
#include "2.7.h"
#include
void Substitution(int A[],int N)
{
int Used[N];
bool exist;
int i,j,tmp;
for(i=0;i<N;i++)
Used[i]=0;
for(i=0;i<N;i++)
{
exist=true;
while(exist==true)
{
tmp=RandInt(0,N);
if(Used[tmp-1]==0)
{
A[i]=tmp;
Used[tmp-1]=1;
exist=false;
}
}
}
}
alg3.c
#include "2.7.h"
static void Swap(int *p,int *q);
void Substitution(int A[],int N)
{
int i;
for(i=0;i<N;i++)
A[i]=i+1;
for(i=1;i<N;i++)
Swap(&A[i],&A[RandInt(0,i)]);
}
void Swap(int *p,int *q)
{
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
}
ceshi.c
#include
#include
#include
#include
#include "2.7.h"
int main(void)
{
double time;
int N,i,j;
char ch;
puts("Enter the \"N\":");
scanf("%d",&N);
int A[N];
start=clock();
Substitution(A,N);
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
printf("The whole time is %lf\n",time);
while((ch=getchar())!='\n')
continue;
puts("Verify the array?y/n:");
if((ch=getchar())=='y')
{
for(i=0,j=0;i<N;i++,j++)
{
if(j%5==0)
printf("\n");
printf("%d ",A[i]);
}
}
printf("\n");
return 0;
}
a.c
long int Pow(long int X,int N)
{
long int Sum=1;
int i;
for(i=1;i<=N;i++)
Sum*=X;
return Sum;
}
b.c
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;
}
#define NotFound -1
int BinarySearch(const int A[],int 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;
}
数组已经排序,采用对分查找,运行时间O(logN)。
a.c
int MinSubsequenceSum(const int A[],int N)
{
int ThisSum=0,i,MinSum=A[0];
for(i=0;i<N;i++)
{
ThisSum+=A[i];
if(ThisSum<MinSum)
MinSum=ThisSum;
else if(ThisSum>0)
ThisSum=0;
}
return MinSum;
}
O(N)
b.c
#include
#define NotFound -1
int MinSubsequenceSum(const int A[],int N)
{
bool Sorted=false;
int i,S[N],Sum=0,temp,Subtraction;
for(i=0;i<N;i++)
{
Sum+=A[i];
S[i]=Sum;
}
while(Sorted==false)
{
Sorted=true;
for(i=0;i<N-1;i++)
{
if(S[i]>S[i+1])
{
temp=S[i];
S[i]=S[i+1];
S[i+1]=temp;
Sorted=false;
}
}
}
Sum=0;
for(i=0;i<N-1;i++)
{
Subtraction=S[i+1]-S[i];
if(Subtraction>0)
if(Sum==0)
Sum=Subtraction;
else if(Sum>Subtraction)
Sum=Subtraction;
}
if(Sum>0)
return Sum;
else
return NotFound;
}
O(N)
c.c
#define Dmax(a,b) ((a)>(b)?(a):(b))
#define Dmin(a,b) ((a)<(b)?(a):(b))
#define Tmax(a,b,c) (((a)>(b)?(a):(b))>(c)?((a)>(b)?(a):(b)):(c))
#define Tmin(a,b,c) (((a)<(b)?(a):(b))<(c)?((a)>(b)?(a):(b)):(c))
#include
int MaxSubsequenceSum(const int A[],int N)
{
int i,temp;
long long int *Max;
long long int Min=0;
long long int Midmax=0;
Max=(int *)malloc(sizeof(long long int)*N);
Max[0]=A[0];
for(i=1;i<N;i++)
{
temp=A[i];
Max[i]=Tmax(A[i-1]*temp,Min*temp,temp);
Min=Tmin(A[i-1]*temp,Min*temp,temp);
}
for(i=0;i<N;i++)
{
Midmax=Dmax(Max[i],Midmax);
}
return Midmax;
}
O(N)
a.c
#include
#include
bool IsPrime(int N)
{
int i;
if(N==1)
return false;
else
for(i=2;i<=(int)(sqrt(N));i++)
{
if(N%i==0)
return false;
}
return true;
}
b. O(N)
c. logN
d. 2^B/2
#include
#include
long int Power(long int X,int N)
{
long int result=1;
long int * array;
int arrlength,i;
arrlength=(int)log2(N)+1;
array=(long int *)malloc(sizeof(long int)*arrlength);
array[0]=X;
for(i=1;i<arrlength;i++)
{
array[i]=array[i-1]*array[i-1];
}
for(i=0;N>0;N/=2,i++)
{
if(N%2==1)
result*=array[i];
}
free(array);
return result;
}
a. 当数组内元素个数小于等于2时,递归终止
b.
#include
int FindChief(int A[],int N)
{
int B[N/2+1],temp,mid,i,j=0;
if(N==1)
return A[0];
else if(N==2)
{
if(A[0]==A[1])
return A[0];
else
return INT_MAX;
}
if(N%2==1)
{
temp=A[N-1];
for(i=0;i<N-2;i+=2)
{
if(A[i]==A[i+1])
{
B[j]=A[i];
j++;
}
}
if((mid=FindChief(B,j))==INT_MAX)
return temp;
else
return mid;
}else
{
for(i=0;i<N-1;i+=2)
{
if(A[i]==A[i+1])
{
B[j]=A[i];
j++;
}
}
return FindChief(B,j);
}
}
N是奇数时用一个中间量储存A【N-1】,继续寻找前面数组的主要元素,如果找到就返回找到的那个,找不到就返回储存的这个中间量。
e.c
#include
int FindChief(int A[],int N)
{
int chief,count=0,i;
for(i=0;i<N;i++)
{
if(count==0)
{
chief=A[i];
count++;
}
else if(chief==A[i])
count++;
else
count--;
}
if(count>0)
return chief;
else
return INT_MAX;
}
不行,假设Low=1,High=2,这是Mid=1,带人Low=Mid,那么Low依然是1,循环无法结束。
不行,递归无法结束。