提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


提示:以下是本篇文章正文内容,下面案例可供参考

3-2: 观察下面程序的输出

#include<iostream>

using namespace std;

int main(){
	int intOne;
	int &rSomeRef = intOne;
	
	intOne = 5;
	cout << intOne << endl;
	cout << rSomeRef << endl;
	
	int intTwo = 8;
	rSomeRef = intTwo;
	cout << intOne << endl;
	cout << intTwo << endl;
	cout << rSomeRef << endl;
}

运行输出:

5
5
8
8
8

前面说了,一旦一个引用被初始化后,就不能改为指向其他对象。但是代码中rSomeRef = intTwo;不是改为指向其他对象,这里相当于是intOne = intTwo;,因为rSomeRef 是 intOne 的引用(别名),所以rSomeRef = intTwo;等价于 intOne = intTwo; → 把 8 赋给 intOne

3-7: 完成函数:参数为两个unsigned short int 型数,返回值为第一个参数除以第二个参数的结果,数据类型为 short int ;如果第二个参数为0,则返回值为 -1,在主程序中实现输入输出。

#include<iostream>

using namespace std;

short int divide(unsigned short int& a, unsigned short int& b){
	if(b == 0){
		return -1;
	}
	else{
		return a/b;
	}
}

int main(){
	unsigned short int x;
	unsigned short int y;
	cin >> x;
	cin >> y;
	int result = divide(x, y);
	cout << result << endl;
}

3-8: 编写函数把华氏温度转换为摄氏温度

#include<iostream>

using namespace std;

float FtoC(float& f){
	float c = 5.0/9*(f-32); //这里一定是5.0/9或者5/9.0,否则输出永远是0
	return c;
}

int main(){
	float f;
	cout << "请输入华氏的温度:";
	cin >> f;
	float result = FtoC(f);
	cout << result << endl;
}

3-9 编写函数:判别一个数是否是质数,在主程序中实现输入输出

#include<iostream>
#include<cmath>

using namespace std;

int prime(int i);

int main(){
	int i ;
	cout << "请输入一个整数:" ;
	cin >> i;
	if(prime(i)){
		cout << i << "是质数" << endl;
	} 
	else{
		cout << i << "不是质数" << endl;
	}
	return 0;
}

int prime(int i){
	int k, j, flag;
	flag = 1;
	k = sqrt(i);
	for(j = 2; j <= k; j++){
		if(i % j == 0){
			flag = 0;
			break;
		}
	}
	return flag;
} 

为什么这个代码中定义函数的时候不需要引用呢?

首先值传递和引用传递有什么区别?
值传递:函数内部得到的是原变量的一个副本,修改这个副本不会影响外部变量
引用传递:函数内部得到的是原变量的别名,修改它会直接影响外部变量

在这个代码中,定义的函数是为了什么?
只是判断一个整数是否为质数,不需要修改输入a的值,所以不需要引用

什么时候需要引用?

  • 1.需要在函数内部修改外部变量时
  • 2.需要避免对大对象的拷贝开销时(比如后面写复杂代码的时候,常用到vector)这时通常会用到引用

3-10 编写函数:求两个整数的最大公约数和最小公倍数

求最大公约数,那我一寻思,求出来x和y的所有因子,然后比较一下,那不得了,好笨啊,蠢哭了

//求最大公约数
//先求出所有的因子getFactors,然后比较
#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

vector<int> getFactors(int k){  //求所有的因子
	vector<int> result;
	for(int i =1; i <= k; i++){
		if(k % i == 0){
			result.push_back(i);
		}
	}
	return result;
}

int main(){
	int x, y;
	int result = INT_MIN;
	cout << "请输入两个整数:";
	cin >> x;
	cin >> y;
	vector<int> result1 = getFactors(x);
	vector<int> result2 = getFactors(y);
	for(int j : result1){
		for(int i : result2){
			if(j == i){
				result = max(result, j);
			}
		}
	}
	cout << result;
} 

上面的方法虽然也对,但是太麻烦了,运行效率也不高,用欧几里得算法:

  • 比如求 gcd(48, 18):

    48 ÷ 18 = 2 … 12 → gcd(48, 18) = gcd(18, 12)

    18 ÷ 12 = 1 … 6 → gcd(18, 12) = gcd(12, 6)

    12 ÷ 6 = 2 … 0 → gcd(12, 6) = 6

    ✅ 所以最大公约数是 6。
    求两个数的最大公约数,就让两个数相除,然后除数 / 余数,知道余数为0,此次的除数就是最大公约数。

