• UVa-817 According to Bartjens


    2000=要输出IMPOSSIBLE

      1 #include<bits/stdc++.h>
      2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
      3 #pragma GCC optimize(2)
      4 
      5 using namespace std;
      6 string input;
      7 set<string> rnt;
      8 
      9 int kase = 1;
     10 
     11 bool judge(string a)
     12 {
     13     stack<int> s;
     14     int tmp = 0;
     15     int i;
     16     if(a[0]=='0'&&isdigit(a[1]))
     17         return false;
     18     for(i = 0; isdigit(a[i]); i ++)
     19     {
     20         tmp = tmp*10+a[i]-'0';
     21     }
     22     s.push(tmp);
     23 
     24     for(; a[i]!='=';)
     25     {
     26         tmp = 0;
     27         if(a[i]=='+')
     28         {
     29             if(a[i+1]=='0'&&isdigit(a[i+2]))
     30                 return false;
     31             for(i ++; isdigit(a[i]); i ++)
     32                 tmp = tmp*10+a[i]-'0';
     33             s.push(tmp);
     34         }
     35         else if(a[i]=='-')
     36         {
     37             if(a[i+1]=='0'&&isdigit(a[i+2]))
     38                 return false;
     39             for(i ++; isdigit(a[i]); i ++)
     40                 tmp = tmp*10+a[i]-'0';
     41             s.push(-tmp);
     42         }
     43         else if(a[i]=='*')
     44         {
     45             if(a[i+1]=='0'&&isdigit(a[i+2]))
     46                 return false;
     47             for(i ++; isdigit(a[i]); i ++)
     48                 tmp = tmp*10+a[i]-'0';
     49             int tmp2 = s.top();
     50             s.pop();
     51             s.push(tmp2*tmp);
     52         }
     53     }
     54     int sum = 0;
     55     while(!s.empty())
     56     {
     57         sum += s.top();
     58         s.pop();
     59     }
     60     return sum==2000;
     61 }
     62 
     63 const char op[] {'+','-','*'};
     64 void dfs(int d,string a)
     65 {
     66     if(a[d+1]=='=')
     67     {
     68         if(judge(a))
     69         {
     70             rnt.insert(a);
     71         }
     72         return ;
     73     }
     74 
     75     if(isdigit(a[d]))
     76     {
     77         int i;
     78         for(i = 0;i < 4;i ++)
     79         {
     80             if(i==0)
     81             {
     82                 dfs(d+1,a);
     83             }
     84             else
     85             {
     86                 string b = a.insert(d+1,1,op[i-1]);
     87                 dfs(d+1,b);
     88                 a.erase(d+1, 1);
     89             }
     90         }
     91     }
     92     else
     93         dfs(d+1,a);
     94 }
     95 void solve()
     96 {
     97     rnt.clear(); 
     98     if(input.size()==2||input=="2000=")
     99         return ;
    100     dfs(0,input);
    101 }
    102 void printRnt()
    103 {
    104     printf("Problem %d
    ",kase ++);
    105     for(auto s:rnt)
    106         printf("  %s
    ",s.c_str());
    107     if(rnt.size()==0)
    108         printf("  IMPOSSIBLE
    ");
    109 }
    110 int main()
    111 {
    112     while(getline(cin,input))
    113     {
    114         if(input[0]=='=')
    115             break;
    116         solve();
    117         printRnt();
    118     }
    119     return 0;
    120 }
  • 相关阅读:
    2.2.16锁对象的改变
    2.2.15内置类与同步:测试2
    2.2.14内置类与同步:测试1
    2.2.13内置类与静态内置类
    libev客户端
    Linux下sqlite3编程
    ds18b20驱动及应用程序
    ds18b20采集温度并上报服务器
    linux下GPRS模块ppp拨号上网
    linux下GPRS模块的应用程序
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10150550.html
Copyright © 2020-2023  润新知