zoukankan      html  css  js  c++  java
  • UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>

    N - 秋实大哥搞算数

    Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
     

    秋实大哥大学物理挂科了,于是在下学期的前两周的某一天要悲剧的补考。为了不给学校的挖掘机大楼做贡献,秋实大哥决定在假期里努力复习。当然,良好的计算能力也是非常必要的,毕竟是涉及计算自己做多少分的题能够通过考试的问题。现在他给自己出了一大堆长长的只有涉及整形四则运算式子,然后埋头计算结果。为了检验自己的计算能力,他请你来帮忙。

    Input

    第一行一个整数T,表示式子的总数。

    接下来每一行有一个长度不超过10^6的表达式,只包含正整数和四则运算符号('+', '-', '*', '/')。

    保证输入合法。

    Output

    对于每一个表达式,输出相应的结果,占一行。

    保证运算及结果在long long范围以内。

    Sample input and output

    Sample InputSample Output
    2
    12+5/4-1
    4*5/3
    12
    6

    解题报告

    本题..显然也没有任何的特别技巧,使用栈来处理.

    不过还是有几点可以改进,因为没有括号,且数据合法,因此我们可以只用一个栈来实现,并且一遍扫描处理掉所有的* , / , -号,之后留在栈中的元素运算都是+,注意使用long long,这样,就解决了本题.

    #include <iostream>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e6 + 50;
    
    ll s[maxn];
    char temp[maxn];
    
    int main(int argc,char *argv[])
    {
      int Case,top;
      scanf("%d%*c",&Case);
      while(Case--)
       {
             top = 0;
             char ch;
             ll read = 0;
             int ope = -1;
             ll sign = 1;
             scanf("%s",temp);
             int len = strlen(temp);
             int pos = 0;
             while(pos < len) 
              {
                    ch = temp[pos++];
                    if (ch == '#' || ch  == ' ')
                     break;
                    //printf("%c
    ",ch);
                    if (ch <= '9' && ch >= '0')
                     {
                           read *= 10;
                  read += (ch-'0'); 
               }
              else
               {
                     s[top++] = read*sign;
                     sign = 1;
                    // cout << read << endl;
                     read = 0;
                     if (ope == 2)
                      {
                            s[top-2] = s[top-1]*s[top-2];
                            top--;
                   }
                  else if(ope == 3)
                   {
                         s[top-2] = s[top-2] / s[top-1];
                         top--;
                   }
                  ope = -1;
                  if(ch == '-')
                          sign = -1;
                  else if(ch == '*')
                       ope = 2;
                  else if(ch == '/')
                    ope = 3;
               }
           }
         s[top++] = sign*read;
          if (ope == 2)
            {
                s[top-2] = s[top-1]*s[top-2];
                   top--;
            }
          else if(ope == 3)
            {
                s[top-2] = s[top-2] / s[top-1];
                top--;
            }
         ll ans = 0;
         for(int i = 0 ; i < top ; ++ i)
          ans += s[i];
         printf("%lld
    ",ans);
       }
      return 0;
    }
    No Pain , No Gain.
  • 相关阅读:
    状态线程
    C++编译 C # 调用方法
    图像算法集合
    openmp 和 thread 性能实测
    RGB转YUV 各种库的性能比较
    ipp 实现图像空间的转换
    Eigen 学习笔记
    线性代数笔记
    凸优化 笔记
    Windows系统服务器中安装Redis服务
  • 原文地址:https://www.cnblogs.com/Xiper/p/4470231.html
Copyright © 2011-2022 走看看