跳到正文

个人记录

小游戏 · 11 蒟蒻的24点游戏攻略

xutongwei约 7 分钟
原文
文章信息与版本

文章信息

发布
2019年10月6日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 7 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <string>
#include <cmath>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int n, m = 0, sum = 0;
double a[20];
double ans[20][5];
string fu[10] = {" 0 ", " + ", " - ", " * ", " / "};

void output(){
	for(int i = 1; i <= m; i++){
		cout << ans[i][1] << fu[(int)round(ans[i][2])] << ans[i][3] << " = " << ans[i][4] << endl;
	}
	cout << endl;
	sum++;
	return;
}

void zuo(int n1, double b[]){
	if(n1 <= 1){
		if(abs(24 - b[1]) < 0.000000001){
			output();
		}
		return;
	}
	for(int i = 1; i <= n1; i++){
		for(int j = i + 1; j <= n1; j++){
			double c[20];
			for(int k = 1; k <= n1; k++){
				c[k] = b[k];
			}
			double ii = c[i];
			double jj = c[j];
			c[i] = ii + jj;
			for(int k = j; k < n1; k++){
				c[k] = c[k + 1];
			}
			c[n1] = 0;
			m++;
			ans[n - n1 + 1][1] = ii;
			ans[n - n1 + 1][2] = 1;
			ans[n - n1 + 1][3] = jj;
			ans[n - n1 + 1][4] = c[i];
			zuo(n1 - 1, c);
			c[i] = ii - jj;
			ans[n - n1 + 1][1] = ii;
			ans[n - n1 + 1][2] = 2;
			ans[n - n1 + 1][3] = jj;
			ans[n - n1 + 1][4] = c[i];
			zuo(n1 - 1, c);
			c[i] = jj - ii;
			ans[n - n1 + 1][1] = jj;
			ans[n - n1 + 1][2] = 2;
			ans[n - n1 + 1][3] = ii;
			ans[n - n1 + 1][4] = c[i];
			zuo(n1 - 1, c);
			c[i] = ii * jj;
			ans[n - n1 + 1][1] = ii;
			ans[n - n1 + 1][2] = 3;
			ans[n - n1 + 1][3] = jj;
			ans[n - n1 + 1][4] = c[i];
			zuo(n1 - 1, c);
			c[i] = ii / jj;
			ans[n - n1 + 1][1] = ii;
			ans[n - n1 + 1][2] = 4;
			ans[n - n1 + 1][3] = jj;
			ans[n - n1 + 1][4] = c[i];
			zuo(n1 - 1, c);
			c[i] = jj / ii;
			ans[n - n1 + 1][1] = jj;
			ans[n - n1 + 1][2] = 4;
			ans[n - n1 + 1][3] = ii;
			ans[n - n1 + 1][4] = c[i];
			zuo(n1 - 1, c);
			m--;
		}
	}
	return;
}

int main( ){
	Sleep(1500) ;
	cout << "这里是24点咨询平台。" << endl;
	Sleep(1500) ;
	cout << "请严格按照步骤执行操作。" << endl; 
	Sleep(1500) ;
	cout << "1.输入一个数字n:" << endl;
	cin >> n;
	Sleep(1500) ;
	cout << "2.输入n个数字" << endl;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
	}
	Sleep(1500) ;
	cout << "3.静静地等着……" << endl; 
	Sleep(1500) ;
	srand(time(NULL));
	int m = (rand() % 16) + 5;
	while(m--){
		system("cls");
		Sleep(150);
		cout << "滴" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查询" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查询中" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查询中!" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查询中!请" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查询中!请稍" << endl;
		Sleep(150);
		system("cls");
		cout << "滴滴!正在查询中!请稍后" << endl;
		Sleep(500);
		system("cls");
		cout << "滴滴!正在查询中!请稍后▁" << endl;
		Sleep(500);
		system("cls");
		cout << "滴滴!正在查询中!请稍后▁▂" << endl;
		Sleep(500);
		system("cls");
		cout << "滴滴!正在查询中!请稍后▁▂▃" << endl;
		Sleep(500);
		system("cls");
		cout << "滴滴!正在查询中!请稍后▁▂▃▅" << endl;
		Sleep(500);
		system("cls");
		cout << "滴滴!正在查询中!请稍后▁▂▃▅▆" << endl;
		Sleep(500);
		system("cls");
		cout << "滴滴!正在查询中!请稍后▁▂▃▅▆▇" << endl;
		Sleep(500);
	}
	system("cls");
	zuo(n, a);
	cout << sum << endl; 
	system("pause");
	return 0;
}
原文