uva 327 Evaluating Simple C Expressions

2024-03-24 07:48

本文主要是介绍uva 327 Evaluating Simple C Expressions,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原题:
The task in this problem is to evaluate a sequence of simple C expressions, buy you need not know C
to solve the problem! Each of the expressions will appear on a line by itself and will contain no more
than 110 characters. The expressions to be evaluated will contain only simple integer variables and a
limited set of operators; there will be no constants in the expressions. There are 26 variables which
may appear in our simple expressions, namely those with the names ‘a’ through ‘z’ (lower-case letters
only). At the beginning of evaluation of each expression, these 26 variables will have the integer values
1 through 26, respectively (that is, a = 1, b = 2, …, n = 14, o = 15, …, z = 26). Each variable will
appear at most once in an expression, and many variables may not be used at all.
The operators that may appear in expressions include the binary (two-operand) ‘+’ and ‘-’, with
the usual interpretation. Thus the expression ‘a + c - d + b’ has the value 2 (computed as ‘1 + 3 -
4 + 2’). The only other operators that may appear in expressions are ‘++’ and ‘–’. These are unary
(one-operand) operators, and may appear before or after any variable. When the ‘++’ operator appears
before a variable, that variable’s value is incremented (by one) before the variable’s value is used in
determining the value of the entire expression. Thus the value of the expression ‘++ c - b’ is 2, with
‘c’ being incremented to 4 prior to evaluating the entire expression. When the ‘++’ operator appears
after a variable, that variable is incremented (again, by one) after its value is used to determine the
value of the entire expression. Thus the value of the expression c ++ - b is 1, but ‘c’ is incremented
after the complete expression is evaluated; its value will still be 4. The ‘–’ operator can also be used
before or after a variable to decrement (by one) the variable; its placement before or after the variable
has the same signficance as for the ‘++’ operator. Thus the expression ‘–c + b–’ has the value 4,
with variables ‘c’ and ‘b’ having the values 2 and 1 following the evaluation of the expression.
Here’s another, more algorithmic, approach to explaining the ‘++’ and ‘–’ operators. We’ll consider
only the ‘++’ operator, for brevity:

  1. Identify each variable that has a ‘++’ operator before it. Write a simple assignment statement
    that increments the value of each such variable, and remove the ‘++’ operator from before that
    variable in the expression.
  2. In a similar manner, identify each variable that has a ‘++’ operator after it. Write a simple
    assignment statement that increments the value of each of these, and remove the ‘++’ operator
    from after that variable in the expression.
  3. Now the expression has no ‘++’ operators before or after any variables. Write the statement that
    evaluates the remaining expression after those statements written in step 1, and before those
    written in step 2.
  4. Execute the statements generated in step 1, then those generated in step 3, and finally the one
    generated in step 2, in that order.
    Using this approach, evaluating the expression ‘++ a + b ++’ is equivalent to computing ‘a = a +
    1’ (from step 1 of the algorithm) ‘expression = a + b’ (from step 3) ‘b = b + 1’ (from step 2) where
    expression would receive the value of the complete expression.
    Input
    Your program is to read expressions, one per line, until the end of the file is reached.
    Output
    Display each expression exactly as it was read, then display the value of the entire expression, and on
    separate lines, the value of each variable after the expression was evaluated. Do not display the value of
    variables that were not used in the expression. The samples shown below illustrate the desired exact
    output format.
    Blanks are to be ignored in evaluating expressions, and you are assured that ambiguous expressions
    like ‘a+++b’ (ambiguous because it could be treated as ‘a++ + b’ or ‘a + ++b’) will not appear in the
    input. Likewise, ‘++’ or ‘–’ operators will never appear both before and after a single variable. Thus
    expressions like ‘++a++’ will not be in the input data.
    Sample Input
    a + b
    b - z
    a+b–+c++
    c+f–±-a
    f-- + c-- + d-++e
    Sample Output
    Expression: a + b
    value = 3
    a = 1
    b = 2
    Expression: b - z
    value = -24
    b = 2
    z = 26
    Expression: a+b–+c++
    value = 6
    a = 1
    b = 1
    c = 4
    Expression: c+f–±-a
    value = 9
    a = 0
    c = 3
    f = 5
    Expression: f-- + c-- + d-++e
    value = 7
    c = 2
    d = 4
    e = 6
    f = 5

中文:
给你一个表达式,这个表达式,这个表达式仅由加法和减法组成,其中表达式中的变量会有类似“++a”和“a++”这样的运算符出现。
给定的表达式保证合法,现在问你这个表达式的计算结果是多少,同时输出每个变量在计算完表达式后的值为多少。

