- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题如下:给定“ABC+DEF=GHI”格式字符串,其中 A、B、C 等代表唯一数字,找到给出最大 GHI 的表达式。例:输入字符串为AAB+AAB=AAB,则无解。如果是 AAA + BBB = AAA,则解为 999 + 000 = 999。另一个示例字符串:ABC + CBA = GGG,结果为 => 543 + 345 = 888。
我已经很容易地排除了不可能的情况。我想到的算法是一种蛮力,它只是先尝试最大化 rhs。然而,我的问题是这样做得很快,还要注意独特的数字。解决这个问题的有效方法是什么?
注意:我希望以单线程方法解决这个问题,我当前的问题是检测“assign_value”函数中是否使用了唯一数字。也许有更好的赋值方法?
编辑:根据 smci 的建议,这就是我最终想要实现的目标:ABRA + CADABRA + ABRA + CADABRA == HOUDINI ; 7457 + 1797457 + 7457 + 1797457 == 3609828 -- 一个系统不仅可以处理我在开头提供的形式的字符串(3 位数字 + 3 位数字 = 3 位数字),还可以处理那些。然而,从简单开始并采用我给出的格式解决方案并没有什么坏处:)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXPRESSION_SIZE 11 + 1
#define MAX_VARIABLES 9
int variables_read[MAX_VARIABLES] = { 0 };
struct variable {
int coefficient;
int* ptr;
int side;
int canhavezero;
unsigned value_max;
};
typedef struct variable Variable;
struct equation {
Variable* variables[9]; // max
unsigned distinct_on_rhs;
unsigned var_count;
};
typedef struct equation Equation;
int int_pow(int n, int k) {
int res = 1;
for(int i = 0; i < k; ++i)
res *= n;
return res;
}
void AddVariable(Equation* E, Variable* V) {
E->variables[E->var_count++] = V;
}
int IsImpossible(char* expression) {
// if all letters are same or end letters are same, no solution
if(
(expression[0] == expression[4] && expression[0] == expression[8]) ||
(!strncmp(expression, expression + 4, 3) && !strncmp(expression, expression + 8, 3))
)
return 1;
return 0;
}
int assign_value(Equation* E, int pos, int* values) {
if(!E->variables[pos]->value_count) {
if(pos < 0)
return 2;
// if no possible values left, reset this, but take one value count from the closest variable
E->variables[pos - 1]->value_count--;
E->variables[pos]->value_count = E->variables[pos]->value_max;
return 0;
}
int i;
for(i = 9; i >= 0 && values[i] == -1; --i)
printf("Assigning %d to %c\n", E->variables[pos]->value_set[E->variables[pos]->value_count - 1], 'A' + (E->variables[pos]->ptr - E->variables[0]->ptr));
*(E->variables[pos]->ptr) = values[i];
values[i] = -1; // we have unique numbers
return 0;
}
int isSolved(Equation E) {
int sum = 0, coeff = 0;
printf("Trying...\n");
for(int i = 0; i < E.var_count; ++i) {
coeff = E.variables[i]->coefficient * (*E.variables[i]->ptr);
printf("%d ", *E.variables[i]->ptr);
if(E.variables[i]->side)
coeff *= -1;
sum += coeff;
}
printf("\nSum was %d\n", sum);
return !sum;
}
char* evaluate(char* expression) {
char* res;
// check for impossible cases first
if(IsImpossible(expression)) {
res = (char *) malloc(sizeof(char) * strlen("No Solution!"));
strcpy(res, "No Solution!");
return res;
}
res = (char *) malloc(sizeof(char) * MAX_EXPRESSION_SIZE);
// now try to find solutions, first describe the given characters as equations
Equation E;
E.var_count = 0;
E.distinct_on_rhs = 0;
int side_mode = 0, powcounter = 0;
int a = -1, b = -1, c = -1, d = -1, e = -1, f = -1, g = -1, h = -1, i = -1;
int* max_variables[MAX_VARIABLES] = { &a, &b, &c, &d, &e, &f, &g, &h, &i };
for(int j = 0; j < MAX_EXPRESSION_SIZE - 1; ++j) {
if(expression[j] == '+')
continue;
if(expression[j] == '=') {
side_mode = 1;
continue;
}
Variable* V = (Variable *) malloc(sizeof(Variable));
// we know we always get 3 digit numbers but we can easily change if we need to
V->coefficient = int_pow(10, 2 - (powcounter % 3));
V->ptr = max_variables[expression[j] - 'A'];
V->side = side_mode;
E.distinct_on_rhs += side_mode && !variables_read[expression[j] - 'A'];
if(!(powcounter % 3)) { // beginning of a number
V->value_count = 9;
V->value_max = 9;
V->canhavezero = 0;
}
else {
V->value_count = 10;
V->value_max = 10;
V->canhavezero = 1;
}
AddVariable(&E, V);
variables_read[expression[j] - 'A'] = 1;
++powcounter;
}
for(int j = 0; j < E.var_count; ++j)
printf("%d %c %d\n", E.variables[j]->coefficient, 'A' + (E.variables[j]->ptr - max_variables[0]), E.variables[j]->side);
// we got a representaion of the equation, now try to solve it
int solved = 0;
// O(9^N), where N is number of distinct variables.
// An optimization we can do is, we first assign possible max values to rhs number, then go down. We need max number.
printf("Distincts: %d\n", E.distinct_on_rhs);
do {
// try to assign values to all variables and try if it solves the equation
// but first try to assign rhs as max as possible
int values[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int temp = E.var_count - E.distinct_on_rhs;
while(temp < E.var_count) {
solved = assign_value(&E, temp, values);
++temp;
}
for(int j = E.var_count - 1 - E.distinct_on_rhs; j >= 0; --j)
solved = assign_value(&E, j, values);
if(solved) // can return no solution
break;
printf("Solving...\n");
solved = isSolved(E);
system("PAUSE");
} while(!solved);
if(solved == 2) {
res = (char *) malloc(sizeof(char) * strlen("No Solution!"));
strcpy(res, "No Solution!");
}
else {
}
return res;
}
int main() {
char expression[MAX_EXPRESSION_SIZE] = { 0 };
do {
printf("Enter the formula: ");
scanf("%s", expression);
char* res = evaluate(expression);
printf("%s\n", res);
free(res);
} while(expression[0] != '-');
return 0;
}
最佳答案
我将从结果开始。没有那么多不同的情况:
AAA
AAB
, ABA
, BAA
ABC
所有其他情况都可以通过重命名变量来简化为这些情况。 ABC + CBA = GGG
将变为 DBC + CBD = AAA
。
那么你有
AAA
的 10 种可能的解决方案 假设任何地方都允许零。如果没有,您可以过滤掉那些不允许的。
这会设置等式右侧的变量。每个仅出现在左侧的变量都会添加可能的解决方案。 B
增加了 9 个因子,C
增加了 8 个因子,D
增加了 7 等等。
关于c - 密码算法求解器分解的效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40294834/
我正在尝试在 R 中计算任意 N x J 矩阵 S 的投影矩阵 P: P = S (S'S) ^ -1 S' 我一直在尝试使用以下函数来执行此操作: P 概述 solve 基于一般方阵的 LU 分解
所以我有一个包含数千行的非常旧的文件(我猜是手工生成的),我正试图将它们移动到一个 rdb 中,但是这些行没有转换为列的格式/模式。例如,文件中的行如下所示: blah blahsdfas
这实际上只是一个“最佳实践”问题...... 我发现在开发应用程序时,我经常会得到很多 View 。 将这些 View 分解为几个 View 文件是常见的做法吗?换句话说......而不只是有view
使用以下函数foo()作为简单示例,如果可能的话,我想将...中给出的值分配给两个不同的函数。 foo args(mapply) function (FUN, ..., MoreArgs = NUL
正面案例:可以进入列表 groovy> println GroovySystem.version groovy> final data1 = [[99,2] , [100,4]] groovy> d
省略素数计算方法和因式分解方法的详细信息。 为什么要进行因式分解? 它的应用是什么? 最佳答案 哇,这个线程里有这么多争斗。 具有讽刺意味的是,这个问题有一个主要的有效答案。 因式分解实际上在加密/解
术语“分解不良”和“重构”程序是什么意思?你能举一个简单的例子来理解基本的区别吗? 最佳答案 重构是一种通用技术,可以指代许多任务。它通常意味着清理代码、去除冗余、提高代码质量和可读性。 分解不良代码
我以前有,here ,表明 C++ 函数不容易在汇编中表示。现在我有兴趣以一种或另一种方式阅读它们,因为 Callgrind 是 Valgrind 的一部分,在组装时显示它们已损坏。 所以我想要么破坏
最初,我一直在打开并同时阅读两个文件,内容如下: with open(file1, 'r') as R1: with open(file2, 'r') as R2: ### m
我正在尝试摆脱 标签和标签内的内容使用 beatifulsoup。我去看了文档,似乎是一个非常简单的调用函数。有关该功能的更多信息是 here .这是我到目前为止解析的 html 页面的内容...
给定一个 float ,我想将它分成几个部分的总和,每个部分都有给定的位数。例如,给定 3.1415926535 并要求将其分成以 10 为基数的部分,每部分 4 位数字,它将返回 3.141 + 5
我的 JSF 项目被部署为一个 EAR 文件。它还包括一些 war 文件。我需要 EAR 的分解版本(包括分解的内部 WAR)。 有什么工具可以做到吗? 最佳答案 以编程方式还是手动? EAR 和 W
以下函数不使用行透视进行 LU 分解。 R 中是否有一个现有的函数可以使用行数据进行 LU 分解? > require(Matrix) > expand(lu(matrix(rnorm(16),4,4
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 7年前关闭。 Improve this
我正在使用登记数据进行病假研究。从登记册上,我只得到了每个人的病假开始日期和结束日期。但日期并没有逐年分割。例如,对于人 A,只有开始日期 (1-may-2016) 和结束日期 (14-feb-201
我发现以下 R 代码使用 qr 因式分解无法恢复原始矩阵。我不明白为什么。 a <- matrix(runif(180),ncol=6) a[,c(2,4)] <- 0 b <- qr(a) d <-
我正在尝试检测气候数据时间序列中的异常值,其中一些缺失的观测值。在网上搜索我发现了许多可用的方法。其中,STL 分解似乎很有吸引力,因为它去除了趋势和季节性成分并研究了其余部分。阅读 STL: A S
我想使用 javascript 分解数组中的 VIN,可能使用正则表达式,然后使用某种循环... 以下是读取 VIN 的方法: http://forum.cardekho.com/topic/600-
我正在研究 Databricks 示例。数据框的架构如下所示: > parquetDF.printSchema root |-- department: struct (nullable = true
我正在尝试简化我的代码并将其分解为多个文件。例如,我设法做到了: socket.once("disconnect", disconnectSocket); 然后有一个名为 disconnectSock
我是一名优秀的程序员,十分优秀!