HDU 3131 One…Two…Five! (暴力搜索)

题目链接:HDU 3131 One…Two…Five! (暴力搜索)

题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6=48),求所有结果中,含有‘3’且该数字出现频率最大,若频率相等,输出数字最大的。

暴力解决之

AC代码:

#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#define LL __int64
using namespace std;
LL a[20],cnt,k;
map<LL,LL> ans;
map<LL,LL>::iterator it;
bool cmp(LL a,LL b)
{
	return a>b;
}

LL cal(LL sum,LL temp,LL op)
{
	if(op==0)
		return sum+temp;
	if(op==1)
		return sum-temp>0? sum-temp:temp-sum;
	if(op==2)
		return sum*temp;
	if(op==3)
		return sum/temp;
}

void dfs(LL x,LL sum)
{
	if(x==cnt)
	{
		LL tempsum=sum;
		while(tempsum)
		{
			if(tempsum%10==3)
			{
				ans[sum]++;
				k++;
				//printf("%I64d\n",sum);
				break;
			}
			tempsum/=10;
		}
		return  ;
	}
	for(LL i=0;i<4;i++)
	{
		if(i==3 && a[x]==0)
			continue;
		LL temp=cal(sum,a[x],i);
		dfs(x+1,temp);
	}
}

int main()
{
	char s[100];
	LL len,i;
	while(gets(s))
	{
		if(s[0]=='#')
			break;
		ans.clear();
		len=strlen(s);
		LL temp;
		cnt=temp=0;
		for(i=0;i<len;i++)
		{
			if(s[i]==' ')
			{
				a[cnt++]=temp;
				temp=0;
				continue;
			}
			temp=temp*10+s[i]-'0';
		}
		a[cnt++]=temp;
		k=0;
		dfs(1,a[0]);
		if(k)
		{
			LL maxt=-1,maxans;
			for(it=ans.begin();it!=ans.end();it++)
			{
				if(maxt<=(it->second))
				{
					maxt=it->second;
					maxans=it->first;
					if((it->first)>maxans)
						maxans=it->first;
				}
			}
			printf("%I64d\n",maxans);
		}
		else
			printf("No result\n");
	}
	return 0;
}
时间: 2024-08-05 11:05:03

HDU 3131 One…Two…Five! (暴力搜索)的相关文章

hdu 1399 Starship Hakodate-maru (暴力搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399 题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值. 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 int j,

HDU 2616 Kill the monster (暴力搜索 || 终极暴力全排列)

题目链接:HDU 2616 Kill the monster 题意:有N个技能去打HP有M的怪兽,技能(A,M),技能伤害为A,当怪兽HP<=M时伤害为2*A.求打死怪兽(HP<=0)用的最少技能 方法一:将技能全排列,计算伤害,得到答案. 方法二:搜索,具体看代码. 全排列AC代码: #include<stdio.h> #include<algorithm> using namespace std; struct node { int p,v; }; struct n

hdu 4770 Lights Against Dudely 暴力搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 题目大意是让你放一些灯照亮一些房间 灯不可以照到某些特殊房间 但可以照出边界 灯光必须覆盖所有可以放灯的房间 求至少需要多少灯 搜索题 应该注意到冗长而无聊的题面中有告诉你最多只有15个可以放灯的房间 所以2^15来枚举 还应该注意到冗长而无聊的题面中有告诉你最多只有1个灯可以转向 所以还应该枚举哪个灯来转向 转向再枚举4个方向 然后判断是否成立 为了简化问题 单独把那些可放灯空间拿出来 最多

HDU ACM 1045 Fire Net 暴力搜索

分析:放过炮台的标记为' @ '(回溯要还原) 递归,下一次遍历时向四个方向延伸(一行或者一列),找到'@' 则标记为不行,若找到'X'或边界则标记为行. 这题貌似还可以用二分匹配做. #include<iostream> using namespace std; #define N 5 char map[N][N]; int dir[4][2]={-1,0,0,-1,1,0,0,1}; int n,max; bool judge(int x,int y) { int i,tx,ty; for

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

hdu 1427 速算24点 dfs暴力搜索

速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13).要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次).游戏很简单,但遇到无解的

ACM 暴力搜索题 题目整理

UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vector> #include<cmath> #include<map> #include<algorithm> #include<cstring> #include<cstdio> #include<cstdlib> #include

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks o