C语言入门代码库,苏小红版第四版C语言程序设计

苏小红版第四版C语言程序设计,第六章习题 

//#include<stdio.h>
//
//int main(void)
//{
//	int i;
//	for(i = 100; i <= 200; i++)
//	{
//		int j=0;
//		int flag = 1;
//		for (j = 2; j <= i - 1; j++)
//		{
//			if (i % j == 0)
//			{
//				flag = 0;
//				break;
//			}
//		}
//		if (flag == 1)
//		{
//			printf("%d\n", i);
//		}
//	}
//	return 0;
//}

//6.4
//#include<stdio.h>
//int main(void)
//{
//	int x;
//	int i = 1;
//	printf("please input x:");
//	scanf("%d", &x);
//	while(i<=x)
//	{
//		printf("%d*%d=%d\n", i, i, i * i);
//		printf("%d*%d*%d=%d\n", i, i, i, i * i * i);
//		i++;
//	}
//	return 0;
//}

//6.5
//#include<stdio.h>
//int main(void)
//{
//	int f;
//	float c;
//	for (f = 0; f <= 300; f += 10)
//	{
//		c = 5.0 / 9 * (f - 32);
//		printf("华氏温度为%4d时的摄氏度为%6.1f\n", f, c);
//	}
//	return 0;


//6.6
//#include<stdio.h>
//#define RATE    0.01875	//宏定义 月息
//#define M0NTHS  12		//宏定义 12个月
//#define CAPITAL 1000	//宏定义 本金
//#define YEARS   5		//宏定义 年份
//int main(void)
//{
//	int i;
//	double deposit = 0;
//	for (i = 0; i < YEARS; i++)
//	{
//		deposit = (deposit + CAPITAL) / (1 + RATE * M0NTHS);
//	}
//	printf("He must save%.2fat the first year\n", deposit);
//	return 0;
//}

//6.7
//#include<stdio.h>
//#define CURRENT 100
//int main()
//{
//	int year;
//	double growrate;
//	double output;
//	printf("Input growrate:");
//	scanf("%lf", &growrate);
//	output = CURRENT;
//	for (year = 0; output < 2 * CURRENT; year++)
//	{
//		output = output * (1 + growrate);
//	}
//	printf("When growrate is%0.f%%,the output can be double affter%dyear\n", growrate * 100, year);
//	return 0;
//}

//6.8
//#include<stdio.h>
//#include<math.h>
//int main()
//{
//	double pi, sum = 0, sign = 1.0;
//	double term;
//	int count = 0, n = 1;
//	do
//	{
//		term = sign / n;
//		sum = sum + term;
//		count++;
//		sign = -sign;
//		n = n + 2;
//	} while (fabs(term) >= 1e-4);
//	pi = sum * 4;
//	printf("pi = % f\ncount = % d\n", pi, count);
//	return 0;
//}

//6.9
//#include<stdio.h>
//#include<math.h>
//int main(void)
//{
//	int n = 1, count = 1;
//	double e = 1.0, term = 1.0;
//	long fac = 1;
//	for(n = 1; fabs(term) >= 1e-5; n++)
//	{
//		fac = fac * n;
//		term = 1.0 / fac;
//		e = e + term;
//		count += 1;
//	}
//	printf("e=%f,count=%d", e, count);
//	return 0;
//}

//6.10

//#include<stdio.h>
//int main(void)
//{
//	int a, b, c, x;
//	for (x = 100; x <= 999; x++)
//	{
//		a = x / 100;
//		b = (x - a*100) / 10;
//		c = x % 10;
//		if (x == a * a * a + b * b * b + c * c * c)
//		{
//			printf("%d\n", x);
//		}
//	}
//	return 0;
//}

//6.11
//#include<stdio.h>
//int main(void)
//{
//	int n;
//	int m = 1, sum = 1;
//	printf("Please input n:");
//	scanf("%d", &n);
//	do
//	{
//		sum = sum * m;
//		m++;
//	} while (sum < n);
//	printf("%d\n", m);
//	return 0;
//}

