首页 > 其他 > 详细

九度 1498:寻找表达式

时间:2014-03-07 13:39:45      阅读:467      评论:0      收藏:0      [点我收藏+]

题目描述:

现在有一个序列123......N,其中N介于3和15之间,要求在序列之间加入+、-或者空格,使得该序列组成的数学表达式的运算结果为0。

 

思路

1. 枚举运算符, 时间复杂度为 o(3^15)

2. 1_2 是 12, 10_11 是 1011, WA了很多次

3. 一个简单的计算器做了一下午, 思路忘了. 要注意案例, 1+2_3, 1+2_3-4

4. 下次要把中序表达式转后序表达式的算法实现以下

5. 使用了 STL, 超时了. 当然不需要使用 STL 也能做出来

 

代码

bubuko.com,布布扣
#include <iostream>
#include <stdio.h>
#include <string>
#include <deque>
#include <vector>
using namespace std;

int n;
int cnt;
char ops[5] = { , +, -};

int calculation(const string &str, int n) {
    vector<int> stack1, stack2;
    stack1.push_back(1);
    stack1.push_back(2);
    stack2.push_back(str[0]-0);


    for(int i = 2; i < n; i ++) {
        int newop = str[i-1]-0;
        if(stack2.size() == 0) {
            stack1.push_back(i+1);
            stack2.push_back(newop);
            continue;
        }

        if(stack2.back() == 0) {
            int rt = stack1.back();
            stack1.pop_back();
            int lt = stack1.back();
            stack1.pop_back();
            if(rt < 10)
                stack1.push_back(lt*10 + rt);
            else
                stack1.push_back(lt*100 + rt);
            stack2.pop_back();
            
            i --;
            continue;    
        }

        if(stack2.back() == 1) {
            if(newop == 0) {
                stack1.push_back(i+1);
                stack2.push_back(0);
            }else {
                int rt = stack1.back();
                stack1.pop_back();
                int lt = stack1.back();
                stack1.pop_back();

                stack1.push_back(lt+rt);
                stack2.pop_back();
                stack2.push_back(newop);
                stack1.push_back(i+1);
            }
            continue;
        }

        if(stack2.back() == 2) {
            if(newop == 0) {
                stack1.push_back(i+1);
                stack2.push_back(0);
            }else {
                int rt = stack1.back();
                stack1.pop_back();
                int lt = stack1.back();
                stack1.pop_back();

                stack2.pop_back();
                stack1.push_back(lt-rt);
                stack2.push_back(newop);
                stack1.push_back(i+1);
            }
            continue;
        }

    }

    while(stack2.size()) {
        int newop = stack2.back();
        int rt = stack1.back();
        stack1.pop_back();
        int lt = stack1.back();
        stack1.pop_back();
        stack2.pop_back();

        if(newop == 0) {
            if(rt < 10)
                stack1.push_back(lt*10 + rt);
            else
                stack1.push_back(lt*100 + rt);
        }else if(newop == 1) {
            stack1.push_back(lt+rt);
        }else{
            stack1.push_back(lt-rt);
        }
    }
    
    return stack1.back();

    
}


void printResult(const string &str) {
    cnt ++;
    string res;
    res.push_back(1+0);
    for(int i = 1; i < n; i ++) {
        res.push_back(ops[str[i-1]-0]);
        if(i+1 < 10) {
            res.push_back(i+1+0);
        }else{
            res.push_back(1);
            res.push_back((i+1)%10+0);
        }
    }
    printf("%s\n", res.c_str());

}

void dfs(string &str, int depth) {
    if(depth == 0) {
        int res = calculation(str, n);
        if(res == 0) {
            //cout << str << endl;
            printResult(str);
        }
            
        return;
    }

    for(int i = 0; i < 3; i ++) {
        //printf("depth = %d, i = %d\n",depth, i);
        str.push_back(i+0);
        dfs(str, depth-1);
        str = str.substr(0, str.size()-1);
    }
}


int main() {

    while(scanf("%d", &n) != EOF) {
        cnt = 0;
        string tmp;
        dfs(tmp, n-1);

    }
    return 0;

    
}
bubuko.com,布布扣

九度 1498:寻找表达式,布布扣,bubuko.com

九度 1498:寻找表达式

原文:http://www.cnblogs.com/xinsheng/p/3585403.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!