【POJ2718】Smallest Difference(枚举排列)
来源:互联网 发布:济南网络服务公司 编辑:程序博客网 时间:2024/06/05 16:30
题目大意:
用给定的数字组成两个数,使差的绝对值最小。
题解:(暴力法,本题还有更好的贪心法)
两个数一定数位尽量相近,枚举所有数的排列,中间分开,求差。
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int n,dig[15],ans;int main(){ int T; scanf("%d\n",&T); int temp=-1; while(T--) { if(temp!=-1) dig[1]=temp; else scanf("%d",dig+1); for(int i=2;;i++) { if(scanf("%d",dig+i)!=1) {n=i-1;break;} if(dig[i]<=dig[i-1]) {temp=dig[i];n=i-1;break;} } ans=0x7FFFFFFF; sort(dig+1,dig+n+1); do { int len=n/2; if((dig[1]==0&&len>1)||(dig[len+1]==0&&n-len>1)) continue; int x=0,y=0; for(int i=1;i<=len;i++) x=x*10+dig[i]; for(int i=len+1;i<=n;i++) y=y*10+dig[i]; ans=min(ans,abs(x-y)); }while(next_permutation(dig+1,dig+n+1)); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- 【POJ2718】Smallest Difference(枚举排列)
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- POJ2718 Smallest Difference 【贪心+枚举】
- 枚举搜索+贪心:POJ2718--Smallest Difference(解一)
- 枚举库函数搜索:POJ2718--Smallest Difference(解二)
- POJ2718 Smallest Difference (暴力搜索+全排列)
- 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
- POJ2718——Smallest Difference
- ZOJ 2132 The Most Frequent Number 贪心
- 从对象深入分析 Java 中实例变量和类变量的区别
- 30天自制操作系统(第00天)
- 自学前端遇到瓶颈怎么办?
- matlab compiler & matlab coder
- 【POJ2718】Smallest Difference(枚举排列)
- handler内存泄露
- 从高考到程序员
- [Python爬虫]1.豆瓣电影Top250
- shader渲染器(BitmapShader 、LinearGradient 、RadialGradient )简单介绍
- 排序算法之堆排序
- 使用html5 history实现微信端,再按一次返回按钮 返回微信
- 关于__declspec(dllimport)的理解
- Jackson初次学习