//6.12
//#include<stdio.h>
//int main(void)
//{
//	int i = 0, n, sum = 0;
//	printf("Please input a number:");
//	scanf("%d", &n);
//	while (n > 0)
//	{
//		sum = sum + n;
//		printf("Please input a number:");
//		scanf("%d", &n);
//		i++;
//	}
//	printf("sum = %d,count = %d\n",sum,i);
//	return 0;
//}

//6.13
//#include<stdio.h>
//int main(void)
//{
//	int i = 0, n, sum = 0;
//	printf("Input a number :");
//	scanf("%d", &n);
//	while (n != 0)
//	{
//		if (n > 0)
//		{
//			sum = sum + n;
//			i++;
//		}
//		printf("Input a number:");
//		scanf("%d", &n);
//	}
//	printf("sum=%d,count=%d\n", sum, i);
//	return 0;
//}

//6.14
//
//#include<stdio.h>
//int main(void)
//{
//	int x, y, z;
//	printf("MAN\tWOMEN\tCHILDREN\n");
//	for (x = 0; x <= 30; x++)
//	{
//		for (y = 0; y <= 30; y++)
//		{
//			for (z = 0; z<=30; z++)
//			{
//				if ((x + y + z == 30) && (3 * x + 2 * y + z == 50))
//				{
//					printf("%3d\t%5d\t%8d\n", x, y, z);
//				}
//			}
//		}
//	}
//	return 0;
//}

//6.16
//#include<stdio.h>
//int main(void)
//{
//	int x, y, z;	//分别代表公鸡母鸡小鸡
//	printf("G\tM\tX\n");
//	for (x = 0; x <= 100; x++)
//	{
//		for (y = 0; y <= 100; y++)
//		{
//			for (z = 0; z <= 100;z++)
//			{
//				if ((100 == 5 * x + 3 * y + (z / 3)) && (100 == x + y + z))
//				{
//					printf("%d\t%d\t%d\n", x, y, z);
//
//				}
//			}
//		}
//	}
//	return 0;
//}

//6.17
//#include<stdio.h>
//int main(void)
//{
//	int x, y, z;
//	int count = 0;
//	for (x = 0; x < 50; x++)
//	{
//		for (y = 0; y < 50; y++)
//		{
//			for (z = 0; z < 50; z++)
//			{
//				if ((50 == x + y + z) && (100 == 10 * x + 5*y + z))
//				{
//					printf("x=%d,y=%d,z=%d\n", x, y, z);
//					count++;
//				}
//			}
//		}
//	}
//	printf("count=%d\n", count);
//	return 0;
//}

//6.18(1)
//#include<stdio.h>
//int main(void)
//{
//	int m, n;
//	for (m = 1; m <= 9; m++)
//	{
//		for (n = 1; n <= 9; n++)
//		{
//			printf("%d*%d=%d\t",m,n,m*n);
//		}
//		printf("\n");
//	}
//	return 0;
//}

//(2)
//#include<stdio.h>
//int main(void)
//{
//	int m, n;
//	for (m = 1; m <= 9; m++)
//	{
//		for (n = 1; n <= m; n++)
//		{
//			printf("%d*%d=%d\t", m, n, m * n);
//		}
//		printf("\n");
//	}
//	return 0;
//}

//(3)
//#include<stdio.h>
//int main(void)
//{
//	int m, n;
//	for (m = 9; m>0; m--)
//	{
//		for (n = 1; n <= m; n++)
//		{
//			printf("%d*%d=%d\t", m, n, m * n);
//		}
//		printf("\n");
//	}
//	return 0;
//}

//6.19
//#include<stdio.h>
//int main(void)
//{
//	double y = 0.01;
//	int n = 0;
//	double sum1 = 0, sum2 = 0;
//	for (n = 1; n <= 30; n++)
//	{
//		sum1 = sum1 + 100000;
//
//		sum2 = sum2 + y;
//		y = 2 * y;
//	}
//	printf("sum1=%f,sum2=%f\n", sum1, sum2);
//	return 0;
//}

