HDU 3347 Calculate the expression By Assassin 模拟水题
来源:互联网 发布:js实现点击图片放大 编辑:程序博客网 时间:2024/06/05 06:32
题目大意
You may find it’s easy to calculate the expression such as: a = 3 b = 4 c = 5 a + b + c = ? Isn’t it?
Input
The first line contains an integer stands for the number of test cases. Each test case start with an integer n stands for n expressions will follow for this case.Then n – 1 expressions in the format: [variable name][space][=][space][integer] will follow.You may suppose the variable name will only contain lowercase letters and the length will not exceed 20, and the integer will between -65536 and 65536.The last line will contain the expression you need to work out.In the format: [variable name| integer][space][+|-][space][variable name| integer] …= ?You may suppose the variable name must have been defined in the n – 1 expression and the integer is also between -65536 and 65536. You can get more information from the sample.
Output
For each case, output the result of the last expression.
Sample Input
34aa = 1bb = -1aa = 2aa + bb + 11 = ?11 + 1 = ?11 + -1 = ?
Sample Output
1220
题目分析
乍一看挺难,但是注意看题!!!前n-1行格式是固定的,一定是给变量赋值,最后一行就是变量+常数的加减,比较简单,注意每次考虑运算的符号即可。变量可以用 map记录
(开始把题目想太复杂了,所以记录一下简单的做法)
#include<bits/stdc++.h>using namespace std;map<string,int>mp;int string_to_int(string s){ //字符串 int sum=0,pos=0,sign=0; if(s[pos]=='-'){ sign=1; pos++; } for(;pos<s.size();pos++){ sum=sum*10+s[pos]-'0'; } if(sign==1){ return -sum; } else { return sum; }}int main(){ int t,n; string variablename,c,integer; scanf("%d",&t); while(t--){ scanf("%d",&n); mp.clear(); for(int i=1;i<n;i++){ cin>>variablename>>c>>integer; mp[variablename]=string_to_int(integer); } int sign=0; long long ans=0; while(cin>>c){ if(c.size()==1){ if(c[0]=='-'){ sign=1; continue; } else if(c[0]=='+'){ sign=0; continue; } } if(c[0]=='=')continue; if(c[0]=='?')break; if(c[0]>='a'&&c[0]<='z'){ if(sign==1){ ans-=mp[c]; } else { ans+=mp[c]; } } else { if(sign==1){ ans-=string_to_int(c); } else { ans+=string_to_int(c); } } } cout<<ans<<endl; } return 0; }
0 0
- HDU 3347 Calculate the expression By Assassin 模拟水题
- HDU 3347 Calculate the expression【模拟】
- HDU 3347 Calculate the expression 模拟 map
- HDU 3347 Calculate the expression(模拟 + map)
- hdu 3347 Calculate the expression
- HDU 3347:Calculate the expression
- hdu 3347 Calculate the expression
- hdu 3347 Calculate the expression
- HDU 3348 By Assassin [可怕的模拟题]
- Calculate the expression
- HDU 2209 翻纸牌游戏 By Assassin 模拟
- HDU 2054 A == B ? By Assassin 模拟
- HDU 2955 Robberies by Assassin
- HDU 2571 命运 by Assassin
- HDU 2159 FATE by Assassin
- HDU 5965 扫雷 By Assassin
- 树状dp入门 HDU 1561 The more, The Better By Assassin
- HDU 4028 The time of a day By Assassin dp+离散化!
- Protobuf 序列化协议详解
- 后台传送时间格式数据json转换
- 白岩松:没有一代人的青春是容易的
- IDEA最有用的快捷键
- 第2天:再接着介绍一下Python呗
- HDU 3347 Calculate the expression By Assassin 模拟水题
- 第3天:Numpy你好
- SpringMVC学习笔记2
- [iOS开发]Xcode8.3兼容iOS10.3.1以及低版本Xcode调试高版本iOS系统
- Solr搜索引擎开发初试(2)(接"Lucene搜索引擎开发初试(1)")
- android 如何判断当前是否为飞行模式
- 恩布开源安卓手机IM,EntboostIM全新发布1.6.0版本
- 使用ls筛选某一天的文件
- Bootstrap的datatable控件