POJ 2718 暴力枚举
来源:互联网 发布:上瘾网络剧未删减全集 编辑:程序博客网 时间:2024/05/22 13:13
#include <cstdio>#include <iostream>#include <algorithm>#include <queue>#include <stack>#include <climits>#include <cstring>#include <cmath>#include <map>#include <set>#include <cctype>using namespace std;int k = 0;int ans;int fi[1050];int se[1050];int flag[15];int num[15];void fun(int t){if(t == k){int qian = 0,hou = 0;int q[15],h[15]; for(int i = 0;i < k;i++){if(flag[i]){q[qian++] = num[i];}else{h[hou++] = num[i];}}if(abs(qian - hou) > 1) return ;sort(q,q+qian);int n = 0,m = 0;do{int tn = 0;if(q[0] == 0 && qian != 1) continue;for(int i = 0;i < qian;i++){tn *= 10;tn += q[i];}fi[n++] = tn;//cout << tn << endl;//cout << n << endl;}while(next_permutation(q,q+qian));do{int tn = 0;if(h[0] == 0 && hou != 1) continue;for(int i = 0;i < hou;i++){tn*=10;tn += h[i]; }se[m++] = tn;//cout << tn << endl;}while(next_permutation(h,h+hou));for(int i = 0;i < n;i++){for(int j = 0; j < m;j++){int g = abs(fi[i] - se[j]);if(g < ans){ans = g;}}}}else{flag[t] = 1;fun(t+1); flag[t] = 0;fun(t+1);}}int main(){int n; //freopen("in.txt","r",stdin); //freopen("2.txt","w",stdout);scanf("%d",&n);getchar();while(n --){char a[100];gets(a);k = 0;for(int i = 0;a[i];i++){if(isdigit(a[i])){num[k++] = a[i] - '0';}}memset(flag,0,sizeof(flag));ans = INT_MAX;fun(0);cout << ans << endl;} return 0;}
0 0
- POJ 2718 暴力枚举
- Smallest Difference (poj 2718 暴力枚举)
- poj 2718 Smallest Difference (STL 暴力枚举)
- POJ:2718 Smallest Difference(暴力枚举)
- POJ 2718 Smallest Difference(暴力枚举)
- poj 3102(暴力枚举)
- POJ-----3279暴力枚举
- POJ 3174 暴力枚举
- poj 3080 暴力法 KMP+暴力枚举
- poj 1248 Safecracker(暴力枚举)
- POJ 1068 Parencodings(暴力枚举)
- poj 3671(暴力 枚举)
- POJ-2078 Matrix,暴力枚举!
- POJ 2718 Smallest Difference(贪心 or next_permutation()暴力枚举)
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
- poj 3450 KMP+暴力枚举(类似于3080)
- POJ 1226 Substrings 暴力枚举+KMP算法
- poj 1166 The Clocks 暴力枚举
- 数据结构与算法学习——冒泡排序
- 在MyEclipse中把JSP默认编码改为UTF-8
- IOS学习之简单的登陆布局(一)
- DBUtils架构分析
- poj3270 Cow Sorting
- POJ 2718 暴力枚举
- LeetCode 195 Tenth Line
- C++模板元编程(递归-->分解)
- Eclipse - Mac Os Default JRE missing
- LaTeX学习笔记
- mac:在当前文件夹打开terminal终端
- Android APK XML解析与反编译方法
- Java 8 详解之默认方法
- hiho 1000 : A + B (数学)