Description
Solution
小模拟(ex)
Code
#include <bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lc rt<<1
#define rc rt<<1|1
#define fi first
#define se second
const int mac=1e6+10;
typedef long long ll;
typedef pair<int,int> pill;
const int mod=1e9+7;
const int inf=1e9+10;
int a[10];
set<int>q;
char fuh[]={'+','-','*'};
int digt(char ch){if (ch>='0' && ch<='9') return 1; return 0;}
int solve(string s)
{
//cout<<s<<" ";
int dig[10];
char f[10];
int taild=0,tailf=0,len=s.length();
for (int i=0; i<len; i++){
int u=0;
if (digt(s[i])) {
while (digt(s[i])) u=u*10+s[i]-'0',i++;
dig[++taild]=u;
i--;
}
else if (s[i]=='*'){
int v=0; i++;
while (digt(s[i])) v=v*10+s[i]-'0',i++;
i--;
//taild--;
int p=v*dig[taild--];
dig[++taild]=p;
}
else f[++tailf]=s[i];
}
int head=2,ans=dig[1];
for (int i=1; i<=tailf; i++){
if (f[i]=='+') ans=ans+dig[head++];
else ans=ans-dig[head++];
}
//cout<<ans<<endl;
return ans;
}
int main(int argc, char const *argv[])
{
int n;
scanf ("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]);
sort(a+1,a+5);
for (;;){
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
for (int k=0; k<3; k++){
string s="";
s+=a[1]+'0'; s+=fuh[i];
s+=a[2]+'0'; s+=fuh[j];
s+=a[3]+'0'; s+=fuh[k];
s+=a[4]+'0';
int ans=solve(s);
if (ans>=0) q.insert(ans);
}
for (int i=0; i<3; i++)
for (int j=0; j<3; j++){
string s="";
s+=a[1]+'0'; s+=fuh[i];
s+=a[2]+'0'; s+=fuh[j];
s+=a[3]+'0'; s+=a[4]+'0';
int ans=solve(s);
if (ans>=0) q.insert(ans);
s="";
s+=a[1]+'0'; s+=a[2]+'0'; s+=fuh[i];
s+=a[3]+'0'; s+=fuh[j];
s+=a[4]+'0';
ans=solve(s);
if (ans>=0) q.insert(ans);
}
for (int i=0; i<3; i++){
string s="";
s+=a[1]+'0'; s+=fuh[i];
s+=a[2]+'0'; s+=a[3]+'0'; s+=a[4]+'0';
int ans=solve(s);
if (ans>=0) q.insert(ans);
s="";
s+=a[1]+'0'; s+=a[2]+'0'; s+=fuh[i];
s+=a[3]+'0'; s+=a[4]+'0';
ans=solve(s);
if (ans>=0) q.insert(ans);
s="";
s+=a[1]+'0'; s+=a[2]+'0'; s+=a[3]+'0'; s+=fuh[i];
s+=a[4]+'0';
ans=solve(s);
if (ans>=0) q.insert(ans);
}
if (!next_permutation(a+1,a+5)) break;
}
printf ("%d\n",q.size());
return 0;
}