2020 ICPC Asia Taipei-Hsinchu Site B. Make Numbers(模拟)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值