• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
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\n", ++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] << "=\n", ok = true;
        if(!ok) puts("  IMPOSSIBLE");
    }
    return 0;
}

 

posted on 2016-08-10 17:46  dwtfukgv  阅读(344)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3