C++程序设计(第3版)谭浩强 第4章 习题

1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用两个函数,并输出结果,两个整数由键盘输入。

【解】

#include <iostream>
using namespace std;
int main()
{
	int hcf(int, int);
	int lcd(int, int, int);
	int u, v, h, l;
	cin >> u >> v;
	h = hcf(u, v);
	cout << "H.C.F=" << h << endl;
	l = lcd(u, v, h);
	cout << "L.C.D=" << l << endl;
	return 0;
}
int hcf(int u, int v)
{
	int t, r;
	if (v > u)
	{
		t = u; u = v; v = t;
	}
	while ((r = u % v) != 0)
	{
		u = v;
		v = r;
	}
	return(v);
}
int lcd(int u, int v, int h)
{
	return(u*v / h);
}

运行结果:
//24 16↙    (输入两个整数)
//H.C.F = 8    (最大公约数)
//L.C.D = 48    (最小公倍数)

2.求方程ax2+bx+c=0的根,用3个函数分别求当b2-4ac大于0、等于0和小于0时的根,并输出结果。从主函数输入a,b,c的值。

【解】

#include <iostream>
#include <math.h>
using namespace std;
float x1, x2, disc, p, q;
int main()
{
	void greater_than_zero(float, float);
	void equal_to_zero(float, float);
	void smaller_than_zero(float, float);
	float a, b, c;
	cout << "input a,b,c:";
	cin >> a >> b >> c;
	disc = b * b - 4 * a * c;
	cout << "root:" << endl;
	if (disc > 0)
	{
		greater_than_zero(a, b);
		cout << "x1=" << x1 << ", x2=" << x2 << endl;
	}
	else if (disc == 0)
	{
		equal_to_zero(a, b);
		cout << "x1=" << x1 << ", x2=" << x2 << endl;
	}
	else
	{
		smaller_than_zero(a, b);
		cout << "x1=" << p << " + " << q << " i" << endl;
		cout << "x2=" << p << " - " << q << " i" << endl;
	}
	return 0;
}
void greater_than_zero(float a, float b)//定义一个函数, 用来求disc>0时方程的根 
{
	x1 = (-b + sqrt(disc)) / (2 * a);
	x2 = (-b - sqrt(disc)) / (2 * a);
}
void equal_to_zero(float a, float b)//定义一个函数, 用来求disc=0时方程的根
{
	x1 = x2 = (-b) / (2 * a);
}
void smaller_than_zero(float a, float b)//定义一个函数, 用来求disc<0时方程的根 
{
	p = -b / (2 * a);
	q = sqrt(-disc) / (2 * a);
}

运行结果:
//① input a, b, c:1 2 1
//root :
//x1  = –1, x2  = –1
//② input a, b, c : 2 4 1
//root :
//x1  = –0.2928931, x2  = –1.70711
//③ input a, b, c : 2 4 3
//x1  = –1 + 0.70107i, x2  = –1– 0.701107i

3.写一个判别素数的函数,在主函数中输入一个整数,输出是否为素数的信息。

【解】

#include < iostream >
using namespace std;
int main()
{
	int prime(int);                          //函数原型声明
	int n;
	cout << "input an integer:";
	cin >> n;
	if (prime(n))
		cout << n << " is a prime." << endl;
	else
		cout << n << " is not a prime." << endl;
	return 0;
}
int prime(int n)
{
	int flag = 1, i;
	for (i = 2; i < n / 2 && flag == 1; i++)
		if (n%i == 0)
			flag = 0;
	return(flag);
}

运行结果:
//input an integer : 17
//17 is a prime.

4.求a!+b!+c!的值,用一个函数fac(n)求n!。a,b,c的值由主函数输入,最终得到的值在主函数中输出。

【解】

#include <iostream>
using namespace std;
int main()
{
	int fac(int);
	int a, b, c, sum = 0;
	cout << "enter a,b,c:";
	cin >> a >> b >> c;
	sum = sum + fac(a) + fac(b) + fac(c);
	cout << a << "!+" << b << "!+" << c << "!=" << sum << endl;
	return 0;
}
int fac(int n)
{
	int f = 1;
	for (int i = 1; i <= n; i++)
		f = f * i;
	return f;
}

运行结果:

//enter a, b, c:4 5 6
//4!+ 5!+ 6 != 864

5.写一函数求sinh(x)的值,求sinh(x)的近似公式为sinh(x)=ex-e-x2其中用一个函数求ex。

