心动C++ 情牵基础算法VI

/*----------------------------------------------------------------------------
 * Project: Algorithms.cpp
 * Name: zwp
 * Date: 2014.3
 *-----------------------------------------------------------------------------*/



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


#define TRUE	1
#define FALSE   0

typedef int ElementType;
struct Point
{
	ElementType x;
	ElementType y;
};


typedef Point *POINT;
typedef int	**Matrix;


ElementType Dist(struct Point* num1, struct Point* num2)
{
	ElementType result = (num1->x - num2->x)*(num1->x - num2->x) + (num1->y - num2->y)*(num1->y - num2->y);

	return sqrt((double)result);
}


/*
** 求最近点对
*/
POINT NearPoint(POINT *point, int N)
{
	int i, j;

	for(i = 0; i < N; ++ i)
		for(j = i + 1; j < N; ++ j)
			if(Dist(point[i], point[j]) < 10)
				return (&point[i][j]);

}


/*
** 矩阵相乘
*/
void MatrixMultiply(Matrix A, Matrix B, Matrix C, int N)
{
	int i, j, k;

	/* Initialize Matrix */
	for(i = 0; i < N; ++ i)
		for(j = 0; j < N; ++ j)
			C[i][j] = 0;

	for(i = 0; i < N; ++ i)
		for(j = 0; j < N; ++ j)
			for(k = 0; k < N; ++ k)
				C[i][j] += A[i][k] * B[k][j];

}


/*
** 计算斐波那契数,糟糕版
*/
ElementType Fib(int N)
{
	if(N <= 1)
		return 1;
	else
		return Fib(N-1) + Fib(N-2);
}

/*
** 计算斐波那契数,优化版
*/
ElementType Fibonacci(int N)
{
	int i, Last, NextToLast, Answer;

	if(N <= 1)
		return 1;
	Last = NextToLast = 1;
	for(i = 2; i <= N; ++ i)
	{
		Answer = Last + NextToLast;
		NextToLast = Last;
		Last = Answer;
	}
	return Answer;
}

/*
** 计算公式
*/
double Eval(int N)
{
	int i;
	double Sum;

	if(N == 0)
		return 1.0;
	else
	{
		Sum = 0.0;
		for(i = 0; i < N; ++ i)
			Sum += Eval(i);

		return (2.0*Sum/N + N);
	}

}

/*
** 计算公式,优化版
*/
double Evale(int N)
{
	int i, j;
	double Sum, Answer;
	double *C;

	C = (double*)malloc(sizeof(double) * (N+1));
	if(C == NULL)
		printf("Out of Space....\n");
	C[0] = 1.0;
	for(i = 1; i <= N; ++ i)
	{
		Sum = 0.0;
		for(j = 0; j < i; ++ j)
			Sum += C[j];

		/* 利用数组记录结果 */
		C[i] = 2.0 * Sum/i + i;


	}
	Answer = C[N];
	free(C);

	return Answer;
}


typedef ElementType** TwoDimArray;
/*
** 矩阵乘法优化版M
*/
void OptMatrix(const long C[], int N, TwoDimArray M, TwoDimArray LastChange)
{
	int i, k, Left, Right;
	long ThisM;

	for(Left = 1; Left <= N; ++ Left)
		M[Left][Left] = 0;

	for(k = 1; k < N; ++ k)
		for(Left = 1; Left <= N-k; Left ++)
		{
			/* for each position */
			Right = Left + k;
			M[Left][Right] = 0;
			for(i = Left; i < Right; ++ i)
			{
				ThisM = M[Left][i] + M[i+1][Right] + C[Left-1]*C[i]*C[Right];
				if(ThisM < M[Left][Right])
				{
					/* Update min */
					M[Left][Right] = ThisM;
					LastChange[Left][Right] = i;
				}
			}
		}
}


/*
** 最短路径问题
*/
void AllPairs(TwoDimArray A, TwoDimArray D, TwoDimArray Path, int N)
{
	int i, j, k;

	/* Initialize D and Path */
	for(i = 0; i < N; ++ i)
		for(j = 0; j < N; ++ j)
		{
			D[i][j] = A[i][j];
			Path[i][j] = 0;		/* NotAVertx */
		}

	for(k = 0; k < N; ++ k)
		for(i = 0; i < N; ++ i)
			for(j = 0; j < N; ++ j)
			{
				/* Update shortest path */
				D[i][j] = D[i][k] + D[k][j];
				Path[i][k] = k;
			}

}

/*
** Random numbers
*/
static unsigned long seed = 1;

#define A 48271L
#define M 2147483647L
#define QQ (M / A)
#define RR (M % A)

long Seed = 100;

double Random(void)
{
	long TmpSeed;

	TmpSeed = A * (Seed % QQ) - RR * (Seed / QQ);
	if(TmpSeed >= 0)
		Seed = TmpSeed;
	else
		Seed = TmpSeed + M;

	return (double) (Seed/M);
}



void Initialize(unsigned long InitVal)
{
	Seed = InitVal;
}


/*
** 判断是否是素数
*/
ElementType Wintness(ElementType H, ElementType i, ElementType N)
{
	ElementType X, Y;

	if( i == 0)
		return 1;

	X = Wintness(H, i / 2, N);
	if(X == 0)
		return 0;

	Y = (X * X) % N;
	if(Y == 1 && X != 1 && X != N - 1)
		return 0;

	if(i % 2 != 0)
		Y = (H * Y) % N;

	return Y;
}


ElementType IsPrimer(ElementType N)
{
	srand(time(0));
	return (Wintness(rand(), N-1, N) == 1) ? TRUE : FALSE;

}


int main(int argc, char* argv[])
{
	printf("%d \n", Evale(20));
	int i = 0;

	for(i = 10; i < 100; ++ i)
	{
		if(IsPrimer(i) == TRUE)
			printf("%lf\n", i);
	}


	system("pause"); 
	return 0;
}
不要让你的程序设计水平停留在“HelloWorld”级,同样不要让你的嵌入式水平停留在“流水灯”。

记得有本微软的书《C 编程精髓》不错推荐给阁下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值