全自动数字论证机(迫真)
众所周知,OIer都事HOMO。本小鬼在看到知乎上问题后想起来以前暑假集训无聊的时候写的一个数字论证搜索,于是来写博客
由于python我没怎么用过,所以还是用的c++写的,并且事在晚上订正完考试写的,所以稍微有些麻烦。
谢谢茄子!
数据库生成
大体思路就事生成一个排列,然后通过现成的计算器生成答案并且保存。
生成排列
dfs即可。代码如下,主要用栈模拟
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma -g -std=c++11
using namespace std;
int t=0;
const int num[7]={0,1,1,4,5,1,4};
char s[10]={0,‘+‘,‘-‘,‘*‘,‘/‘,‘^‘,‘!‘};
const int mulj[4][7]={ {0,0,0,0,0,0,0},{0,1,1,2,1,1,2},{0,0,0,4,2,0,4},{0,0,0,0,0,0,0}};
const int lenj[7]={0,1,1,2,3,1,2};
string ss;
int maxn=0;
int numb=0;
stackans;
const int MAX = 30;
const int DONE = 1;
//栈定义
template
class Stack{
public:
Stack(int MaxStackSize=10);
~Stack() { delete [] stack;}
bool IsEmpty() const {return top==-1;}
bool IsFull() const {return top==MaxTop;}
T Top() const;
Stack& Add(const T& x);
Stack& Del(T& x);
void MakeEmpty(){top=-1;} //清空栈
void print(){
for(int i; i < top + 1; i ++){
cout<
}
cout<
}
private:
int top;//栈顶
int MaxTop;//最大的栈顶值
T *stack;//堆栈元素数组
};
template
Stack::Stack(int MaxStackSize){
MaxTop=MaxStackSize-1;
stack=new T[MaxStackSize];
top=-1;
}
template
Stack& Stack::Add(const T& x){
if(IsFull())
{cout<
top=top+1;
stack[top]=x;
return *this;
}
template
Stack& Stack::Del(T& x){
if(IsEmpty())
{cout<
x=stack[top];
top=top-1;
return *this;
}
template
T Stack::Top() const{
return stack[top];
}
//判断一个字符是否为数字
bool isNum(char c){
if((c > ‘0‘||c == ‘0‘)&&(c < ‘9‘||c == ‘9‘))
return true;
else
return false;
}
//删除字符串中的空格
void deleteBlank(string &s){
string::iterator i = s.begin();
while ((i=find(i, s.end(), ‘ ‘))!=s.end())
s.erase(i);
}
//计算器
class Calculator{
public:
Calculator(string s);
~Calculator();
int outPriority(char); //返回栈外优先级
int inPriority(char); //返回栈内优先级
bool judgePri(char, char); //判断优先级 前一个为栈外符号,后一个为栈内符号 若前大于后返回1,否则返回0
int judgePri(char); //判断运算符 若是‘#‘返回 -1,若是‘)‘返回 0,否则返回 1
void dealNum(); //处理数据
int calculate(); //计算
void setString(string const s){
this->s = ‘#‘ + s + ‘#‘;
deleteBlank(this->s); //删除字符串中的空格
}
private:
Stack *s_sym; //符号栈
Stack *s_num; //数据栈
string s;
};
Calculator::Calculator(string s){
this->s = ‘#‘ + s + ‘#‘;
deleteBlank(this->s);
s_sym = new Stack(MAX);
s_num = new Stack(MAX);
}
Calculator::~Calculator(){
delete s_sym;
delete s_num;
}
int Calculator::outPriority(char symble){
switch(symble){
case ‘#‘:
return 0;
case ‘(‘:
return 8;
case ‘+‘:
return 2;
case ‘-‘:
return 2;
case ‘*‘:
return 4;
case ‘/‘:
return 4;
case ‘%‘:
return 4;
case ‘^‘:
return 6;
case ‘)‘:
return 1;
default:
throw 1;
}
}
int Calculator::inPriority(char symble){
switch(symble){
case ‘#‘:
return 0;
case ‘(‘:
return 1;
case ‘+‘:
return 3;
case ‘-‘:
return 3;
case ‘*‘:
return 5;
case ‘/‘:
return 5;
case ‘%‘:
return 5;
case ‘^‘:
return 7;
case ‘)‘:
return 8;
default:
throw 1;
}
}
bool Calculator::judgePri(char out, char in){
if(outPriority(out) > inPriority(in))
return true;
else
return false;
}
int Calculator::judgePri(char symble){
if(symble == ‘#‘)
return -1;
else if(symble == ‘)‘)
return 0;
else
return 1;
}
void Calculator::dealNum(){
//将数据栈中的前两个弹出进行计算,结果放回数据栈,符号栈弹出顶部元素
char _temp = 0;
int dtemp1 = 0;
int dtemp2 = 0;
s_sym->Del(_temp);
s_num->Del(dtemp1);
s_num->Del(dtemp2);
switch(_temp){
case ‘+‘: