2024春晚刘谦魔术代码实现c++

用数组来实现魔术代码,简单易懂,分享给大家,祝大家新年快乐!!

#include
#include
#include 
using namespace std;
const int N = 8;
char ch[N], arry[N];;
int num;

int main()
{
	cout << "输入随意的四张牌" << endl;
	for (int i = 0;i < 4;i++) {
		cin >> ch[i];
	}
	for (int i = 4;i < N;i++) {
		ch[i] = ch[i - 4];
	}
	cout << "输入你名字的个数" << endl;
	cin >> num;

	for (int i = 0;i < num;i++) {
		arry[i] = ch[i];
	}
	for (int i = 0;i < N - num;i++) {
		ch[i] = ch[i + num];
	}
	for (int i = N - num;i < N;i++) {
		ch[i] = arry[i - N + num];
	}
	

	cout << "从顶上拿三张牌随意的插到中间" << endl;
	int x = (rand() % 4) + 1;//1-4的数
	for (int i = 0;i < 3;i++) {
		arry[i] = ch[i];
		ch[i] = ch[i + 3];
	}
	for (int i = x + 1;i < N - x;i++) {
		ch[i] = ch[i + 3];
	}
	for (int i = x + 1;i < x + 4;i++) {

		ch[i] = arry[i - x - 1];
	}
	cout << "最顶上的牌压到屁股底下" << endl;
	char ass = ch[0];
	for (int i = 0;i < N;i++) {
		ch[i] = ch[i + 1];
	}//前七位有效
	ch[N - 1] = NULL;
	
	

	


	cout << "如果你是南方人,就从牌顶拿1张牌放到中间。如果你是北方人,放2张" << endl;
	string str;
	cout << "输入南方人或北方人" << endl;
	cin >> str;
	if (str == "南方人") {

		char op = ch[0];
		for (int i = 0;i < 5;i++) {
			ch[i] = ch[i + 1];
		}
		ch[5] = op;
	}

	if (str == "北方人") {

		char o = ch[0];
		char p = ch[1];
		for (int i = 0;i < 4;i++) {
			ch[i] = ch[i + 2];
		}
		ch[4] = o;
		ch[5] = p;
	}
	ch[7] = NULL;


	cout << "如果你是男生,从最上面拿1张洒到空中。如果你是女生,从最上面拿2张洒到空中" << endl;
	cin >> str;
	if (str == "男生") {
		for (int i = 0;i < 7;i++) {
			ch[i] = ch[i + 1];
		}
		ch[6] = NULL;
	}
	if (str == "女生") {
		for (int i = 0;i < 6;i++) {
			ch[i] = ch[i + 2];
		}
		ch[5] = ch[6] = NULL;
	}

	cout << "口中念“见证奇迹的时刻”7个字,每念一次,把一张牌从上面拿到底层" << endl;
	int nn = 7;
	if (str == "男生") {
		while (nn--) {
			char oo = ch[0];
			for (int i = 0;i < 5;i++) {
				ch[i] = ch[i + 1];
			}
			ch[5] = oo;
		}
	}
	else {
		while (nn--) {
			char oo = ch[0];
			for (int i = 0;i < 4;i++) {
				ch[i] = ch[i + 1];
			}
			ch[5] = oo;
		}

	}
	cout << "口中喊“好运留下来!”并把顶上的牌放到底层。再喊“烦恼丢出去!”把顶上的牌扔到空中" << endl;
	if (str == "男生") {
		int nums = 6;
		while (nums != 1) {
			char oo = ch[0];
			for (int i = 0;i < nums - 1;i++) {
				ch[i] = ch[i + 1];
			}
			ch[nums - 1] = oo;
			for (int i = 0;i < nums - 1;i++) {
				ch[i] = ch[i + 1];
			}
			ch[nums - 1] = NULL;
			nums--;
		}
	}
	else {
		int nums = 5;
		while (nums != 1) {
			char oo = ch[0];
			for (int i = 0;i < nums - 1;i++) {
				ch[i] = ch[i + 1];
			}
			ch[nums - 1] = oo;
			for (int i = 0;i < nums - 1;i++) {
				ch[i] = ch[i + 1];
			}
			ch[nums - 1] = NULL;
			nums--;
		}
	}
	cout << "手里:"<

2024春晚刘谦魔术代码实现c++_第1张图片

你可能感兴趣的:(c++,算法,数据结构)