C语言设计------第二章 算法---程序的灵魂

第二章 算法—程序的灵魂
例2.16 求5!
例2.17 求多项式1-1/2+1/3-1/4+…+1/99-1/100的值

C语言设计------第二章 算法---程序的灵魂_第1张图片

例2.16 求5!

#include 
int main()
{
	int i,t;
	t=1;
	i=2;
	while(i<=5)
	{
	t=t*i;
	i=i+1;
	}
	printf("%d\n",t);
	 return 0;
}

运行结果
C语言设计------第二章 算法---程序的灵魂_第2张图片

例2.17 求多项式1-1/2+1/3-1/4+…+1/99-1/100的值

#include 
int main()
{
	int sign=1;
	double deno=2.0,sum=1.0,term;   //定义demo,sum,term 为双精度型变量
	while(deno<=100)
		{
		sign=-sign;
		term=sign/deno;
		sum=sum+term;
		deno=deno+1;
		}
	printf("%f\n",sum);
	return 0;
}

运行结果

C语言设计------第二章 算法---程序的灵魂_第3张图片

一个小时的任务,硬生生拖了一星期??

— 2019.1.29

你可能感兴趣的:(1)