//6.20
//#include<stdio.h>
//int main(void)
//{
//	int x, y, k, m;
//	for (x = 1; x <= 9; x++)
//	{
//		for (y = 1; y <= 9; y++)
//		{
//			if (x!=y)
//			{
//				k = x * 1000 + x * 100 + y * 10 + y;
//				for (m = 31; m * m <= k; m++)
//				{
//					if (m * m == k)
//					{
//						printf("k=%d,m=%d\n", k, m);
//					}
//				}
//			}
//		}
//	}
//	return 0;
//}

//6.21
//#include<stdio.h>
//int main(void)
//{
//	int n = 0, t = 0;
//	for (t = 0;t <= 20 * 7; t++)
//	{
//		if (t % 5 == 0 && t <= 20 * 5)
//		{
//			n++;
//			continue;
//		}
//		if (t % 6 == 0 && t <= 20 * 6)
//		{
//			n++;
//		}
//	}
//	printf("n=%d\n", n);
//	return 0;
//}


//6.22(1)
//#include<stdio.h>
//#include<math.h>
//
//#define cube 1.42e8
//int main(void)
//{
//	int n;
//	double sum = 0, i = 1;
//	for (n = 1; n <= 64; n++)
//	{
//		i = i * 2;
//		sum = sum + 1;
//	}
//	printf("sum=%e\n", sum);
//	printf("volum=%e\n", sum / cube);
//	return 0;
//}

//(2)
#include<stdio.h>
#include<math.h>

#define cube 1.42e8
int main(void)
{
	int n;
	double sum = 0, term;
	for (n = 1; n <= 64; n++)
	{
		term = pow(2, n - 1);
		sum = sum + term;
	}
	printf("sum=%e\n", sum);
	printf("volum=%e\n", sum / cube);
	return 0;
}//#include<stdio.h>
//
//int main(void)
//{
//	int i;
//	for(i = 100; i <= 200; i++)
//	{
//		int j=0;
//		int flag = 1;
//		for (j = 2; j <= i - 1; j++)
//		{
//			if (i % j == 0)
//			{
//				flag = 0;
//				break;
//			}
//		}
//		if (flag == 1)
//		{
//			printf("%d\n", i);
//		}
//	}
//	return 0;
//}

//6.4
//#include<stdio.h>
//int main(void)
//{
//	int x;
//	int i = 1;
//	printf("please input x:");
//	scanf("%d", &x);
//	while(i<=x)
//	{
//		printf("%d*%d=%d\n", i, i, i * i);
//		printf("%d*%d*%d=%d\n", i, i, i, i * i * i);
//		i++;
//	}
//	return 0;
//}

//6.5
//#include<stdio.h>
//int main(void)
//{
//	int f;
//	float c;
//	for (f = 0; f <= 300; f += 10)
//	{
//		c = 5.0 / 9 * (f - 32);
//		printf("华氏温度为%4d时的摄氏度为%6.1f\n", f, c);
//	}
//	return 0;


//6.6
//#include<stdio.h>
//#define RATE    0.01875	//宏定义 月息
//#define M0NTHS  12		//宏定义 12个月
//#define CAPITAL 1000	//宏定义 本金
//#define YEARS   5		//宏定义 年份
//int main(void)
//{
//	int i;
//	double deposit = 0;
//	for (i = 0; i < YEARS; i++)
//	{
//		deposit = (deposit + CAPITAL) / (1 + RATE * M0NTHS);
//	}
//	printf("He must save%.2fat the first year\n", deposit);
//	return 0;
//}

