[HNOI2002]沙漠寻宝 题解
一道大模拟
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
string st[1010];
int value[26];
int f,o,s,i;
int p[1010],num[1010];
int s1[150];
char s2[150];
int t1=0,t2=0;
char c;
bool check(char ch){
if((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')||(ch=='+')||(ch=='-')||(ch=='*')
||(ch=='/')||(ch=='(')||(ch==')')||(ch=='='))
return true;
return false;
}
void calcu(){
int x1,x2,x;
char p;
p=s2[t2--];
x2=s1[t1--];
x1=s1[t1--];
switch(p){
case '+' : x=x1+x2; break;
case '-' : x=x1-x2; break;
case '*' : x=x1*x2; break;
case '/' : x=x1/x2; break;
}
s1[++t1]=x;
}
void readStr(){
char b;
s++;
int count=0;
scanf("%c",&b);
while(!check(b)) scanf("%c",&b);
while(check(b)){
st[s]=st[s]+b;
if(b=='d')count++;
if(count>20){
st[s]=st[s].substr(0,3);
break;
}
scanf("%c",&b);
}
}
int result(string t){
char c;
unsigned i=0;
t1=0,t2=0;
int v;
while(i<t.size()){
c=t[i];
if(c=='+'||c=='-'){
while(t2&&s2[t2]!='(') calcu();
s2[++t2]=c;
i++;
}
else if(c=='*'||c=='/'){
while(t2&&(s2[t2]=='*' || s2[t2]=='/')) calcu();
s2[++t2]=c;
i++;
}
else if(c=='('){
s2[++t2]=c;
i++;
}
else if(c==')'){
while(s2[t2]!='(') calcu();
t2--;
i++;
}
else if(c>='a'&&c<='z'){
s1[++t1]=value[c-'a'];
i++;
}else{
v=0;
do{
v=10*v+c-'0';
c=t[++i];
}while(c>='0'&&c<='9'&&i<t.size());
s1[++t1]=v;
}
}
while(t2) calcu();
return s1[t1];
}
int main(){
o=1;
while(o!=0){
readStr();
//cout<<st[s]<<endl;
if(st[s]=="loop") o++;
else if(st[s]=="end") o--;
}
i=2;
f=1;num[1]=1;
while(i<=s){
if(st[i]=="loop"){
f++;
i++;
num[f]=result(st[i]);
i++; p[f]=i;
}
else if(st[i]=="end"){
num[f]--;
if(num[f]==0){
f--;i++;
}else i=p[f];
}
else if(st[i]=="break"){
o=1;
while(o!=0){
i++;
if(st[i]=="loop") o++;
else if(st[i]=="end") o--;
}
num[f]=1;
}else if(st[i]=="continue"){
o=1;
while(o!=0){
i++;
if(st[i]=="loop") o++;
else if(st[i]=="end") o--;
}
}
else if(st[i]=="write"){
i++;
printf("%d\n",result(st[i]));
i++;
}
else{
value[st[i][0]-'a']=result(st[i].substr(2,st[i].size()-2));
i++;
}
}
return 0;
}
[HNOI2002]沙漠寻宝 题解的更多相关文章
- BZOJ1588:[HNOI2002]营业额统计——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务 ...
- 洛谷 P1076 寻宝 题解
今天又TM考试了...... 这是T1,然后我模拟20分滚粗. Analysis 在每层的时候用编号%这层可以上楼的房间个数就行了. #include<iostream> #include ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- SET || BZOJ 1588: [HNOI2002]营业额统计 || Luogu P2234 [HNOI2002]营业额统计
题面:P2234 [HNOI2002]营业额统计 题解:随便写写 注意:cmath中abs函数返回的是一个浮点数,在bzoj上会ce 代码: #include<cstdio> #inclu ...
- P1076 寻宝
P1076 寻宝 题解 这道题真是感人啊,废了蒟蒻一天的时间 关键 1. a[ k ][ ] 数组记录第k层有楼梯房间的编号 a[ k ][ 0 ] 第k层有几个有楼梯的房间 a[ k ][ i ] ...
- 【题解】Kathy函数 [BZOJ1223] [P2235] [HNOI2002]
[题解]Kathy函数 [BZOJ1223] [P2235] [HNOI2002] 这几疯狂刷了数位\(dp\)的题,到这道题时被卡了一天,一看大佬的讲解发现居然是求回文数╮(╯_╰)╭ 感觉被大佬狠 ...
- [HNOI2018]寻宝游戏(题解转载自别处)
题解(自别处转载): Luogu CSDN 这题关键是将运算符也替换成0,1 然后在运算符与原串混杂里找规律. 而且替换的方式也有所要求,考场上两种替换方式都要尝试. #include <bit ...
- BZOJ5285 & 洛谷4424 & UOJ384:[HNOI/AHOI2018]寻宝游戏——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5285 https://www.luogu.org/problemnew/show/P4424 ht ...
- 【题解】 P2234 [HNOI2002]营业额统计
平衡树板题 原题传送门 这道题要用Splay,我博客里有对Splay的详细介绍 这道题目还算比较模板的 每输入一个数,先不要插入 要求一下前驱和后继与x差的最小值并加到答案中 再把x插入平衡树 然后你 ...
随机推荐
- MemCached的工具类。获取cached中的所有key
package com.ibs.auth.controller; import java.io.UnsupportedEncodingException; import java.util.Date; ...
- logback使用配置
一:logback.xml配置内容如下 <?xml version="1.0" encoding="UTF-8"?> <!-- Copyrig ...
- WPF控件截图
//截图 RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height) { ...
- luogu1220_关路灯 区间dp
传送门 区间dp f[i][j][state] : [i, j]区间 state=0 当前选i state = 1 当前选j 注意枚举的顺序 转移的设计时 在同时刻不在[i,j]区间里的数也要考虑 不 ...
- 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces
题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...
- Go orm框架gorm学习
之前咱们学习过原生的Go连接MYSQL的方法,使用Go自带的"database/sql"数据库连接api,"github.com/go-sql-driver/mysql& ...
- 【0802 | Day 7】Python进阶(一)
目 录 数字类型的内置方法 一.整型内置方法(int) 二.浮点型内置方法(float) 字符串类型内置方法 一.字符串类型内置方法(str) 二.常用操作和内置方法 优先掌握: 1.索引取值 2. ...
- (十四)c#Winform自定义控件-键盘(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十七)c#Winform自定义控件-多输入窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- JAVA的初体验
jdk的地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 第一步:找到相应的JAVA SE版本,点击down ...