uva 817According to Bartjens
来源:互联网 发布:淘宝卖家怎么修改物流 编辑:程序博客网 时间:2024/06/04 18:20
说到这题 我只想说,uva的测试数据真牛逼,面面俱到
此题其实就是一个暴力搜索,尝试完所有的情况,看其是否等于2000就行了,
一下是关键的几点 :2000=是 不可能的;注意排除具有前导0的数字的情况,
//// main.cpp// uva 817 - According to Bartjens//// Created by XD on 15/8/3.// Copyright (c) 2015年 XD. All rights reserved.////暴力搜索#include <iostream>#include <string>#include <queue>#include <stack>#include <stdio.h>#include <stdlib.h>#include <math.h>#include<vector>#include <string.h>#include <string>#include <algorithm>#include <set>#include <map>#include <cstdio>using namespace std ;char inter = 'A' ;//定义操作符char operation[3] = {'+','-' ,'*'} ;char s[30] ;int n ; //有多少个位置可以填充int md ; //有多少个位置插入了符号set<string> ans ; //存放结果的集合vector<int > pos ; //存放当前情况下的填充位置bool isnumber(char ch){ return ch >= '0' && ch <= '9' ;}//获取数字---如果是*连接的要在这里算出来-知道遇到+或者减号int getnum(int *p){ char num[10] ; int j = 0 ; while (*p < 2 * n + 1 && s[*p]!='+'&&s[*p]!= '-' ) { if (isnumber(s[*p])) { num[j++] = s[*p] ; } else{ if (s[*p] == '*') { if (num[0] == '0' && j > 1) { return -1 ; } else{ num[j] = '\0' ; ++(*p) ; int t = getnum(p) ; if (t!=-1) { return atoi(num) *t ; } return -1 ; } } } (*p)++ ; } num[j] = '\0' ; if (num[0] == '0' && j > 1) { return -1 ; } return atoi(num) ;}//运算函数int run(int a , int b , char operation){ switch (operation) { case '+': return a + b ; break; case '-': return a - b ; default: return a*b ; break; }}//判断是否等于2000bool equal2000(){ int a =0 ,b,ope = '+' ; for(int i = 0 ; i < 2 * n + 1 ; i++) { if(isnumber(s[i])){ b =getnum(&i) ; if(b==-1) { return false ; } i-- ; a = run(a , b , ope) ; } else if (s[i] == '+' || s[i] == '-') { ope = s[i] ; } } return a == 2000 ? true:false ;}void dfs(int d){ if (d == md) { if (equal2000()) { string result ; for (int i = 0 ; i < 2*n + 1; i++) { if(isnumber(s[i]) || s[i] == '*'|| s[i] == '+'||s[i] == '-') { result.operator+=(s[i]) ; } } ans.insert(result) ; } } else { for (int i = 0; i < 3; i++) { s[pos[d] * 2 + 1] = operation[i] ; dfs(d + 1) ; } }}void solved(){ int end = 1 << n ; for(int i = 1 ; i <end ; i++ ) { pos.clear() ; for (int j = 0; j < n; j++) { if (i &(1 << j)) { pos.push_back(j) ; } } md = (int )pos.size() ; dfs(0) ;// int len =(int ) pos.size() ; for (int j =0 ; j < md; j++) { s[2*pos[j]+1] = inter ; } }}int main() { char temp[15] ; int casenum =0 ; while (scanf(" %s" , temp)==1 && temp[0]!='=') { int len = (int )strlen(temp) ; s[len-1] ='\0' ; for (int i = 0; i < len-1; i++) { s[i*2] = temp[i] ; s[i*2+1] = inter ; } s[2*(len-2)+1] = '\0' ; n = len -2 ; ans.clear() ; solved() ; len = (int )ans.size() ; cout <<"Problem "<<++casenum <<endl; if (len == 0 ) { printf(" IMPOSSIBLE\n") ; } else{ for (set<string> ::iterator it = ans.begin(); it != ans.end(); it++) { cout <<" "<<*it <<"="<<endl ; } } } return 0;}
0 0
- UVA - 817 According to Bartjens
- uva 817According to Bartjens
- According to Bartjens UVA
- According to Bartjens UVA
- UVA 817 - According to Bartjens(暴力搜索)
- uva 817 - According to Bartjens(暴力)
- UVa 817 - According to Bartjens(暴力)
- UVA - 817 According to Bartjens 暴力
- UVa 817:According to Bartjens(DFS)
- Blog according to Scott Guthrie
- The Future According to ASP+
- How to send sysLog according to RFC3164
- G3: random number according to their weigtht
- According to TLD or attribute directive
- UUID objects according to RFC 4122
- vim add some characters according to lines
- Languages abbreviations in alphabetical order according to language
- jstl According to TLD or attribute directive in tag file
- 给Jquery动态添加的元素添加事件
- 时间特效
- spring 集成 kafka
- 南邮 OJ 1102 黑白图像
- 笔记
- uva 817According to Bartjens
- wind10 解决Genymotion出现Unable to load VirtualBox engin
- Qt自定义委托在QTableView中绘制控件、图片、文字
- iOS启动页延时
- ios-企业证书创建
- 南邮 OJ 1103 全排列
- OpenGL笔记之矩阵变换(Matrix Transformation)
- cocos HttpClient enableCookies 大坑
- C#的零星基础