POJ-2718

Smallest Difference

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12158   Accepted: 3306

Description

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

题意:

将所给出的所有数字排列组合生成两个数,使其差的绝对值最小。求最小值。

贪心可做,学到了一种STL求全排列的方法:next_permutation();

AC代码:

 1 //#include<bits/stdc++.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8
 9 const int INF=1<<30;
10 char str[30];
11 int num[15];
12
13 int main(){
14     int n;
15     cin>>n;
16     getchar();
17     while(n--){
18         int res=INF;
19         gets(str);//得到数列
20         int len=strlen(str);
21         int k=0;
22         for(int i=0;i<len;i++){
23             if(str[i]>=‘0‘&&str[i]<=‘9‘){
24                 num[k++]=str[i]-‘0‘;
25             }
26         }
27         if(k==2){//如果只有两个数的情况
28             cout<<abs(num[0]-num[1])<<endl;
29             continue;
30         }
31         while(num[0]==0){//不能含有前导零
32             next_permutation(num,num+k);
33         }
34         //int ans=INF;
35         do{
36             int mid=(k+1)/2;
37             if(num[mid]){//第二个数也不能有前导零
38                 int a=0,b=0;
39                 for(int i=0;i<mid;i++){
40                     a=a*10+num[i];
41                 }
42                 for(int i=mid;i<k;i++){
43                     b=b*10+num[i];
44                 }
45                 res=min(res,abs(a-b));
46             }
47
48         }while(next_permutation(num,num+k));
49         cout<<res<<endl;
50     }
51     return 0;
52 }
时间: 2024-08-15 00:51:11

POJ-2718的相关文章

POJ 2718 Smallest Difference 未AC 《挑战程序设计竞赛》

题目:POJ 2718 思路: 分为奇数和偶数两种情况进行处理,输入个数为奇数的时候,无须穷举,最小差是一定的,如0 1 2 3 4,最小差为102 - 43. 输入个数为偶数的时候,用next_permutation穷举. 没有AC-- 1 #include <iostream> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdio.h> 5 #include <string.h

poj 2718 最优比例生成树(01分数规划)模板

/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1100 #define eps 1e-10 #define inf 0x3fffffff struct node { int u,v,w; }p[N]; double ma[N][N]; double distance(int i,int j) { return sqrt(1.0*(p[i].u-p[j].u

(暴力+深搜)POJ - 2718 Smallest Difference

原题链接: http://poj.org/problem?id=2718 题意: 给你几个数字,可以分成两个子集,然后分别按一定顺序排列组成一个数,求出这两只值差的绝对值的最小值. 分析: 反正也是刷着玩,果断先交一波全排列枚举的代码,果断TLE,然后开始想正解. 稍微想想,既然要差最小,肯定是两个数各一半.所以只要深搜出所有n/2(n为给定数字的个数)的组合,另外个n-n/2个数就有了. 但是枚举出来后的操作又想了很久,想过很多算法,都不怎么满意,最终用二分解决. 先把n/2和n-n/2全排列

POJ 2718 Smallest Difference (穷竭搜索)

http://poj.org/problem?id=2718 将一个数切一刀拆成两个数,两个数每一位数字的顺序都可改变,但是不能有前导0.求这两个数之差的最小值. 我使用了搜索并且避免了递归,自认为是比较好的算法. 一开始我想到的是枚举,并且很快给出了实现: #ifndef ONLINE_JUDGE #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <al

穷竭搜索: POJ 2718 Smallest Difference

题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一刀,比如  n1:[1 4 5],n2:[6 8 9]这样,然后 abs(n1- n2),对n1 和 n2的所有可能的排列 n1: [1 4 5][1 5 4]...这样,要算出来的最小的差,显然从中间切一刀才会出现这种解. 题解: 这里可以用来练习 STL,算法不会也没有关系,可以在这题学到 bi

POJ 2718 (Smallest Different)

题目链接:http://poj.org/problem?id=2718 题意: 将所给出的所有数字排列组合生成两个数,使其差的绝对值最小.求最小值. 这是一道穷竭搜索类型的题目,难度不大,还学到了 next_permutation 函数, 写好了一个代码后提交却是TLE!太搞人心态了,修改后有时报错,不得不说小白太辣鸡.. 这道题的坑就在于对输入数字为1和2特殊情况的单独列取 ac代码: #include <iostream> #include <algorithm> #inclu

POJ - 2718 Smallest Difference(全排列)

题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举n个数字的全排列. 2.当两组数字个数相同或只差1时组成的两个数字才可能出现最小差值. 3.0~cnt/2 - 1为前半组数字,cnt/2~cnt-1为后半组数字. 4.注意getchar()的位置. #pragma comment(linker, "/STACK:102400000, 102400

poj 2718 Smallest Difference(穷竭搜索dfs)

Description 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

Smallest Difference (poj 2718 暴力枚举)

Language: Default Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5034   Accepted: 1382 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits an

poj 2718 Smallest Difference(暴力枚举)(贪心待补充)

abs写成fabs导致WA了一发. 关于next_permutation()这个STL里面的函数的基本应用 http://www.cnblogs.com/luosuo10/p/5479188.html 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace s