***POJ 2718 Smallest Difference【next_permination】
来源:互联网 发布:淘宝开店身份证泄露 编辑:程序博客网 时间:2024/06/14 09:17
原题链接
题意:数字排序组成两个数,求最小差值。
思路:
next_permutation函数的应用,历遍把数组所有的可能排列顺序。
为求最小差值,分割数组时应使两个数的位数差尽可能小(为1),选取中间值mid为第二个数的开头。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std;int num[10];int main() { int t,i,k; char ch; scanf("%d",&t); getchar(); while(t--) { k=0; while((ch=getchar())!='\n'){ if(ch>='0' && ch<='9') num[k++]=ch-'0'; } if(k==2) //处理只有两个数的情况 { printf("%d\n",num[1]-num[0]); continue; } int ans=999999999; do { if(num[0]==0) continue; int mid=(k+1)/2; //找到第二个数的开头 if(num[mid]) //第二个数不能以零开头 { int a=0,b=0; for(i=0;i<mid;i++) a=a*10+num[i]; for(i=mid;i<k;i++) b=b*10+num[i]; ans=min(ans,abs(b-a)); } }while(next_permutation(num,num+k)); printf("%d\n",ans); } return 0; }
1 0
- ***POJ 2718 Smallest Difference【next_permination】
- POJ 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ-2718-Smallest Difference
- poj 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ 2718-Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- Smallest Difference(POJ-2718)
- POJ 2718 Smallest Difference
- POJ-2718 Smallest Difference
- poj-2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ--2718 Smallest Difference
- poj-2718-Smallest Difference
- 使用BFS验证二分图
- Hash与Hash一致性
- linux命令行大全2
- Struts2中ognl中值栈
- 大数加法
- ***POJ 2718 Smallest Difference【next_permination】
- 字符串的全排列:看似繁杂,递归则简。
- jquery框架封装及解析原理+自己搭建jquery框架+注释
- JSP基础学习(二)
- liferay7.0开发的二三坑
- Ubuntu 16.04/10 Git配置及连接Github
- C++定义命名空间
- HDU5317 RGCDQ(数论素筛)
- Web安全颜色