codeforces55B. Smallest number

B. Smallest number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Vladimir got bad mark in algebra again. To avoid such unpleasant events in future he decided to train his arithmetic skills. He wrote four integer numbers abcd on the blackboard. During each of the next three minutes he took two numbers from the blackboard (not necessarily adjacent) and replaced them with their sum or their product. In the end he got one number. Unfortunately, due to the awful memory he forgot that number, but he remembers four original numbers, sequence of the operations and his surprise because of the very small result. Help Vladimir remember the forgotten number: find the smallest number that can be obtained from the original numbers by the given sequence of operations.

Input

First line contains four integers separated by space: 0 ≤ a, b, c, d ≤ 1000 — the original numbers. Second line contains three signs ('+' or '*' each) separated by space — the sequence of the operations in the order of performing. ('+' stands for addition, '*' — multiplication)

Output

Output one integer number — the minimal result which can be obtained.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).

Examples
input
1 1 1 1
+ + *
output
3
input
2 2 2 2
* * +
output
8
input
1 2 3 4
* + +
output
9

题意:给出4个正数和3操作符每次可以从四个数中选一个按照操作符所给的顺序运算再从剩下的数中在选两个运算求最后所剩的一个数最小是多少

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<list>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
char oper[5];
long long num[5],ans=1000000000000000ll;
long long calu(long long a,long long b,char o){
	if(o=='+')return a+b;
	if(o=='*')return a*b;
}
int main()
{
	scanf("%lld%lld%lld%lld",&num[0],&num[1],&num[2],&num[3]);
	getchar();
	scanf("%c %c %c",&oper[0],&oper[1],&oper[2]);
	int id[5];id[0]=0;id[1]=1;id[2]=2;id[3]=3;
	do
	{
		long long a=calu(num[id[0]],num[id[1]],oper[0]);
		long long b=calu(a,num[id[2]],oper[1]);
		long long temp=calu(b,num[id[3]],oper[2]);
		a=calu(num[id[0]],num[id[1]],oper[0]);
		b=calu(num[id[2]],num[id[3]],oper[1]);
		long long temp1=calu(a,b,oper[2]);
		ans=min(ans,min(temp,temp1));
	}while(next_permutation(id,id+4));
	printf("%lld\n",ans);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值