2021-10-24

本文档包含三个C++编程任务:1) 实现求两个或三个数的最大值函数;2) 模拟射击对决,根据概率决定胜负;3) 创建一个函数将两个动态整数数组拼接。每个任务都有详细代码实现,并在主函数中进行测试和展示结果。
摘要由CSDN通过智能技术生成

C++第一次实验

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


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


task 1 求最大数

Write an overloaded function max that takes either two or three parameters of type double and returns the largest of them.

#include <iostream>
using namespace std;



double max(double x, double y, double z) {
	double a;


	if (x > y) {
		if (x> z) {
			a= x ;
		}
		else {

			a=z ;

		}
	}

	else {
		if (y > z) {

			a= y;
		}
		else {

			a= z ;

		}



	}
	return a;
}


double max(double x, double y) {
	double a;


	if (x > y) {
		a = x;
	}
	else {
		a = y;
	}
	return a;
}

int main() {

	//max(3.3, 6.6, 9.9);
	double x, y, z;
	cout << "请输入你想要比较大小的三个值" << endl;
	cin >> x >> y >> z;
	cout << "你输入的三个值为" << x << ' ' << y << ' ' << z << endl;
	double a, b;

	a= max(x,y,z);
	cout << "三个值中最大的数为" << a<<endl;

	b = max(x, y);
	cout << "前两个值中最大的数为" << b << endl;
	

	return 0;



}


task 2 射击对决

2.In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.
To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.

a. Write a function to simulate a single shot. It should use the following declaration: void shoot(bool& targetAlive, double accuracy); This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy, then the target is hit and targetAlive should be set to false.
For example, if Bob is shooting at Charlie, this could be invoked as: shoot(charlieAlive, 0.5);
Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.

b. An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

c. In your main function, invoke the startDuel function 1000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.

d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

#include<iostream>;
#include<cstdlib>
#include<time.h> 
using namespace std;


void shoot(bool& targetAlive, double accuracy) {
	
	double a;
	a = rand() % 100;
	a = a / 100;

	cout << "此轮随机射击的准确率为" << a << endl;
	if (accuracy == 0.33) {
		cout << "a开始射击" << endl;
		if (a < 0.33) {
			cout << "当a射击b或者c时,则b或c出局" << endl;
			targetAlive = 0;




		}
		else {
			cout << "a一个都没射中" << endl;
		}


	}
	else {
		if (accuracy == 0.5) {
			cout << "b开始射击" << endl;
			if (a < 0.5) {
				cout << "当b射击a或者c时,则a或c出局" << endl;
				targetAlive = 0;




			}
			else {
				cout << "b一个都没射中" << endl;
			}


		}

		else
		{
			cout << "c开始射击" << endl;
			if (a < 1) {
				cout << "当c射击a或者b时,则a或b出局" << endl;
				targetAlive = 0;




			}
			else {
				cout << "c一个都没射中" << endl;
			}


		}
	}

}
//判断谁最后赢得了比赛
char whoWin(bool& targetAlive1, bool& targetAlive2, bool& targetAlive3) {

	if (targetAlive1 == 1) {
		cout << "a赢得了最后的比赛" << endl;
		return 'a';

	}
	else {
		if (targetAlive2 == 1) {
			cout << "b赢得了最后的比赛" << endl;
			return 'b';
		}
		else {
			cout << "c赢得了最后的比赛" << endl;
			return 'c';
		}
	}


}

//开始模拟对决
char startDuel(bool& targetAlive1, bool& targetAlive2, bool& targetAlive3) {
	int i = 1;
	double accuracy = 0.0;
	while ((targetAlive1 == 1 && targetAlive2 == 1) || (targetAlive1 == 1 && targetAlive3 == 1) || (targetAlive2 == 1 && targetAlive3 == 1)) {
		if (i % 3 == 1) {
			if (targetAlive1 == 1) {
				if (targetAlive3 == 1) {
					
					accuracy = 0.33;
					shoot(targetAlive3, accuracy);
					cout << "此轮a射击的人是c" << endl;
					cout << "\n" << endl;
					i = i + 1;
				}
				else {
					if (targetAlive2 == 1) {
						
						accuracy = 0.33;
						shoot(targetAlive2, accuracy);
						cout << "此轮a射击的人是b" << endl;
						cout << "\n" << endl;
						i = i + 1;
					}
					else {
						cout << "a win";
						cout << "\n" << endl;
						i = i + 1;
					}

				}



			}
			else {
				cout << "a已经出局" << endl;
				cout << "\n" << endl;
				i = i + 1;
			}
		}
		else {
			if (i % 3 == 2) {
				if (targetAlive2 == 1) {
					if (targetAlive3 == 1) {
						
						accuracy = 0.5;
						shoot(targetAlive3, accuracy);
						cout << "此轮b射击的人是c" << endl;
						cout << "\n" << endl;
						i = i + 1;
					}
					else {
						if (targetAlive1 == 1) {
							
							accuracy = 0.5;
							shoot(targetAlive1, accuracy);
							cout << "此轮b射击的人是a" << endl;
							cout << "\n" << endl;
							i = i + 1;
						}
						else {
							cout << "b win";
							cout << "\n" << endl;
							i = i + 1;
						}

					}



				}
				else {
					cout << "b已经出局" << endl;
					cout << "\n" << endl;
					i = i + 1;
				}





			}
			else {
				{
					if (targetAlive3 == 1) {
						if (targetAlive2 == 1) {
							accuracy = 1.0;
							
							shoot(targetAlive2, accuracy);
							cout << "此轮c射击的人是a" << endl;
							cout << "\n" << endl;
							i = i + 1;
						}
						else {
							if (targetAlive1 == 1) {
								
								accuracy = 1.0;
								shoot(targetAlive1, accuracy);
								cout << "此轮c射击的人是a" << endl;
								cout << "\n" << endl;
								i = i + 1;
							}
							else {
								cout << "c win";
								cout << "\n" << endl;
								i = i + 1;
							}

						}



					}
					else {
						cout << "c已经出局" << endl;
						cout << "\n" << endl;
						i = i + 1;
					}





				}
			}
		}


	}


	return whoWin(targetAlive1, targetAlive2, targetAlive3);


}


char CounterIntuitiveDuel(bool& targetAlive1, bool& targetAlive2, bool& targetAlive3) {
	
	cout << "第一轮开始" << "\t" << "a故意射偏" << "\n" << endl;
	int i = 2;
	double accuracy = 0.0;
	while ((targetAlive1 == 1 && targetAlive2 == 1) || (targetAlive1 == 1 && targetAlive3 == 1) || (targetAlive2 == 1 && targetAlive3 == 1)) {
		
		if (i % 3 == 1) {
			if (targetAlive1 == 1) {
				if (targetAlive3 == 1) {

					accuracy = 0.33;
					shoot(targetAlive3, accuracy);
					cout << "此轮a射击的人是c" << endl;
					cout << "\n" << endl;
					i = i + 1;
				}
				else {
					if (targetAlive2 == 1) {

						accuracy = 0.33;
						shoot(targetAlive2, accuracy);
						cout << "此轮a射击的人是b" << endl;
						cout << "\n" << endl;
						i = i + 1;
					}
					else {
						cout << "a win";
						cout << "\n" << endl;
						i = i + 1;
					}

				}



			}
			else {
				cout << "a已经出局" << endl;
				cout << "\n" << endl;
				i = i + 1;
			}
		}
		else {
			if (i % 3 == 2) {
				if (targetAlive2 == 1) {
					if (targetAlive3 == 1) {

						accuracy = 0.5;
						shoot(targetAlive3, accuracy);
						cout << "此轮b射击的人是c" << endl;
						cout << "\n" << endl;
						i = i + 1;
					}
					else {
						if (targetAlive1 == 1) {

							accuracy = 0.5;
							shoot(targetAlive1, accuracy);
							cout << "此轮b射击的人是a" << endl;
							cout << "\n" << endl;
							i = i + 1;
						}
						else {
							cout << "b win";
							cout << "\n" << endl;
							i = i + 1;
						}

					}



				}
				else {
					cout << "b已经出局" << endl;
					cout << "\n" << endl;
					i = i + 1;
				}





			}
			else {
				{
					if (targetAlive3 == 1) {
						if (targetAlive2 == 1) {
							accuracy = 1.0;

							shoot(targetAlive2, accuracy);
							cout << "此轮c射击的人是a" << endl;
							cout << "\n" << endl;
							i = i + 1;
						}
						else {
							if (targetAlive1 == 1) {

								accuracy = 1.0;
								shoot(targetAlive1, accuracy);
								cout << "此轮c射击的人是a" << endl;
								cout << "\n" << endl;
								i = i + 1;
							}
							else {
								cout << "c win";
								cout << "\n" << endl;
								i = i + 1;
							}

						}



					}
					else {
						cout << "c已经出局" << endl;
						cout << "\n" << endl;
						i = i + 1;
					}





				}
			}
		}


	}


	return whoWin(targetAlive1, targetAlive2, targetAlive3);


}









//模拟对局
void simulatDuel(int i) {
	int aWin = 0;
	int bWin = 0;
	int cWin = 0;
	
	char count;
	for (int j = 0; j < i; j++) {
		bool aAlive = 1, bAlive = 1, cAlive = 1;
		//count = startDuel(aAlive, bAlive, cAlive);
		
		count = CounterIntuitiveDuel(aAlive, bAlive, cAlive);
		if (count == 'a') {
			aWin++;
		}
		else {
			if (count == 'b') {
				bWin++;
			}
			else {
				cWin++;
			}
		}
		
	}
	double a_Winrate = aWin/double(i);
	double b_Winrate = bWin / double(i);
	double c_Winrate = cWin / double(i);
	//cout << a_Winrate;
	cout << '\n' << endl;
	cout << "在这些模拟对决中a赢的次数为" << aWin << "\t" << "比重为" << a_Winrate<<"\n" << endl;
	cout << "在这些模拟对决中b赢的次数为" << bWin << "\t" << "比重为" << b_Winrate << "\n" << endl;
	cout << "在这些模拟对决中c赢的次数为" << cWin << "\t" << "比重为" << c_Winrate<< "\n" << endl;




}





int main() {
	char a;
	srand(time(NULL));
	bool aAlive = 1, bAlive = 1, cAlive = 1;
	//shoot(aAlive, 0.5);

	int aWin = 0, bWin = 0, cWin = 0;
	//a=CounterIntuitiveDuel(aAlive, bAlive, cAlive);
	//simulatDuel(int i, bool& targetAlive1, bool& targetAlive2, bool& targetAlive3)
	
	//a=startDuel(aAlive, bAlive, cAlive);
	//cout << a << endl;

	simulatDuel(1000);
	/*for (int j = 0; j < 1000; j++) {
		bool aAlive = 1, bAlive = 1, cAlive = 1;
		a = startDuel(aAlive, bAlive, cAlive);
		if (a == 'a') {
			aWin++;
		}
		else {
			if (a == 'b') {
				bWin++;
			}
			else {
				cWin++;
			}
		}
	}
		cout << aWin << endl;
		cout << bWin << endl;
		cout << cWin << endl;*/


}

task 3 复制动态数组

3.Write a function to concatenate the content of two dynamic integer arrays. Your function should contain parameters for two reference pointers to the arrays and the size of both arrays. Your function should return a pointer to a new array which contains a copy of the elements of both arrays. Test your function using a driver program.

#include<iostream>
using namespace std;
//创造数组
int* CreatDynamArray() {

	int num;
	cout << "你想要创建多少长度的数组" << endl;
	cin >> num;
	int* score;
	score = new int[num];
	cout << "请输入你想要创建的数组的值" << endl;
	for (int i = 0; i < num; i++) {
		cout << "score [" << i << "]" << endl;
		cin >> score[i];
	}

	return score;

};
//拼接数组
int* concatDyamArray( int* score1, int* score2 ,  int num1, int num2) {
	int num3 = num1 + num2;
	int* score3 = new int[num3];
	int i = 0;
		for (int j = 0; j < num1; j++) {
			score3[i] = score1[j];
			i = i + 1;
		}

	
		for (int z = 0; z < num2; z++) {
			score3[i] = score2[z];
			i = i + 1;
		}


		return score3;

}

void PrintArray(int* score, int num) {
	cout << "数组里面的元素为" << endl;
	for (int i = 0; i < num; i++) {
		cout << score[i] << endl;
		
	}
}

int main() {
	/*int num;
	cin >> num;
	int* score;
	score = new int[num];
	for (int i = 0; i < num; i++) {
		cout << "score [" << i << "]" << endl;
		cin >> score[i];
	}*/

	int* score1;
	int* score2;
	int* score3;
	score1 = CreatDynamArray();
	score2 = CreatDynamArray();
	
	
	int num1 = _msize(score1) / sizeof(score1[0]);
	
	int num2 = _msize(score2) / sizeof(score2);
	cout << "输出第一个数组里面的元素" << endl;
	PrintArray(score1, num1);
	cout << "输出第二个数组里面的元素" << endl;
	PrintArray(score2, num2);
	
	score3 = concatDyamArray(score1, score2,  num1,  num2);
	int num3 = _msize(score3) / sizeof(score3[0]);
	//cout << num1 << "\t" << num2 << "\t" << num3 << endl;
	cout << "输出第三个数组里面的元素" << endl;
	PrintArray(score3, num3);


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值