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 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

Source

Rocky Mountain 2005

题意:给2~10个不同的数字,要求把这些数字分成两堆,每一堆任意组合成一个数(开头不能为0),求两个数差的最小值。

思路:数据很小,直接暴力枚举所有情况,并且很容易想到,要使差最小则这两个数的位数应该尽量相等;枚举排列时用到了next_permutation函数。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int num[12];

int main()
{
    int i,j;
    int cas,cnt;
    char ch;
    sf(cas);
    getchar();
    while (cas--)
    {
        cnt=0;
        while ((ch=getchar())!='\n')
            if (ch>='0'&&ch<='9')
                num[cnt++]=ch-'0';
        if (cnt==2){
            pf("%d\n",abs(num[1]-num[0]));
            continue;
        }
        int ans=INF;
        while (num[0]==0)
            next_permutation(num,num+cnt);
        do{
            int mid=(cnt+1)>>1;
            if (num[mid])
            {
                int x=0,y=0;
                FRL(i,0,mid)
                    x=x*10+num[i];
                FRL(i,mid,cnt)
                    y=y*10+num[i];
                ans=min(ans,abs(x-y));
            }
        }while (next_permutation(num,num+cnt));
        pf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-25 07:42:17

Smallest Difference (poj 2718 暴力枚举)的相关文章

Smallest Difference POJ 2718

1.题目描述:点击打开链接 2.解题思路:本题利用暴力搜索法解决.因为数据规模比较小,可以直接利用next_permutation函数枚举所有排列.根据经验知,当两个数的位数差不多时,差值可能达到最下.因此枚举完分两种情况分别计算差值并取较小者.(1)[0,cnt/2)和[cnt/2,cnt).(2)[0,cnt/2+1)和[cnt/2+1,cnt).其中cnt是数组的长度.考虑第二种情况是因为有0的存在. 3.代码: #define _CRT_SECURE_NO_WARNINGS #inclu

Smallest Difference POJ 2718(搜索)

原题 题目链接 题目分析 题目要求将一组数组成两个数,注意不能有前导零,要求差绝对值最小, 所以要取两个位数最接近的数来做差,然后搜就完事了.搜法可以用全排列搜,由于只取两个数,就可以直接取前half个数作为一个数,剩下的作为一个数,做差就行了,这样全排列刚好能遍历所有情况. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #

POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

Smallest Difference 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

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】Smallest Difference(搜索 )

-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来说,给定数字0,1,2,4,6与7,你可以写出10和2467.当然写法多样:210和764,204和176,等等.最后一对数差的绝对值为28,实际上没有其他对拥有更小的差. Input  输入第一行的数表示随后测试用例的数量.对于每组测试用例,有一行至少两个

POJ2718 Smallest Difference 【贪心+枚举】

Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 1290 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

POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895   Accepted: 10906 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 1873 凸包+枚举

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1744 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been

hdu5143 暴力枚举

http://acm.hdu.edu.cn/showproblem.php?pid=5143 Problem Description NPY is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) is a sequence of numbers such that the difference between the consecutive term