【解】

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double e(double);
	double x, sinh;
	cout << "enter x:";
	cin >> x;
	sinh = (e(x) + e(-x)) / 2;				//计算sinh(x)
	cout << "sinh(" << x << ")=" << sinh << endl;
	return 0;
}
double e(double x)				//求ex的函数
{
	return exp(x);
}

运行结果:

//enter x : 1.5
//sinh(1.5) = 2.35241

6.用牛顿迭代法求根。方程为ax3+bx2+cx+d=0。系数a,b,c,d的值依次为1,2,3,4,由主函数输入。求x在1附近的一个实根。求出根后由主函数输出。

【解】

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double solut(double, double, double, double);
	double a, b, c, d;
	cout << "input a,b,c,d:";
	cin >> a >> b >> c >> d;
	cout << solut(a, b, c, d) << endl;
	return 0;
}
double solut(double a, double b, double c, double d)
{
	double x = 1, x0, f, f1;
	do
	{
		x0 = x;
		f = ((a*x0 + b)*x0 + c)*x0 + d;
		f1 = (3 * a*x0 + 2 * b)*x0 + c;
		x = x0 - f / f1;
	} while (fabs(x - x0) >= 1e-5);
	return(x);
}

运行结果:

//input a, b, c, d:4 3 2 1
//x  = – 0.60583

7.写一个函数验证哥德巴赫猜想: 一个不小于6的偶数可以表示为两个素数之和,如6=3+3, 8=3+5, 10=3+7,…,在主函数中输入一个不小于6的偶数n,然后调用函数gotbaha,在gotbaha函数中再调用prime函数,prime函数的作用是判别一个数是否为素数。在godbah函数中输出以下形式的结果: 34=3+31

【解】

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	void godbaha(int);
	int n;
	cout << "input n:";
	cin >> n;
	godbaha(n);					//调用godbaha函数, 选出满足要求的组合
	return 0;
}
void godbaha(int n)
{
	int prime(int);
	int a, b;
	for (a = 3; a <= n / 2; a = a + 2)			//a由3变到n/2(取其整数), 每次增值2
	{
		if (prime(a))					//调用prime函数, 如果a是素数则prime(a) 的值为1
		{
			b = n - a;					//a是素数, 应检查b是否为素数
			if (prime(b))				//如果b也是素数则输出结果
				cout << n << " = " << a << " + " << b << endl;
		}
	}
}
int prime(int m)					//判别m是否为素数的函数
{
	int i, k = sqrt(m);
	for (i = 2; i <= k; i++)
		if (m%i == 0) break;
	if (i > k)  	return 1;
	else     	return 0;
}

运行结果:

//input n : 20
//20 = 3 + 17
//20 = 7 + 13

8.用递归方法求n阶勒让德多项式的值,递归公式为

pn(x)= 1(n=0)

x(n=1)

((2n-1)·x-pn-1(x)-(n-1)·pn-2(x))/n(n≥1)

【解】

#include <iostream>
using namespace std;
int main()
{
	int x, n;
	float p(int, int);
	cout << "input n & x: ";
	cin >> n >> x;
	cout << "n = " << n << ", x = " << x << endl;
	cout << "P" << n << "(" << x << ")=" << p(n, x) << endl;
	return 0;
}
float p(int n, int x)
{
	if (n == 0)
		return(1);
	else if (n == 1)
		return(x);
	else
		return(((2 * n - 1)*x*p((n - 1), x) - (n - 1)*p((n - 2), x)) / n);
}

运行结果:

//① input n & x:0 7
//n = 0, x = 7
//P0(7) = 1
//② input n & x : 1 2
//n = 1, x = 2
//P1(2) = 2
//③ input n & x : 3 4
//n = 3, x = 4
//P3(4) = 154

9.Hanoi(汉诺)塔问题。这是一个经典的数学问题: 古代有一个梵塔,塔内有3个座A,B,C,开始时A座上有64个盘子,盘子大小不等,大的在下,小的在上(见图4.16)。有一个老和尚想把这64个盘子从A座移到C座,但每次只允许移动一个盘,且在移动过程中在3个座上都始终保持大盘在下,小盘在上。在移动过程中可以利用B座,要求编程序打印出移动的步骤。

【解】

