zoukankan      html  css  js  c++  java
  • UVa 817 According to Bartjens (暴力,DFS)

    题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子。 所有数字不能有前导0,

    且式子必须是合法的。

    析:这个题很明显的暴力,因为最长才9位数字,也就是最多有8个位置位置可能插符号,当然实际并没有那么多,所以直接暴力就行,也不用优化,直接暴就行。

    就是DFS,在每个位置考虑四种情况,*,+,-,或者不放,最后再一个一个的判断是不是等于2000就好,注意这个题有一个坑,我也不知道是哪个数据,

    也没有想到,就是一个没有用运算符也没有的时候,是不成立的,比如2000,这个是不成立,我并没有找到其他的数据,但是如果我只判2000,是WA,

    如果哪位大神知道,告诉一下,感激不尽。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <stack>
    using namespace std ;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 10 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {0, 0, -1, 1};
    const int dc[] = {-1, 1, 0, 0};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    vector<string> ans;
    
    void dfs(int idx, string s){
        if(idx == s.size()){
            ans.push_back(s);
            return ;
        }
    
        for(int i = 0; i < 3; ++i){
            string t = s;
            t.insert(idx, 1, mark[i]);
            dfs(idx+2, t);
        }
        dfs(idx+1, s);
    }
    
    inline bool before0(const string &s){
        int cnt = 0;
        for(int i = 1; i < s.size()-1; ++i){
            if(!isdigit(s[i]))  ++cnt;
            if(!isdigit(s[i-1]) && s[i] == '0' && isdigit(s[i+1]))  return true;
        }
        
        return !(cnt > 0);
    }
    
    bool judge(const string &s){
        int i = 0;
        int ans = 0, tmp = 0;
        char ch = '+';
        while(i < s.size() && isdigit(s[i])) tmp = tmp * 10 + s[i++] - '0';
        while(i < s.size()){
            if(s[i] == '*'){
                int t = 0;  ++i;
                while(i < s.size() && isdigit(s[i])) t = t * 10 + s[i++] - '0';
                tmp *= t;
            }
            else{
                ans += ch == '+' ? tmp : -tmp;
                ch = s[i];  ++i;  tmp = 0;
                while(i < s.size() && isdigit(s[i]))  tmp = tmp * 10 + s[i++] - '0';
            }
        }
        ans += ch == '+' ? tmp : -tmp;
        return ans == 2000;
    }
    
    int main(){
        string s;
        int kase = 0;
        while(cin >> s && s[0] != '='){
            printf("Problem %d
    ", ++kase);
            if(s == "2000=" || s.size() < 4){  puts("  IMPOSSIBLE"); continue; }
            s.pop_back();
            ans.clear();
            dfs(1, s);
            bool ok = false;
            for(int i = 0; i < ans.size(); ++i)
                if(!before0(ans[i]) && judge(ans[i]))
                    cout << "  " << ans[i] << "=
    ", ok = true;
            if(!ok) puts("  IMPOSSIBLE");
        }
        return 0;
    }
    
  • 相关阅读:
    未来的计划和考虑
    jquery 常用的方法
    对于页面动态加载的元素事件无效的解决方案
    Myeclipse8.5中svn插件安装方法总结
    JS读RSS
    JAVA的RSS处理
    环境:win7+ie8 IE8的F12不起作用,原因如下:
    关闭和释放JDBC
    关于Eclipse无法生成class文件的问题
    JavaScript跨域总结与解决办法
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5757740.html
Copyright © 2011-2022 走看看