【POJ】最小差值
来源:互联网 发布:装修效果图设计软件 编辑:程序博客网 时间:2024/06/05 00:23
【POJ】最小差值
题目
Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.
For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.
Input
The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, …, 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.
Output
For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.
Sample Input
1
0 1 2 4 6 7
Sample Output
28
解析
1.手写排列
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;string s1;int a[15],Min,p,n;int ABS(int a){ return a<0?-a:a;}void swap(int &x,int &y){ int t = x;x = y;y = t;}void Fun(int x){ if( x == n ) { int A = 0,B = 0; if( a[1] != 0 && a[p] != 0 ) { for(int j=1;j<p;j++) A = A * 10 + a[j]; for(int j=p;j<=n;j++) B = B * 10 + a[j]; if( Min > ABS(A-B) ) Min = ABS(A-B); } return ; } for(int i=x;i<=n;i++) { swap(a[x],a[i]); Fun(x+1); swap(a[x],a[i]); }}int main(){ int Case; scanf("%d\n",&Case); for(int i=1;i<=Case;i++) { getline(cin,s1); n = 0; for(int j=0;j<int(s1.size());) { a[++n] = s1[j] - '0'; j = j + 2; } sort(a+1,a+n+1); Min = 10000000;p = (n+1)/2+1; Fun(1); if( n == 2 ) printf("%d\n",ABS(a[1]-a[2])); else printf("%d\n",Min); }}
2. next_permutation#include
#include<iostream>#include<algorithm>using namespace std;string s1;int a[15];int ABS(int a){ return a<0?-a:a;}int main(){ int Case; scanf("%d\n",&Case); for(int i=1;i<=Case;i++) { getline(cin,s1); int n = 0; for(int j=0;j<int(s1.size());) { a[++n] = s1[j] - '0'; j = j + 2; } sort(a+1,a+n+1); int Min = 10000000,p = (n+1)/2+1; do { int A = 0,B = 0; if( a[1] != 0 && a[p] != 0 ) { for(int j=1;j<p;j++) A = A * 10 + a[j]; for(int j=p;j<=n;j++) B = B * 10 + a[j]; if( Min > ABS(A-B) ) Min = ABS(A-B); } }while(next_permutation(a+1,a+n+1)); if( n == 2 ) printf("%d\n",ABS(a[1]-a[2])); else printf("%d\n",Min); }}
3. 贪心
#include<cstdio>#include<algorithm>#include<iostream>using namespace std;string s1;int a[15];int main(){ int Case; scanf("%d\n",&Case); for(int i=1;i<=Case;i++) { getline(cin,s1); int n = 0; for(int j=0;j<int(s1.size());) { a[++n] = s1[j] - '0'; j = j + 2; } sort(a+1,a+n+1); int A = 0,B = 0; if( n % 2 == 1 ) { if( a[1] == 0 ) { A = a[2]*10 + a[1]; for(int j=3;j<=n/2+1;j++) A = A * 10 + a[j]; } else { A = a[1]; for(int j=2;j<=n/2+1;j++) A = A * 10 + a[j]; } B = a[n]; for(int j=n-1;j>n/2+1;j--) B = B * 10 + a[j]; printf("%d\n",A-B); } else { int Min = 10000; for(int i=1;i<n;i++) { if( a[i] == 0 ) continue; A = a[i+1];B = a[i]; int s = 1,t = n; while( s <= t ) { while( s == i || s == i+1 ) s++; while( t == i || t == i+1 ) t--; if( s <= t ) A = A * 10 + a[s]; if( s <= t ) B = B * 10 + a[t]; s++;t--; } if( A - B < Min ) Min = A - B; } if( n == 2 ) printf("%d\n",a[2] - a[1]); else printf("%d\n",Min); } }}
- 【POJ】最小差值
- 最小差值
- POJ 3264 Balanced Lineup(最大最小差值 线段树水题)
- POJ 3522 Slim Span(最小差值生成树)
- POJ 3522 最大边与最小边差值最小的生成树
- GDUT2012-1002最小差值
- 最小差值 小水题
- 最小差值的划分
- dfs 求差值最小
- POJ 3522 最大边与最小边差值最小的生成树(最小生成树的性质)
- Poj 3522 最长边与最短边差值最小的生成树
- POJ 1789 权值为字符串差值的最小生成树
- 找差值最小的数
- 划分数组,求最小差值
- 530. BST间最小差值
- RMQ算法求最大最小差值
- 和差值最小子数组划分
- POJ3522差值最小的生成树
- BZOJ 1122: [POI2008]账本BBB
- Picked up _JAVA_OPTIONS: -Xmx900M”
- C语言实现:合并两个有序的数组,合并后的数组依然有序
- com.alibaba.druid:type=DruidDataSourceStat报错
- pat 1005. Spell It Right (20)
- 【POJ】最小差值
- MFC 集成 Caffe导出的dll文件做人脸识别
- 4. 如何使用SolidWorks软件导出URDF机器人模型文件
- Kafka Creating Topic Error: replication factor larger than available brokers
- php设置报错级别,项目后台使用YII框架开发
- nanopi 烧写系统+连接wifi+stfp
- 5. 编码器与PID控制
- IntelliJ IDEA For Mac 快捷键
- Java画面精美的简易聊天室