UVA 215 Spreadsheet Calculator (模拟)
模拟题。每个单元格有表达式就dfs,如果有环那么就不能解析,可能会重复访问到不能解析的单元格,丢set里或者数组判下重复。
这种题首先框架要对,变量名不要取的太乱,细节比较多,知道的库函数越多越容易写。注意细节,注意格式。
/*********************************************************
* --------------Tyrannosaurus--------- *
* author AbyssalFish *
**********************************************************/
#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int R = , C = , LEN = ; struct Node
{
int n;
string s;
bool tp; //1 num 0 expr
Node(){ s.reserve(LEN); }
}nd[R][C];
int Err[R][C], kas_clk; void readNd(int x,int y)
{
Node &u = nd[x][y];
getline(cin, u.s);
u.tp = !isalpha(u.s[]);// - ,1, A
if(u.tp) {
stringstream ssin(u.s);
ssin>>u.n;
}
} const int ErrorCode = <<; typedef pair<int,int> pii;
vector<pii > un_ev; int vis[R][C], clk;
//parseNd
int dfs(int x,int y)
{
Node &u = nd[x][y];
if(u.tp) return u.n;
if(Err[x][y] == kas_clk) return ErrorCode;
if(vis[x][y] == clk) {
Err[x][y] = kas_clk;
un_ev.push_back(pii(x,y));
return ErrorCode;
}
vis[x][y] = clk;
int ans = , sz = u.s.size();
//stack<char> stk;
for(int i = ; i < sz; i++){
if(isalpha(u.s[i])){
int val = dfs(u.s[i]-'A',u.s[i+]-'');
if(val == ErrorCode) {
if(Err[x][y] != kas_clk){
Err[x][y] = kas_clk;
un_ev.push_back(pii(x,y));
}
return ErrorCode;
}
char op = i?u.s[i-]:'+';//stk.top(); stk.pop();
ans += op=='+'?val:-val;
i++;
}
else if(isdigit(u.s[i])){
int j = i+;
while(j < sz && isdigit(u.s[j])) j++;
int val = atoi(u.s.substr(i,j).c_str());
ans += u.s[i-]=='+'?val:-val;
i = j-;
}
//if(s[i] == '+' || s[i] == '-'){
//}
}
u.tp = true;
return u.n = ans;
} void init()
{
un_ev.reserve(R*C);
} int r,c;
void solve()
{
for(int i = ; i < r; i++){
for(int j = ; j < c; j++){
readNd(i,j);
}
}
kas_clk++;
for(int i = ; i < r; i++)
for(int j = ; j < c; j++){
if(Err[i][j] != kas_clk){
clk++;
dfs(i,j);
}
}
if(un_ev.size()){
sort(un_ev.begin(),un_ev.end());
//un_ev.erase(unique(un_ev.begin(),un_ev.end()),un_ev.end());
for(auto p: un_ev){
int x = p.first, y = p.second;
printf("%c%c: %s\n",x+'A',y+'',nd[x][y].s.c_str());
}
un_ev.clear();
}
else {
putchar(' ');
for(int j = ; j < c; j++) printf("%6d",j);
puts("");
for(int i = ; i < r; i++){
putchar(i+'A');
for(int j = ; j < c; j++){
printf("%6d",nd[i][j].n);
}
puts("");
}
}
puts(""); //注意格式
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
//ios::sync_with_stdio(false);
//cin.tie(nullptr);
init();
while(scanf("%d%d\n",&r,&c),r+c){
solve();
}
return ;
}
UVA 215 Spreadsheet Calculator (模拟)的更多相关文章
- Spreadsheet Calculator 电子表格计算器 (Uva 215)
原题:https://uva.onlinejudge.org/external/2/215.pdf 有一个M x N的表格,每个单元格是个数字或者表达式.表达式由单元格编号和+ - 号组成 输出单元格 ...
- 【set&&sstream||floyed判环算法】【UVa 11549】Calculator Conundrum
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bored ...
- UVa 11210 - Chinese Mahjong 模拟, 枚举 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 1156 - Pixel Shuffle(模拟+置换)
UVA 1156 - Pixel Shuffle 题目链接 题意:依据题目中的变换方式,给定一串变换方式,问须要运行几次才干回复原图像 思路:这题恶心的一比,先模拟求出一次变换后的相应的矩阵,然后对该 ...
- UVA 12050 - Palindrome Numbers 模拟
题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; ;i<=x;i+ ...
- Uva 679 Dropping Balls (模拟/二叉树的编号)
题意:有一颗二叉树,深度为D,所有节点从上到下从左到右编号为1,2,3.....在结点一处放一个小球,每个节点是一个开关,初始全是关闭的,小球从顶点落下,小球每次经过开关就会把它的状态置反,现在问第k ...
- ●UVa 1589 Xiangqi(模拟)
●赘述题意 给出一个中国象棋残局,告诉各个棋子的位置,黑方只有1枚“将”,红方有至少2枚,至多7枚棋子,包含1枚“帅G”,和若干枚“车R”,“马H”,“炮C”.当前为黑方的回合,问黑方的“将”能否在移 ...
- Uva - 512 - Spreadsheet Tracking
Data in spreadsheets are stored in cells, which are organized in rows (r) and columns (c). Some oper ...
- 【每日一题】 UVA - 213 Message Decoding 模拟解码+读入函数+阅读题
题意:阅读理解难度一道比一道难orz.手摸了好久样例 题解: 读入:大循环用getline读入header顺便处理一下, 里面再写两重循环,外层一次读三个串,内层一次读num个串. 之后就查表,线性 ...
随机推荐
- 796D(bfs)
题目链接: http://codeforces.com/problemset/problem/796/D 题意: 给出一颗 n 个节点树, 树枝连接的两个定点距离为 1, 树中有 k 个特殊点, 问最 ...
- VS2010 不显示 最近使用的项目 解决办法(转)
昨天重装了VS2010,然后开了项目看了下今天早上再打开发现起始页近使用项目列表是空白的,每次打开项目都要去到指定目录去找解决方案才能打开,感觉很麻烦,在网上找了下解决方案,解决步骤下:菜单 —— 运 ...
- 洛谷 P1439 【模板】最长公共子序列LCS 解题报告
题目传送门 是一道十分经典的LCS问题 很容易想到 的一般算法:主题代码如下: for (int i = 1; i <= n; i++) for (int j = 1; j <= n; ...
- POJ2758 Checking the Text 哈希
注意到插入次数挺少的,于是每次暴力重构,然后哈希+二分 #include<cstdio> #include<iostream> #include<algorithm> ...
- Docker从入门到实战(四)
Docker基础 一:Docker基本操作 一般情况安装Docker之后系统会自动创建一个Docker的用户组,如果没有创建可以手动创建groupadd docker把当前非root用户加入group ...
- java——方法重载与重写、构造方法、this关键字、static关键字、strictfp关键字、类的导入
Java SE5新增加@Override注解,它并不是关键字,但是可以把它当作关键字使用.当你想要覆写(重写)某个方法时,可以选择添加这个注解,在你不留心重载而并非覆写了该方法时,编译器就会生成一条错 ...
- HDU 5785 Interesting manacher + 延迟标记
题意:给你一个串,若里面有两个相邻的没有交集的回文串的话,设为S[i...j] 和 S[j+1...k],对答案的贡献是i*k,就是左端点的值乘上右端点的值. 首先,如果s[x1....j].s[x2 ...
- 关于Linux Mint下的pluma软件打开txt文本文件有时候乱码的解决办法
解决方法: 1.在终端输入“dconf-editor”回车(见图2).(如果没有安装用“sudo apt-get install dconf-tools”安装.) 图2 2.展开org/gnome/g ...
- Android Asynchronous Http Client
Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happ ...
- hibernate课程 初探单表映射1-2 ORM定义
1 什么是ORM? ORM(Object / RelationShip Mapping) 对象/关系映射 面向对象编程(OOP)最终要把对象信息保存在关系性数据库中,要写好多sql语句.这与面向对象编 ...