zoukankan      html  css  js  c++  java
  • UVA 215 Spreadsheet Calculator (模拟)

    模拟题。每个单元格有表达式就dfs,如果有环那么就不能解析,可能会重复访问到不能解析的单元格,丢set里或者数组判下重复。

    这种题首先框架要对,变量名不要取的太乱,细节比较多,知道的库函数越多越容易写。注意细节,注意格式。

    /*********************************************************
    *      --------------Tyrannosaurus---------              *
    *   author AbyssalFish                                   *
    **********************************************************/
    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    
    const int R = 20, C = 10, LEN = 76;
    
    
    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[0]);// - ,1, A
        if(u.tp) {
            stringstream ssin(u.s);
            ssin>>u.n;
        }
    }
    
    const int ErrorCode = 1<<30;
    
    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 = 0, sz = u.s.size();
        //stack<char> stk;
        for(int i = 0; i < sz; i++){
            if(isalpha(u.s[i])){
                int val = dfs(u.s[i]-'A',u.s[i+1]-'0');
                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-1]:'+';//stk.top(); stk.pop();
                ans += op=='+'?val:-val;
                i++;
            }
            else if(isdigit(u.s[i])){
                int j = i+1;
                while(j < sz && isdigit(u.s[j])) j++;
                int val = atoi(u.s.substr(i,j).c_str());
                ans += u.s[i-1]=='+'?val:-val;
                i = j-1;
            }
            //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 = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                readNd(i,j);
            }
        }
        kas_clk++;
        for(int i = 0; i < r; i++)
            for(int j = 0; 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
    ",x+'A',y+'0',nd[x][y].s.c_str());
            }
            un_ev.clear();
        }
        else {
            putchar(' ');
            for(int j = 0; j < c; j++) printf("%6d",j);
            puts("");
            for(int i = 0; i < r; i++){
                putchar(i+'A');
                for(int j = 0; 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
    ",&r,&c),r+c){
            solve();
        }
        return 0;
    }
  • 相关阅读:
    博客地址
    Version 1.4.2_03 of the JVM not suitable for this product.解决
    http请求(一) 工具
    Service 的两种启动方法和区别
    软件开发过程应该采用集中优势兵力各个击破
    架构感悟
    嵌套事务模版
    软件行业对人才的依赖
    使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
    架构设计中的分层与分区
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4957709.html
Copyright © 2011-2022 走看看