//6.7
//#include<stdio.h>
//#define CURRENT 100
//int main()
//{
//	int year;
//	double growrate;
//	double output;
//	printf("Input growrate:");
//	scanf("%lf", &growrate);
//	output = CURRENT;
//	for (year = 0; output < 2 * CURRENT; year++)
//	{
//		output = output * (1 + growrate);
//	}
//	printf("When growrate is%0.f%%,the output can be double affter%dyear\n", growrate * 100, year);
//	return 0;
//}

//6.8
//#include<stdio.h>
//#include<math.h>
//int main()
//{
//	double pi, sum = 0, sign = 1.0;
//	double term;
//	int count = 0, n = 1;
//	do
//	{
//		term = sign / n;
//		sum = sum + term;
//		count++;
//		sign = -sign;
//		n = n + 2;
//	} while (fabs(term) >= 1e-4);
//	pi = sum * 4;
//	printf("pi = % f\ncount = % d\n", pi, count);
//	return 0;
//}

//6.9
//#include<stdio.h>
//#include<math.h>
//int main(void)
//{
//	int n = 1, count = 1;
//	double e = 1.0, term = 1.0;
//	long fac = 1;
//	for(n = 1; fabs(term) >= 1e-5; n++)
//	{
//		fac = fac * n;
//		term = 1.0 / fac;
//		e = e + term;
//		count += 1;
//	}
//	printf("e=%f,count=%d", e, count);
//	return 0;
//}

//6.10

//#include<stdio.h>
//int main(void)
//{
//	int a, b, c, x;
//	for (x = 100; x <= 999; x++)
//	{
//		a = x / 100;
//		b = (x - a*100) / 10;
//		c = x % 10;
//		if (x == a * a * a + b * b * b + c * c * c)
//		{
//			printf("%d\n", x);
//		}
//	}
//	return 0;
//}

//6.11
//#include<stdio.h>
//int main(void)
//{
//	int n;
//	int m = 1, sum = 1;
//	printf("Please input n:");
//	scanf("%d", &n);
//	do
//	{
//		sum = sum * m;
//		m++;
//	} while (sum < n);
//	printf("%d\n", m);
//	return 0;
//}

//6.12
//#include<stdio.h>
//int main(void)
//{
//	int i = 0, n, sum = 0;
//	printf("Please input a number:");
//	scanf("%d", &n);
//	while (n > 0)
//	{
//		sum = sum + n;
//		printf("Please input a number:");
//		scanf("%d", &n);
//		i++;
//	}
//	printf("sum = %d,count = %d\n",sum,i);
//	return 0;
//}

//6.13
//#include<stdio.h>
//int main(void)
//{
//	int i = 0, n, sum = 0;
//	printf("Input a number :");
//	scanf("%d", &n);
//	while (n != 0)
//	{
//		if (n > 0)
//		{
//			sum = sum + n;
//			i++;
//		}
//		printf("Input a number:");
//		scanf("%d", &n);
//	}
//	printf("sum=%d,count=%d\n", sum, i);
//	return 0;
//}

//6.14
//
//#include<stdio.h>
//int main(void)
//{
//	int x, y, z;
//	printf("MAN\tWOMEN\tCHILDREN\n");
//	for (x = 0; x <= 30; x++)
//	{
//		for (y = 0; y <= 30; y++)
//		{
//			for (z = 0; z<=30; z++)
//			{
//				if ((x + y + z == 30) && (3 * x + 2 * y + z == 50))
//				{
//					printf("%3d\t%5d\t%8d\n", x, y, z);
//				}
//			}
//		}
//	}
//	return 0;
//}

//6.16
//#include<stdio.h>
//int main(void)
//{
//	int x, y, z;	//分别代表公鸡母鸡小鸡
//	printf("G\tM\tX\n");
//	for (x = 0; x <= 100; x++)
//	{
//		for (y = 0; y <= 100; y++)
//		{
//			for (z = 0; z <= 100;z++)
//			{
//				if ((100 == 5 * x + 3 * y + (z / 3)) && (100 == x + y + z))
//				{
//					printf("%d\t%d\t%d\n", x, y, z);
//
//				}
//			}
//		}
//	}
//	return 0;
//}

