CCF 2019-03-2 二十四点 极简写法递归 100分

题意简单就不多说了直接上代码

// 二十四点.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int n;

int fun(int a, string s) {
	if (s.size() == 0)return a;
	char f = s[0];
	int b = s[1] - '0';
	s = s.substr(2);
	if (f == '+' || f == '-') 
		return f == '+' ? a + fun(b, s) : a + fun(b*-1, s);
	else 
		return f == 'x' ? fun(a * b, s) : fun(a / b, s);
}

int main()
{
	cin >> n;
	cin.get();
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		vstr.push_back(s);
		if (fun(s[0] - '0', s.substr(1)) == 24)
			cout << "Yes" << endl;
		else 
			cout << "No" << endl;
	}
}
发布了8 篇原创文章 · 获赞 6 · 访问量 537

猜你喜欢

转载自blog.csdn.net/qq_41433566/article/details/105459234