Skip to content

Commit

Permalink
docs: modify README
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans774882968 committed Dec 12, 2023
1 parent 4cd6ade commit fcb4e2e
Showing 1 changed file with 4 additions and 369 deletions.
373 changes: 4 additions & 369 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

对于`0 0 0``10 10 10`这11组数,请填入运算符凑出6。允许使用的二元运算符包括`** * / % + - << >> & | ^`,允许使用的一元运算符包括`sqrt, fac`(根号、阶乘),允许使用括号。每一次的二元运算结果都可以使用至多一次一元运算符,比如`((0! + 0!)! + 0!)! = 6`是合法的。其中除法需要除得尽才合法。

[本文GitHub传送门](https://github.com/Hans774882968/apple-interview-24point-similar)

## 表达式树介绍

24点的dfs比较好理解,但如果没有专门学习过很难自己想出来。首先我们要了解**表达式树**。以下面这棵表达式树为例:

```cpp
```
*
/ \
1 +
Expand Down Expand Up @@ -325,372 +327,5 @@ int main(int, char **) {
}
```

`v2`

```cpp
#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
// Copyright 2023 hans7

const int N = 3, M = 11;
bool vis[N];
int p3[N + 1];
LL fac[M], a[N];
string ans[N];
set<string> ansSet;

void dbg() { puts(""); }
template <typename T, typename... R>
void dbg(const T &f, const R &...r) {
cout << f << " ";
dbg(r...);
}

bool powTooLarge(LL x, LL y) {
if (y >= 63) return x >= 2;
return y * log(x) >= 43.66827237527655;
}

bool isPerfectSquare(LL v) {
LL root = sqrt(v);
return root * root == v;
}

bool dfs(int dep) {
if (dep + 1 == N) {
for (int i = 0; i < N; i++) {
if (!vis[i]) {
if (a[i] == 6) {
ansSet.insert(ans[i]);
}
return a[i] == 6;
}
}
return false;
}
bool fl = false;
for (int i = 0; i < N; i++) {
if (vis[i]) continue;
for (int j = i + 1; j < N; j++) {
if (vis[j]) continue;
vis[j] = true;
LL x = a[i], y = a[j];
string tmpAnsI = ans[i], tmpAnsJ = ans[j];

if (!powTooLarge(x, y)) {
a[i] = round(pow(x, y));
ans[i] = "(" + tmpAnsI + ") ** (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
LL originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") ** (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") ** (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
}
if (!powTooLarge(y, x)) {
a[i] = round(pow(y, x));
ans[i] = "(" + tmpAnsJ + ") ** (" + tmpAnsI + ")";
if (dfs(dep + 1)) fl = true;
LL originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsJ + ") ** (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsJ + ") ** (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
}

if (y && x % y == 0) {
a[i] = x / y;
ans[i] = "(" + tmpAnsI + ") / (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
LL originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") / (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") / (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
}
if (x && y % x == 0) {
a[i] = y / x;
ans[i] = "(" + tmpAnsJ + ") / (" + tmpAnsI + ")";
if (dfs(dep + 1)) fl = true;
LL originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsJ + ") / (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsJ + ") / (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
}

LL originalTmpAI = a[i];
a[i] = x * y;
ans[i] = "(" + tmpAnsI + ") * (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") * (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") * (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}

if (y) {
a[i] = x % y;
ans[i] = "(" + tmpAnsI + ") % (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
LL originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") % (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") % (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
}
if (x) {
a[i] = y % x;
ans[i] = "(" + tmpAnsJ + ") % (" + tmpAnsI + ")";
if (dfs(dep + 1)) fl = true;
LL originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsJ + ") % (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsJ + ") % (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
}

a[i] = x + y;
ans[i] = "(" + tmpAnsI + ") + (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") + (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") + (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x - y;
ans[i] = "(" + tmpAnsI + ") - (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") - (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") - (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
a[i] = y - x;
ans[i] = "(" + tmpAnsJ + ") - (" + tmpAnsI + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsJ + ") - (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsJ + ") - (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x << y;
ans[i] = "(" + tmpAnsI + ") << (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") << (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") << (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
a[i] = y << x;
ans[i] = "(" + tmpAnsJ + ") << (" + tmpAnsI + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsJ + ") << (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsJ + ") << (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x >> y;
ans[i] = "(" + tmpAnsI + ") >> (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") >> (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") >> (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
a[i] = y >> x;
ans[i] = "(" + tmpAnsJ + ") >> (" + tmpAnsI + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsJ + ") >> (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsJ + ") >> (" + tmpAnsI + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x & y;
ans[i] = "(" + tmpAnsI + ") & (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") & (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") & (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x | y;
ans[i] = "(" + tmpAnsI + ") | (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") | (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") | (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x ^ y;
ans[i] = "(" + tmpAnsI + ") ^ (" + tmpAnsJ + ")";
if (dfs(dep + 1)) fl = true;
originalTmpAI = a[i];
if (0 <= originalTmpAI && originalTmpAI < M) {
a[i] = fac[originalTmpAI];
ans[i] = "fac((" + tmpAnsI + ") ^ (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}
if (isPerfectSquare(originalTmpAI)) {
a[i] = sqrt(originalTmpAI);
ans[i] = "sqrt((" + tmpAnsI + ") ^ (" + tmpAnsJ + "))";
if (dfs(dep + 1)) fl = true;
}

a[i] = x;
ans[i] = tmpAnsI;
vis[j] = false;
}
}
return fl;
}

int main(int, char **) {
fstream file;
file.open("apple_interview_24point_similar-v2.txt", ios::out);
streambuf *stream_buffer_out = cout.rdbuf();
cout.rdbuf(file.rdbuf());
for (int i = 0; i <= N; i++) p3[i] = i ? p3[i - 1] * 3 : 1;
for (int i = 0; i < M; i++) fac[i] = i ? fac[i - 1] * i : 1;
for (int i = 0; i < M; i++) {
ansSet.clear();
bool hasAns = false;
for (int S = 0; S < p3[N]; S++) {
for (int j = 0; j < N; j++) {
int v = 0;
string strV;
int sVal = S / p3[j] % 3;
if (sVal == 1 && isPerfectSquare(i)) {
v = sqrt(i);
strV = "sqrt(" + to_string(i) + ")";
} else if (sVal == 2) {
v = fac[i];
strV = "fac(" + to_string(i) + ")";
} else {
v = i;
strV = to_string(i);
}
a[j] = v;
ans[j] = strV;
}
hasAns |= dfs(0);
}
cout << i << " " << hasAns << endl;
cout << "ansSet.size() = " << ansSet.size() << endl;
string jsCode =
"let fac = (v) => v > 0 ? v * fac(v - 1) : 1, { sqrt } = Math;\n[";
for (auto v : ansSet) {
jsCode += v + ", ";
}
jsCode += "].every((v) => v === 6);";
cout << jsCode << endl;
}
cout.rdbuf(stream_buffer_out);
file.close();
return 0;
}
```
`v2`代码过长,在此不贴出,[传送门](https://github.com/Hans774882968/apple-interview-24point-similar/blob/main/apple_interview_24point_similar-v2.cpp)

0 comments on commit fcb4e2e

Please sign in to comment.