//6.17
//#include<stdio.h>
//int main(void)
//{
//	int x, y, z;
//	int count = 0;
//	for (x = 0; x < 50; x++)
//	{
//		for (y = 0; y < 50; y++)
//		{
//			for (z = 0; z < 50; z++)
//			{
//				if ((50 == x + y + z) && (100 == 10 * x + 5*y + z))
//				{
//					printf("x=%d,y=%d,z=%d\n", x, y, z);
//					count++;
//				}
//			}
//		}
//	}
//	printf("count=%d\n", count);
//	return 0;
//}

//6.18(1)
//#include<stdio.h>
//int main(void)
//{
//	int m, n;
//	for (m = 1; m <= 9; m++)
//	{
//		for (n = 1; n <= 9; n++)
//		{
//			printf("%d*%d=%d\t",m,n,m*n);
//		}
//		printf("\n");
//	}
//	return 0;
//}

//(2)
//#include<stdio.h>
//int main(void)
//{
//	int m, n;
//	for (m = 1; m <= 9; m++)
//	{
//		for (n = 1; n <= m; n++)
//		{
//			printf("%d*%d=%d\t", m, n, m * n);
//		}
//		printf("\n");
//	}
//	return 0;
//}

//(3)
//#include<stdio.h>
//int main(void)
//{
//	int m, n;
//	for (m = 9; m>0; m--)
//	{
//		for (n = 1; n <= m; n++)
//		{
//			printf("%d*%d=%d\t", m, n, m * n);
//		}
//		printf("\n");
//	}
//	return 0;
//}

//6.19
//#include<stdio.h>
//int main(void)
//{
//	double y = 0.01;
//	int n = 0;
//	double sum1 = 0, sum2 = 0;
//	for (n = 1; n <= 30; n++)
//	{
//		sum1 = sum1 + 100000;
//
//		sum2 = sum2 + y;
//		y = 2 * y;
//	}
//	printf("sum1=%f,sum2=%f\n", sum1, sum2);
//	return 0;
//}

//6.20
//#include<stdio.h>
//int main(void)
//{
//	int x, y, k, m;
//	for (x = 1; x <= 9; x++)
//	{
//		for (y = 1; y <= 9; y++)
//		{
//			if (x!=y)
//			{
//				k = x * 1000 + x * 100 + y * 10 + y;
//				for (m = 31; m * m <= k; m++)
//				{
//					if (m * m == k)
//					{
//						printf("k=%d,m=%d\n", k, m);
//					}
//				}
//			}
//		}
//	}
//	return 0;
//}

//6.21
//#include<stdio.h>
//int main(void)
//{
//	int n = 0, t = 0;
//	for (t = 0;t <= 20 * 7; t++)
//	{
//		if (t % 5 == 0 && t <= 20 * 5)
//		{
//			n++;
//			continue;
//		}
//		if (t % 6 == 0 && t <= 20 * 6)
//		{
//			n++;
//		}
//	}
//	printf("n=%d\n", n);
//	return 0;
//}


//6.22(1)
//#include<stdio.h>
//#include<math.h>
//
//#define cube 1.42e8
//int main(void)
//{
//	int n;
//	double sum = 0, i = 1;
//	for (n = 1; n <= 64; n++)
//	{
//		i = i * 2;
//		sum = sum + 1;
//	}
//	printf("sum=%e\n", sum);
//	printf("volum=%e\n", sum / cube);
//	return 0;
//}

//(2)
#include<stdio.h>
#include<math.h>

#define cube 1.42e8
int main(void)
{
	int n;
	double sum = 0, term;
	for (n = 1; n <= 64; n++)
	{
		term = pow(2, n - 1);
		sum = sum + term;
	}
	printf("sum=%e\n", sum);
	printf("volum=%e\n", sum / cube);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花开富贵xxxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值