正文
UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
N - 秋实大哥搞算数
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit Status
秋实大哥大学物理挂科了,于是在下学期的前两周的某一天要悲剧的补考。为了不给学校的挖掘机大楼做贡献,秋实大哥决定在假期里努力复习。当然,良好的计算能力也是非常必要的,毕竟是涉及计算自己做多少分的题能够通过考试的问题。现在他给自己出了一大堆长长的只有涉及整形四则运算式子,然后埋头计算结果。为了检验自己的计算能力,他请你来帮忙。
Input
第一行一个整数T,表示式子的总数。
接下来每一行有一个长度不超过10^6的表达式,只包含正整数和四则运算符号('+', '-', '*', '/')。
保证输入合法。
Output
对于每一个表达式,输出相应的结果,占一行。
保证运算及结果在long long范围以内。
Sample input and output
Sample Input | Sample Output |
---|---|
2 | 12 |
解题报告
本题..显然也没有任何的特别技巧,使用栈来处理.
不过还是有几点可以改进,因为没有括号,且数据合法,因此我们可以只用一个栈来实现,并且一遍扫描处理掉所有的* , / , -号,之后留在栈中的元素运算都是+,注意使用long long,这样,就解决了本题.
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;ll s[maxn];
char temp[maxn];int main(int argc,char *argv[])
{
int Case,top;
scanf("%d%*c",&Case);
while(Case--)
{
top = ;
char ch;
ll read = ;
int ope = -;
ll sign = ;
scanf("%s",temp);
int len = strlen(temp);
int pos = ;
while(pos < len)
{
ch = temp[pos++];
if (ch == '#' || ch == ' ')
break;
//printf("%c\n",ch);
if (ch <= '' && ch >= '')
{
read *= ;
read += (ch-'');
}
else
{
s[top++] = read*sign;
sign = ;
// cout << read << endl;
read = ;
if (ope == )
{
s[top-] = s[top-]*s[top-];
top--;
}
else if(ope == )
{
s[top-] = s[top-] / s[top-];
top--;
}
ope = -;
if(ch == '-')
sign = -;
else if(ch == '*')
ope = ;
else if(ch == '/')
ope = ;
}
}
s[top++] = sign*read;
if (ope == )
{
s[top-] = s[top-]*s[top-];
top--;
}
else if(ope == )
{
s[top-] = s[top-] / s[top-];
top--;
}
ll ans = ;
for(int i = ; i < top ; ++ i)
ans += s[i];
printf("%lld\n",ans);
}
return ;
}
UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>的更多相关文章- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<;Problem H>;
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<;Problem G>;
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<;Problem E>;
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<;Problem D>;
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<;Problem C>;
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<;Problem B>;
B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<;Problem A>;
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<;Problem B>;
B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<;Problem M>;
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
随机推荐- 集群之LVS(负载均衡)详解
提高服务器响应能力的方法 scale on 在原有服务器的基础上进行升级或者直接换一台新的性能更高的服务器. scale out 横向扩展,将多台服务器并发向外响应客户端的请求.优点:成本低,扩展 ...
- C++:构造函数的默认参数知识拓展
和普通函数一样,构造函数中参数的值既可以通过实参传递,也可以指定为某些默认值,即如果用户不指定实参值,编译系统就使形参取默认值. 例9.3的问题也可以使用包含默认参数的构造函数来处理. [例9 ...
- 整理自己的.net工具库
前言 今天我会把自己平日整理的工具库给开放出来,提供给有需要的朋友,如果有朋友平常也在积累欢迎提意见,我会乐意采纳并补充完整.按照惯例在文章结尾给出地址^_^. 之前我开放其他源码的时候(Framew ...
- Java注解--笔记
@Override标签的作用@Override是伪代码,所以是可写可不写的.它表示方法重写,写上会给我们带来好处. 1.可以当注释用,方便阅读. 2.告诉阅读你代码的人,这是方法的复写. 3.编译器可 ...
- JS 高级总结
一.查找HTML元素 通常,通过 JavaScript,您需要操作 HTML 元素. 1.通过 id 找到 HTML 元素 2.通过标签名找到 HTML 元素 3.通过类名找到 HTML 元素 提示: ...
- Video Frame Synthesis using Deep Voxel Flow 论文笔记
Video Frame Synthesis using Deep Voxel Flow 论文笔记 arXiv 摘要:本文解决了模拟新的视频帧的问题,要么是现有视频帧之间的插值,要么是紧跟着他们的探索. ...
- sqlite、mysql 将时间戳转换成本地时间语句
sqlite:SELECT datetime(createdTime, 'unixepoch', 'localtime'); mysql:SELECT FROM_UNIXTIME( 124948800 ...
- 医药箱APP静态小项目
花费了10天时间,纯手写一个医药箱APP静态小项目,里面有上拉加载.左右滑动.弹出层淡入淡出等效果,主要是练习. 以下是一部分页面效果图: 我用的是谷歌的开发者工具的手机端模拟器. 里面需要优化的地方 ...
- 区分SQL Server关联查询之inner join,left join, right join, full outer join并图解
1.from A inner join B on A.ID=B.ID :两表都有的记录才列出 A表: ID Name B表: ID Clas ...
- JSP web.xml <;jsp-config>;标签使用详解
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素.其中<taglib> 元素在JSP 1.2 ...
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ...
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- 集群之LVS(负载均衡)详解
提高服务器响应能力的方法 scale on 在原有服务器的基础上进行升级或者直接换一台新的性能更高的服务器. scale out 横向扩展,将多台服务器并发向外响应客户端的请求.优点:成本低,扩展 ...
- C++:构造函数的默认参数知识拓展
和普通函数一样,构造函数中参数的值既可以通过实参传递,也可以指定为某些默认值,即如果用户不指定实参值,编译系统就使形参取默认值. 例9.3的问题也可以使用包含默认参数的构造函数来处理. [例9 ...
- 整理自己的.net工具库
前言 今天我会把自己平日整理的工具库给开放出来,提供给有需要的朋友,如果有朋友平常也在积累欢迎提意见,我会乐意采纳并补充完整.按照惯例在文章结尾给出地址^_^. 之前我开放其他源码的时候(Framew ...
- Java注解--笔记
@Override标签的作用@Override是伪代码,所以是可写可不写的.它表示方法重写,写上会给我们带来好处. 1.可以当注释用,方便阅读. 2.告诉阅读你代码的人,这是方法的复写. 3.编译器可 ...
- JS 高级总结
一.查找HTML元素 通常,通过 JavaScript,您需要操作 HTML 元素. 1.通过 id 找到 HTML 元素 2.通过标签名找到 HTML 元素 3.通过类名找到 HTML 元素 提示: ...
- Video Frame Synthesis using Deep Voxel Flow 论文笔记
Video Frame Synthesis using Deep Voxel Flow 论文笔记 arXiv 摘要:本文解决了模拟新的视频帧的问题,要么是现有视频帧之间的插值,要么是紧跟着他们的探索. ...
- sqlite、mysql 将时间戳转换成本地时间语句
sqlite:SELECT datetime(createdTime, 'unixepoch', 'localtime'); mysql:SELECT FROM_UNIXTIME( 124948800 ...
- 医药箱APP静态小项目
花费了10天时间,纯手写一个医药箱APP静态小项目,里面有上拉加载.左右滑动.弹出层淡入淡出等效果,主要是练习. 以下是一部分页面效果图: 我用的是谷歌的开发者工具的手机端模拟器. 里面需要优化的地方 ...
- 区分SQL Server关联查询之inner join,left join, right join, full outer join并图解
1.from A inner join B on A.ID=B.ID :两表都有的记录才列出 A表: ID Name B表: ID Clas ...
- JSP web.xml <;jsp-config>;标签使用详解
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素.其中<taglib> 元素在JSP 1.2 ...