最小公倍数和最大公约数有一个关系是:最小公倍数= axb÷最大公约数

#include<iostream>

using namespace std;

int gcd(int a, int b){
	while(b != 0){
		int temp = a % b;
		a = b;
		b = temp;
	}
	return a;
}

int main(){
	int x, y;
	cout << "请输入两个整数:";
	cin >> x >> y;
	cout << "最大公约数:" << gcd(x, y) << endl;
	cout << "最小公倍数:" << x /gcd(x, y) * b << endl; //先除后乘,防止溢出 
}

3-12. 在主程序中提示输入整数n,编写函数用递归的方法求1+2+…+n的值

好简单的题目,但是我写不出来递归函数,回家吧孩子

  • 首先呢,递归三部曲是什么?

  • 1.确定传入的参数和返回值类型

    2.确定递归终止条件

    3.确定单层递归逻辑

前两个都是很简单的呀,但是第三个单层递归逻辑到底怎么写!!
单层递归逻辑就是当前层和下一层是什么关系?sum(n)和sum(n-1)是什么关系呢?
sum(n) = 1+2+…+(n-1)+n,sum(n-1) = 1+1+…+(n-1)
所以啊sum(n) = sum(n-1) + n啊!!

记住哦,单层递归逻辑就是要确定 “当前这一层要做的事+把剩下的交给子递归去做”

#include<iostream>

using namespace std;

int sum(int n){
	if(n == 1){
		return 1;
	}
	else{
		return n+sum(n-1);
	}
} 
int main(){
	int n;
	cout << "请输入一个整数:";
	cin >> n;
	cout << sum(n) << endl;
}

3-13 用递归的方法编写函数求Fibonacci级数,公式为:

Fn = Fn-1 + Fn-2(n>2),F1=F2=1

写的代码:

#include<iostream>

using namespace std;

int Fibonacci(int n){
	if(n==1 || n==2){
		return 1;
	}
	else{
		Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);
	}
	return Fibonacci(n);
} 
int main(){
	int n;
	cout << "请输入整数n:";
	cin >> n;
	cout << Fibonacci(n);
}

报错!!我真的很搞笑了,Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);虽然你的公式是这样写的,但是你代码怎么能这样写呢?Fibonacci是什么?是你定义的函数,你怎么调用这个函数然后还企图给他赋值??你在想什么??逆天

正确的代码:

#include<iostream>

using namespace std;

int Fibonacci(int n){
	if(n==1 || n==2){
		return 1;
	}
	else{
		return Fibonacci(n-1) + Fibonacci(n-2);
	}
} 
int main(){
	int n;
	cout << "请输入整数n:";
	cin >> n;
	cout << Fibonacci(n);
}

3-14 用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入输出,递归公式为:

#include<iostream>

using namespace std;

double Legendre(int n, int x){ //这里要用double啊,用int计算n=3,x=4结果是150,不对
	if(n == 0){
		return 1;
	}
	else if(n == 1){
		return x;
	}
	else{
		return ((2.0*n-1)*x*Legendre(n-1, x) - (n-1)*Legendre(n-2, x)) / n; //除以 n 可能得到小数结果
	} 
}
int main(){
	int n, x;
	cout << "请输入n和x:";
	cin >> n >> x;
	cout << Legendre(n, x) << endl;
}

3-15 编写递归函数getPower计算x^y,在同一程序中针对整型和实型实现两个重载的函数:

int getPower(int x, int y); //整型版本,当y<0时,返回0

double getPower(double x, int y); //实型版本

在主程序中实现输入输出,分别输入一个整数a和一个实数b作为底数,在输入一个整数m作为指数,输出a的m次方和b的m次方

#include<iostream>

using namespace std;

int getPower(int x, int y){
	if(y < 0){
		return 0;
	}
	else if(y == 0){
		return 1;
	}
	else{
		return getPower(x, y-1) * x;
	}
} 
double getPower(double x, int y){
	if(y < 0){
		return 1/getPower(x, -y);
	}
	else if(y == 0){
		return 1;
	}
	else{
		return getPower(x, y-1) * x;
	}
}
int main(){
	int a;
	cin >> a;
	double b;
	cin >> b;
	int m;
	cin >> m;
	cout << getPower(a, m) << endl;
	cout << getPower(b, m) << endl;
}

更多推荐