HDU 3131 One…Two…Five! (暴力搜索)
题目链接: pid=3131">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;
}
HDU 3131 One…Two…Five! (暴力搜索)的更多相关文章
- HDU 2616 Kill the monster (暴力搜索 || 终极全阵列暴力)
主题链接:HDU 2616 Kill the monster 意甲冠军:有N技能比赛HP有M怪物,技能(A,M),能伤害为A.当怪兽HP<=M时伤害为2*A. 求打死怪兽(HP<=0)用的 ...
- HDU 1399 Starship Hakodate-maru(暴力搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1399 Starship Hakodate-maru Time Limit: 2000/1000 MS ...
- hdu 1399 Starship Hakodate-maru (暴力搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399 题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值. #inc ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- 随手练——洛谷-P1151(枚举与暴力搜索)
枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...
随机推荐
- Codefroces B. Hamming Distance Sum
Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...
- xcode 条件调试
添加条件 有时候我们可能会在某个循环中创建断点,但一次又一次地点击 continue 直到我们想要的条件出现,显然是一种非常低效的方式.好在 Xcode 为我们提供了条件断点. 首先在下列代码中插入一 ...
- WebAssembly学习(六):AssemblyScript - 限制与类型
一.限制 将无类型的JavaScript编译为WebAssembly没有意义,因为它最终会导致运行其中较慢的一个JavaScript. 相反,AssemblyScript专注于WebAssembly擅 ...
- U-BOOT概述及源码分析(一)
嵌入式Linux系统从软件角度通常可以分为以下4个层次: 引导加载程序 | Linux内核 | 文件系统 | 用户应用程序 嵌入式Linux系统中典型分区结构: 正常启动过程中,Bootloader首 ...
- 紫书 例题 9-7 UVa 11584 (线性结构上的动态规划)
这道题判断回文串的方法非常的秀! 这里用到了记忆化搜索,因为会有很多重复 同时用kase来区分每一组数据 然后还有用递归来判断回文,很简洁 然后这种线性结构的动态规划的题,就是把 当前的这个数组分成两 ...
- 紫书 习题 10-19 UVa 10868 (物理动能定理)
这道题看起来很长,而实际上就是考物理 可以用动能定理来算出末速度. 同时注意要特判绳子比桥还长的情况. #include<cstdio> #include<cmath> #de ...
- [Python] Read and Parse Files in Python
This lesson will teach you how to read the contents of an external file from Python. You will also l ...
- 《linux 内核全然剖析》编译linux 0.12 内核 Ubuntu 64bits 环境
我×.. . 最终好了,大概3 4个小时吧...各种毛刺问题.终究还是闯过来了.. .. ubuntu2@ubuntu:~/Downloads/linux-0.00-050613/linux-0.00 ...
- 【HDU 4763】Theme Section(KMP)
这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...
- vue1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...