注意,会有如下数据
a
++a
–a
a++
a–
结果为:
Expression: a
value = 1
a = 1
Expression: ++a
value = 2
a = 2
Expression: --a
value = 0
a = 0
Expression: a++
value = 1
a = 2
Expression: a–
value = 1
a = 0

代码:

#include <bits/stdc++.h>using namespace std;
struct node
{char m;//f表示a++,r表示++a,x表示没有自加或自减操作char o;//'-'还是'+'
};string s,expr,ex;
int mark[26];
bool used[26];int main()
{ios::sync_with_stdio(false);while(getline(cin,ex)){s.clear();for(int i=0;i<ex.size();i++){if(ex[i]!=' ')s+=ex[i];}for(int i=0;i<26;i++)mark[i]=i+1;memset(used,false,sizeof(used));expr.clear();//expr为去掉自加自减运算符后的表达式queue<node> q;//队列中保存每个变量是自加、自减还是没有运算符for(int i=0;i<s.size();){if(s[i]>='a'&&s[i]<='z'){used[s[i]-'a']=true;if(i==0){if(i+1>=s.size())//防止expr="a"{expr+=s[i];q.push(node{'x','x'});break;}if(s[i+1]==s[i+2]){q.push(node{'f',s[i+1]});expr+=s[i];if(i+3<s.size()){expr+=s[i+3];i+=3;}elsei+=2;}else{q.push(node{'x','x'});expr+=s[i];expr+=s[i+1];i+=1;}}else{if(s[i-1]==s[i-2]){q.push(node{'r',s[i-1]});expr+=s[i];if(i+1<s.size())expr+=s[i+1];i+=1;}else{if(i+2<s.size()){if(s[i+1]==s[i+2]){q.push(node{'f',s[i+1]});expr+=s[i];if(i+3<s.size()){expr+=s[i+3];i+=3;}elsei+=2;}else{q.push(node{'x','x'});expr+=s[i];expr+=s[i+1];i+=1;}}else{q.push(node{'x','x'});expr+=s[i];if(i+1<s.size())expr+=s[i+1];i++;}}}}elsei++;}int ans=0;for(int i=0;i<expr.size();){if(i==0){node t=q.front();q.pop();if(t.m=='x')ans=mark[expr[i]-'a'];else{if(t.m=='f')//a++{ans=mark[expr[i]-'a'];if(t.o=='+')mark[expr[i]-'a']++;elsemark[expr[i]-'a']--;}else//++a{if(t.o=='+')mark[expr[i]-'a']++;elsemark[expr[i]-'a']--;ans=mark[expr[i]-'a'];}}i++;}else{char o=expr[i];char c=expr[i+1];node t=q.front();q.pop();if(o=='-'){if(t.m=='f')//a++{ans-=mark[c-'a'];if(t.o=='+')mark[c-'a']++;elsemark[c-'a']--;}else//++a{if(t.m=='x')ans-=mark[c-'a'];else{if(t.o=='+')mark[c-'a']++;elsemark[c-'a']--;ans-=mark[c-'a'];}}}else{if(t.m=='f')//a++{ans+=mark[c-'a'];if(t.o=='+')mark[c-'a']++;elsemark[c-'a']--;}else//++a{if(t.m=='x')ans+=mark[c-'a'];else{if(t.o=='+')mark[c-'a']++;elsemark[c-'a']--;ans+=mark[c-'a'];}}}i+=2;}}cout<<"Expression: "<<ex<<endl;cout<<"    value = "<<ans<<endl;for(int i=0;i<26;i++){if(used[i])cout<<"    "<<(char)('a'+i)<<" = "<<mark[i]<<endl;}}return 0;
}

解答:

小白书上的练习题,题目上标着是二叉树,可是我没看出来-_-||| 直接用字符串处理给解决了,感觉应该是使用表达式树来处理双目运算符吧。

思路很简单,首先遍历表达式,判断表达式中每个变量前面两个运算符和后面两个运算符当中是否有相等的符号,如果包含相等的符号,那么说明该变量有自加自减的运算符,将该变量的自操作算符所在变量的前面或是后面,以及是加法还是减法保存在队列当中,同时在原表达式中去掉,仅保留双目运算符,并存储在expr当中。

使用mark来标记每个变量的当前值,遍历expr中的每个变量,根据队列中的信息,计算表达式值即可。

这篇关于uva 327 Evaluating Simple C Expressions的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/840919

相关文章

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=