• C*++ Calculations


    http://codeforces.com/problemset/problem/39/A

    贪心 按照常数大小从小到大排序, 结果一个一个计算上来, 不影响结果正确性。

    View Code
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    int f_min(int x,int y) {if(x<y)return x; else return y;}
    int f_max(int x,int y) {if(x<y)return y; else return x;}
    void swap(int&x,int&y) {x=x^y;y=x^y;x=x^y;}
    int f_abs(int x) {return (x)>(0)?(x):(-x);}
    int lowbit(int x) {return (x)&(-x);}
    bool isdig(char ch) {return ch>='0' && ch<='9';}
    //double f_min(double x,double y) {if(x<y)return x; else return y;}
    //double f_max(double x,double y) {if(x<y)return y; else return x;}
    using namespace std;
    
    const int MM = 222222;
    #define mod 10000000000
    const double Pi = acos(-1.0);
    const double lep = 1e-10;
    const double inf = 1000000000.00;
    #define debug puts("wrong")
    #define maxint 0x3f3f3f3f
    #define lson rt<<1
    #define rson rt<<1|1
    typedef __int64 int64;
    //typedef long long int64;
    //const __int64 maxint = 1000000000000000;
    int dx[8]={1,2,1,2,-1,-2,-1,-2};
    int dy[8]={2,1,-2,-1,2,1,-2,-1};
    
    
    int N,M, Q;
    int NE;
    struct Pos {
        int64 val;
        bool ff;
        bool  operator<(Pos x) const{
            return val<x.val;
        }
    }p[MM];
    
    char name[MM];
    
    void get_data() {
        int64 i,j,k,a,tmp,len;
        scanf("%s",name);
        len=strlen(name);
        NE=0;
        for(i=0;i<len;i++) {
    //        printf("%d\n", i);
            if(i==0) {
                if(name[i]=='a') {
                    p[NE].val=1;
                    p[NE++].str="a";
                    i+=2;
                    continue;
                }
                if(name[i]=='+') {
                    p[NE].val=1;
                    p[NE++].str="+";
                    i+=2;
                    continue;
                }
                if(isdig(name[i])) {
                    tmp=0;
                    for(j=i;j<len;j++) 
                        if(name[j]=='*') break;
                        else tmp=tmp*10+name[j]-'0';;
                    p[NE].val=tmp; p[NE++].str=name[j+1];
                    i=j+3;
                    continue;
                }
            }
            else {
                if(name[i]=='+') {
                    if(name[i+1]=='a') {
                     p[NE].val=1;
                     p[NE++].str="a";
                     i+=3;
                     continue;
                    }
                    if(isdig(name[i+1])) {
                        tmp=0;
                        for(j=i+1;j<len;j++) 
                            if(name[j]=='*') break;
                            else tmp=tmp*10+name[j]-'0';
                        p[NE].val=tmp; p[NE++].str=name[j+1];
                        i=j+3;
                        continue;
                    }
                    if(name[i+1]=='+') {
                        p[NE].val=1;
                        p[NE++].str="+";
                        i+=3;
                        continue;
                    }
                }
                if(name[i]=='-') {
                        if(name[i+1]=='a') {
                            p[NE].val=-1; 
                            p[NE++].str=name[i+1];
                            i+=3;
                            continue;
                        }
                        if(isdig(name[i+1])) {
                            tmp=0;
                            for(j=i+1;j<len;j++) 
                                if(name[j]=='*') break;
                                else tmp=tmp*10+name[j]-'0';
                            p[NE].val=-tmp; p[NE++].str=name[j+1];
                            i=j+3;
                            continue;
                        }
                        if(name[i+1]=='+') {
                            p[NE].val=-1; 
                            p[NE++].str=name[i+1];
                        i+=3;
                        continue;
                    }    
            }
        }
        }
    }
    void solve() {
        int64 i,j,k,tmp=0,ans=0;
        sort(p,p+NE);
    //    for(i=0;i<NE;i++) cout<<p[i].str<<endl;
        for(i=0;i<NE;i++) {
            if(p[i].str=="+") ans+=(p[i].val*(N+1));
            else ans+=(p[i].val*N);
            N++;
        }
        printf("%I64d\n", ans);
    }
    
    int main() {
        //    freopen("input.txt","r",stdin);
        //    freopen("output.txt","w",stdout);
        while(scanf("%I64d",&N)!=EOF) get_data(),solve();
        return 0;
    }
    /*
    10
    a++-++a-5*++a-4*a++
    */
  • 相关阅读:
    windows 环境下 MySQL 8.0.13 免安装版配置教程
    mysql锁分析
    Sublime Text 安装sftp插件
    SecureCRT rz 上传文件失败问题
    java与javac版本不一致问题
    比较几种工具Python(x,y) Anaconda WinPython
    王石:没变强是因为你太舒服!
    网络爬虫urllib2 tornado
    R包介绍
    互联网金融必须知道:O2O、P2P、MRD、BRD、LBS、PV、UV、KPI、MRD、VP、UED....
  • 原文地址:https://www.cnblogs.com/zhang1107/p/2987605.html
Copyright © 2020-2023  润新知