由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Computation版 - Interview Question
相关主题
[请教]uniformly random sampling矩阵问题求解
Fortran里面哪个函数显示系统时间?Please help!
求矩阵逆的算法[转载] 一串32-bit integers,要数bit-1的总数?
Fortran 77 dynamic memory allocation 1[转载] Re: 未婚证明求救
谁熟悉cplex的integer programming谁能帮我把这个fortran函数接口写成C的形式,
look for a software[转载] 一个gnuplot的问题
Mixed Integer Programminga question about Matlab fread
a question about Theta(nlgn)[合集] 有关fortran返回数组的问题!
相关话题的讨论汇总
话题: expression话题: question话题: interview话题: operators话题: int
进入Computation版参与讨论
1 (共1页)
M**8
发帖数: 25
1
Hi,
I was being asked the question below long time ago. Still I cannot figure
out the problem today. For expression 1 + 2 + 3 * 4 - 5 * 2, the maximum
combination I can get is 16. let alone 18 unique results.
Any expert ideas?
The string ‘expression’ contains the operators ’+’, ’-’, ’*’ and
positive integers (or possibly no operators and just one integer). The
expression is entirely unparenthesized. Your function should determine the
result of every possible parenthesization of the expression
t****e
发帖数: 69
2
My solution, some recursive function, implemented in C++:
using namespace std;
void GetUniqueValues( const string & expression, vector & uniqueValues )
{
if(expression.empty()) return;
if(expression.length()==1)
{
uniqueValues.push_back( atoi(expression[0]) );
return;
}
map uniqueMap;
for(int i = 1; i < expression.length(); i+=2)
{
vector leftValues;
vector rightValues;
GetUniqueValues( expression.substr(0, i), leftValues );
GetUnique

【在 M**8 的大作中提到】
: Hi,
: I was being asked the question below long time ago. Still I cannot figure
: out the problem today. For expression 1 + 2 + 3 * 4 - 5 * 2, the maximum
: combination I can get is 16. let alone 18 unique results.
: Any expert ideas?
: The string ‘expression’ contains the operators ’+’, ’-’, ’*’ and
: positive integers (or possibly no operators and just one integer). The
: expression is entirely unparenthesized. Your function should determine the
: result of every possible parenthesization of the expression

t****e
发帖数: 69
3
Well, this assumes the numbers are all single digit, and no space in the exp
ression. To be better some interpretation of the expression needs to be done
to get the numbers and operators. But you get the idea.

uniqueValues )

【在 t****e 的大作中提到】
: My solution, some recursive function, implemented in C++:
: using namespace std;
: void GetUniqueValues( const string & expression, vector & uniqueValues )
: {
: if(expression.empty()) return;
: if(expression.length()==1)
: {
: uniqueValues.push_back( atoi(expression[0]) );
: return;
: }

t****e
发帖数: 69
4
刚用cygwin试了,确实是18个。
原程序有个错误:atoi()的参数是const char *,不是char。
暂且换成 expression[0] - '0' 吧。

exp
done

【在 t****e 的大作中提到】
: Well, this assumes the numbers are all single digit, and no space in the exp
: ression. To be better some interpretation of the expression needs to be done
: to get the numbers and operators. But you get the idea.
:
: uniqueValues )

M**8
发帖数: 25
5
HI tutule,
Your solution is correct. I was thinking, first bracket left, then bracket
right for every expression, like
(1+2+3*4-5)*2
1+(2+3*4-5*2)
But I did not realized middle cases like
(1+2+3)*(4-5*2)
Thanks for your time,
1 (共1页)
进入Computation版参与讨论
相关主题
[合集] 有关fortran返回数组的问题!谁熟悉cplex的integer programming
求助:DGBTRS look for a software
关于计算精度的问题 c++Mixed Integer Programming
要解MILP ( mixed integer linear programming)问题a question about Theta(nlgn)
[请教]uniformly random sampling矩阵问题求解
Fortran里面哪个函数显示系统时间?Please help!
求矩阵逆的算法[转载] 一串32-bit integers,要数bit-1的总数?
Fortran 77 dynamic memory allocation 1[转载] Re: 未婚证明求救
相关话题的讨论汇总
话题: expression话题: question话题: interview话题: operators话题: int