zoukankan      html  css  js  c++  java
  • 2013 NOIP 普及组 第2题 表达式求值

    P1981 表达式求值 (NOIP2013)

    注意:

    (我自己的一些错误和注意点)
    if ((s[i]>=’0′) && (s[i]<=’9′)) 这句话建议加上,因为谁知道在字符串末尾可能会有一些稀奇古怪的东西
    每一个 case 下面都要写上 break 来退出当前 switch 语句(我也不知道为什么,加上以后貌似就对了,之前误判了很多乘号)
    还有,我自己犯了一个错误:我忘记会有连乘的情况,害得我这破题搞了一晚
     
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstring>
     6 using namespace std;
     7 int main()
     8 {
     9     freopen("expr.in","r",stdin);
    10     freopen("expr.ans","w",stdout);
    11     string s;
    12     long long now=0,result=0,time=1,isTime=0;
    13     cin >> s;
    14     int len = s.length();
    15     for (int i=0;i<len;i++)
    16     {
    17         switch(s[i])
    18         {
    19             case '+':
    20             {
    21                 if (isTime)
    22                 {
    23                     result+=time*now;
    24                     result = result%10000;
    25                     isTime=0;
    26                     time=1;
    27                 }
    28                 else
    29                 {
    30                     result+=now;
    31                     result = result%10000;
    32                 }
    33                 now = 0;
    34                 break;
    35             }
    36             case '*':
    37             {
    38                 time*=now;
    39                 time%=10000;
    40                 isTime = 1;
    41                 now=0;
    42                 break;
    43             }
    44             default:
    45             {
    46                 if ((s[i]>='0') && (s[i]<='9'))
    47                 {
    48                     now = (now*10+s[i]-48)%10000;
    49                 }
    50                 break;
    51             }
    52         }
    53     }
    54     if (isTime)
    55     {
    56         result+=time*now;
    57         result = result%10000;
    58     }
    59     else
    60     {
    61         result+=now;
    62         result = result%10000;
    63     }
    64     cout << result << endl;
    65     return 0;
    66 }
  • 相关阅读:
    flexpager权限控制文件crossdomain.xml
    MongoDB之mongodb.cnf配置
    MySQL之my.cnf配置
    在CentOS的profile文件中配置环境变量
    在CentOS上配置MySQL服务
    在CentOS上配置redis服务
    在CentOS上配置tomcat服务
    在CentOS上配置Tomcat服务脚本
    Netflix Hystrix — 应对复杂分布式系统中的延时和故障容错 转
    ETCD 简介 + 使用
  • 原文地址:https://www.cnblogs.com/OIerPrime/p/7674624.html
Copyright © 2011-2022 走看看