#include <iostream>
using namespace std;
int main()
{
	void hanoi(int n, char one, char two, char three);
	int m;
	cout << " input the number of disks:";
	cin >> m;
	cout << " The steps of moving " << m << " disks:" << endl;
	hanoi(m, 'A', 'B', 'C');
	return 0;
}
void hanoi(int n, char one, char two, char three)
//将n个盘从one座借助two座移到three座
{
	void move(char x, char y);
	if (n == 1)  move(one, three);
	else
	{
		hanoi(n - 1, one, three, two);
		move(one, three);
		hanoi(n - 1, two, one, three);
	}
}
void move(char x, char y)
{
	cout << x << "->" << y << endl;
}

运行结果:

//input the number of disks : 4
//The steps of moving 4 disks :
//A→B
//A→C
//B→C
//A→B
//C→A
//C→B
//A→B
//A→C
//B→C
//B→A
//C→A
//B→C
//A→B
//A→C
//B→C

10.用递归法将一个整数n转换成字符串。例如,输入483,应输出字符串"483"。n的位数不确定,可以是任意位数的整数。

【解】

#include <iostream>
using namespace std;
int main()
{
	void convert(int n);
	int number;
	cout << "input an integer:";
	cin >> number;							//输入一个整数
	cout << "output:" << endl;
	if (number < 0)
	{
		cout << "–";
		number = -number;					//如果是负数, 把它变为正数再处理
	}
	convert(number);						//调用convert函数
	cout << endl;
	return 0;
}
void convert(int n)
{
	int i;
	char c;
	if ((i = n / 10) != 0)					//检查n是否为个位数
		convert(i);							//如果不是, 递归调用convert函数
	c = n % 10 + '0';
	cout << " " << c;
}

运行结果:

//input an integer : 345
//output : 3 4 5

11.用递归方法求f(x)=∑ni=1i2n的值由主函数输入。

【解】

#include <iostream>
using namespace std;
int main()
{
	int f(int);
	int n, s;
	cout << "input the number n:";
	cin >> n;
	s = f(n);
	cout << "The result is " << s << endl;
	return 0;
}
int f(int n)
{
	if (n == 1)
		return 1;
	else
		return (n*n + f(n - 1));
}

运行结果:

//input the number n : 5
//The result is 55

12.三角形的面积为area=s·(s-a)·(s-b)·(s-c) 其中,s=12(a+b+c),a,b,c为三角形的三边。定义两个带参数的宏,一个用来求s,另一个用来求area。编写程序,在程序中用带实参的宏名来求面积area。

【解】

#include <iostream>
#include <cmath>
using namespace std;
#define S(a,b,c)  (a+b+c)/2
#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)–a)*(S(a,b,c)–b)*(S(a,b,c)–c))
int main()
{
	float a, b, c;
	cout << "input a,b,c:";
	cin >> a >> b >> c;
	if (a + b > c && a + c > b && b + c > a)
		cout << "area=" << AREA(a, b, c) << endl;
	else
		cout << "It is not a triangle!" << endl;
	return 0;
}

运行结果:

//① input a, b, c:3 4 5
//area = 6
//② input a, b, c : 12 3 5
//It is not a triangle!

 

  • 22
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《C程序设计(第3)》是由谭浩强的一本经典的学习C语言程序设计的教材。本书分为13,内容涵盖了C语言的基础知识、语法规则、数据类型、控制语句、数组、字符串、指针、结构体、文件操作等内容。 本书采用了简洁明了的语言,结合了大量的示例代码和练习题,帮助读者逐步理解和掌握C语言的核心概念和编程技巧。无论是初学者还是有一定编程基础的人士,都可以通过学习本书快速上手C语言的编程。 《C程序设计(第3)》的电子除了提供书的内容外,还可以通过电子书的方式提供更多的学习辅助资源。例如,电子可以提供在线的习题答案和实例代码,方便读者进行复习和练习;同时还可以提供一些额外的学习资源,如C语言的常见错误和调试技巧等,帮助读者更好地理解和运用C语言。 与纸质书相比,电子的《C程序设计(第3)》具有可携带性和交互性的优势。读者可以随时随地通过电子设备进行阅读,无需携带厚重的纸质书籍。而且,电子还可以通过搜索功能迅速定位所需内容,方便读者快速查找和阅读。 总之,《C程序设计(第3)》是一本优秀的C语言学习教材,通过学习本书,读者可以系统地学习和掌握C语言的基础知识和编程技巧。通过电子,读者可以更加方便地学习和使用这本教材,提高学习效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值