电子科大版《C语言上机指导》第五章部分习题参考答案(完更)

第三题:

#include<stdio.h>
void main()
{
	int x=0,m=0,n=0;
	printf("x=");
	scanf("%d",&x);
	while(n<5)
	{
		if (x%12==0||x%15==0)
		{
			m+=x;
		}
		n++;
		x--;
	}
	printf("sum=%d",m);
}

第四题

#include<stdio.h>
#include<math.h>
void main()
{
	int sign=1,count=1;				//count表示循环次数,sign表示当前循环的符号
	double pi=0,n=1.0,term=1.0;		//pi表示多项式的值,term表示当前项的值
	while(fabs(term)>=1e-6)
	{
		n=n+2;
		pi+=term;
		count++;
		sign=-sign;
		term=sign/n;
	}
	pi=pi*4;
	printf("pi=%10.6f\n",pi);
	printf("count=%d\n",count);
	getchar();
}

第五题(1)

#include<stdio.h>
void main()
{
	int sign=1;
	int result=0;
	int x=1;
	while(x<=101)
	{
		result+=sign*x;
		sign=-sign;
		x=x+2;
	}
	printf("%d",result);
	getchar();
}

第五题(2)

#include<stdio.h>
void main()
{
	double jiecheng(double x);
	double a,b;
	double result;
	a=1.0;
	b=3.0;
	result=1.0;
	while(b<=18)
	{	
		result +=jiecheng(a);
		b+=3;
	}
	printf("%lf",result);
}

double jiecheng(double x)
{
	int y;
	for(;y<=x;y++)
		x*=y;
}

第五题(3)

#include<stdio.h>
void main()
{
	int zhishu(int x);
	int a[30];
	int c = 0;
	int b = 0;
	for (int i = 0;; i++)
	{
		c = zhishu(i);
		if (c != 0)
		{
			if (b == 30)
				break;
			a[b] = i;
			b++;
		}
	}
	for (int p = 1; p <= 30; p++)
	{
		printf("%d\t", a[p-1]);
		if (p % 6 == 0)
			printf("\n");
	}
}
int zhishu(int x)
{
	for (int n = 2; n < x; n++)
	{
		if (x % n == 0)
			return 0;
	}
	return x;
}

第五题(4)

#include<stdio.h>
void main()
{
	int m, n,r;
	printf("请输入两个正整数,并用空格分隔");
	scanf("%d %d", &m, &n);
	if (m<n)
	{
		r = n;
		n = m;
		m = r;
	}
	flag:
	r = m % n;
	if (r == 0)
		printf("%d", n);
	else
	{
		m = n;
		n = r;
		goto flag;
	}
}

第五题(5)

#include<stdio.h>
void main()
{
	int i, j, k;
	for (i = 0; i <= 3; i++)
	{
		for (j = 0; j < 3 - i; j++)
			printf(" ");
		for (k=0;k<2*i+1;k++)
			printf("*");
		printf("\n");
	}
	for (i=2; i >= 0; i--)
	{
		for (j = 0; j < 3 - i; j++)
			printf(" ");
		for (k = 0; k < 2 * i + 1; k++)
			printf("*");
		printf("\n");
	}
}

第六题

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) 2*x*x*x-4*x*x+3*x-6
void main()
{
	double  x0, x1, x2;
shuru:
	printf("请输入两个数并用空格分隔");
	scanf("%lf %lf", &x1, &x2);
	if (f(x1) * f(x2) > 0)
	{
		printf("跳转输入");
			goto shuru;
	}
zhongdian:
	x0 = (x1 + x2) / 2;
	if (f(x0) * f(x1) > 0)
		x1 = x0;
	else
		x2 = x0;
	if (abs(x1-x2) < pow(-6,10))
		printf("%.10lf", x0);
	else
	{
		printf("跳转求中点");
		goto zhongdian;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值