Smallest Difference POJ 2718(搜索)

原题

题目链接

题目分析

题目要求将一组数组成两个数,注意不能有前导零,要求差绝对值最小, 所以要取两个位数最接近的数来做差,然后搜就完事了.搜法可以用全排列搜,由于只取两个数,就可以直接取前half个数作为一个数,剩下的作为一个数,做差就行了,这样全排列刚好能遍历所有情况.

代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13
14 using namespace std;
15 const int INF=0x3f3f3f3f;
16
17 int num[20];
18 int ans;
19
20 void solve(int n)
21 {
22     do
23     {
24         if(num[0]||n==2||n==3)
25         {
26             int mid=n/2;
27             if(num[mid]||n==2)
28             {
29                 int x=num[0],y=num[mid];
30                 for(int i=1;i<mid;i++) x=x*10+num[i];
31                 for(int i=mid+1;i<n;i++) y=y*10+num[i];
32                 ans=min(ans,abs(x-y));
33             }
34         }
35     }while(next_permutation(num,num+n));
36 }
37
38 int main()
39 {
40 //    freopen("black.in","r",stdin);
41 //    freopen("black.out","w",stdout);
42     int T;
43     cin>>T;
44     getchar();
45     while(T--)
46     {
47         ans=INF;
48         char x;
49         int cnt=0;
50         while(~scanf("%c",&x)&&x!=‘\n‘) if(x>=‘0‘&&x<=‘9‘) num[cnt++]=x-‘0‘;
51         sort(num,num+cnt);
52         solve(cnt);
53         cout<<ans<<endl;
54     }
55     return 0;
56 }

原文地址:https://www.cnblogs.com/VBEL/p/11396859.html

时间: 2024-11-06 07:24:38

Smallest Difference POJ 2718(搜索)的相关文章

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

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

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

【POJ - 2718】Smallest Difference(搜索 )

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

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(贪心 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

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

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j