HDU 3131 One…Two…Five! (暴力搜索)
来源:互联网 发布:亚特兰大机场数据 编辑:程序博客网 时间:2024/06/07 13:51
题目链接: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 __int64using 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);}elseprintf("No result\n");}return 0;}
1 0
- HDU 3131 One…Two…Five! (暴力搜索)
- URAL 1495. One-two, One-two 2(暴力枚举)
- Lesson one hundred twenty five Tea for two.
- 如何实现将'one,two,three,four,five' 分别打印
- 小学三年级题目 five+one+two=eight
- HDU 2699 Five in a Row (搜索)
- HDU 1399(搜索题,暴力- -!)
- HDU 1022(栈模拟;暴力搜索)
- hdu 4584 Building bridges(暴力搜索)
- HDU 4499 Cannon (暴力搜索)
- HDU 4499 Cannon (暴力搜索,回溯)
- HDU 5167 Fibonacci(DFS暴力搜索)
- 暴力搜索(HDU 5305,Friends)
- HDU-1128 暴力搜索
- hdu 5952 暴力搜索
- HDU 5563 Clarke and five-pointed star(暴力)
- - Five Dimensional Points(暴力)
- HDU 1795--The least one【二分 || 暴力】
- opencv学习1——Mat介绍
- Android 学习笔记 第五季 4 Size Qualifiers
- 八皇后
- 【 D3.js 进阶系列 — 2.2 】 力学图的参数
- nginx运维(1)nginx php mysql安装测试
- HDU 3131 One…Two…Five! (暴力搜索)
- jQuery??学还是不学 --- Dmore 来谈谈
- JQuery基本知识
- strcpy和memcpy的区别
- 1005. Spell It Right (20)
- 纳什:无常命运中的美丽心灵
- "高可用方案工具包" high availability toolkit 1.2 发布了。version 1.2 新增了 负载均衡 load balance 的技术实现
- 使用CURL来发送HTTP请求的方法
- Lua学习之6:面向对象编程