POJ2718 Smallest Difference (暴力搜索+全排列)
来源:互联网 发布:javascript读取html 编辑:程序博客网 时间:2024/05/19 17:59
题目链接:传送门
题意:
给你一个长度为n的数列 ,0<=a[i]<=9;用他们组成两个数
然后使两个数的差最小,求这个数。不能有前导0。
分析:
要使差最小肯定要使两个数的长度尽量相等,一个为n/2,另
一个为n-n/2.然后dfs搜一下选的数,然后另一个数用剩下的
数做一个全排列就可以了。
代码如下:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int mmax = 999999999;int a[10];bool vis[10];int cnt,ans;void solve(int val){ int tmp = 0; int b[10]; int tot= 0; for(int i=0;i<cnt;i++){ if(!vis[i]) b[tot++]=a[i],tmp=tmp*10+a[i]; } do{ tmp = 0; for(int i=0;i<tot;i++) tmp=tmp*10+b[i]; if(b[0]!=0||tot==1) ans = min(ans,abs(tmp-val)); }while(next_permutation(b,b+tot));}void dfs(int val,int num){ if(num==cnt/2){ solve(val); return; } for(int i=0;i<cnt;i++){ if(!vis[i]){ if(num==0&&a[i]==0&&cnt>3) continue; vis[i]=1; dfs(val*10+a[i],num+1); vis[i]=0; } }}int main(){ int t; scanf("%d",&t); getchar(); while(t--){ cnt = 0; char s[100]; gets(s); for(int i=0;i<strlen(s);i++) if(s[i]>='0'&&s[i]<='9') a[cnt++]=s[i]-'0'; memset(vis,0,sizeof(vis)); ans = mmax; dfs(0,0); printf("%d\n",ans); } return 0;}
0 0
- POJ2718 Smallest Difference (暴力搜索+全排列)
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- POJ2718-Smallest Difference-穷竭搜索
- Smallest Difference (全排列 + 暴力?)
- POJ2718——Smallest Difference(暴力瞎搞)
- 【POJ2718】Smallest Difference(枚举排列)
- POJ 2718 Smallest Difference(暴力,全排列,next_permutation)
- poj2718 Smallest Difference
- poj2718 Smallest Difference
- POJ2718---Smallest Difference
- [dfs]poj2718 Smallest Difference
- POJ2718-Smallest Difference
- Smallest Difference poj2718
- POJ2718 Smallest Difference
- POJ2718-Smallest Difference
- poj2718 Smallest Difference【贪心】
- POJ2718 Smallest Difference
- poj2718 Smallest Difference
- 半同步半异步线程池的实现(C++11)
- SpringMVC入门
- 怎么面试架构师【注重招式还是心法】
- javascript 改变html样式常用属性
- Android Service与Activity之间通信的几种方式
- POJ2718 Smallest Difference (暴力搜索+全排列)
- Cocos2d-x PNG图片资源加密
- redis常用内存优化手段和参数含义
- 手把手教你:解决python UnicodeDecodeError: 'gb2312' codec can't decode问题
- C++通过引用来传递和返回类对象
- mysql产生随机数
- 用栈和队列实现魔王语言
- 按回车自动跳到下一个dialog
- Redis安装问题