博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2718 Smallest Difference
阅读量:4604 次
发布时间:2019-06-09

本文共 2685 字,大约阅读时间需要 8 分钟。

                                                                                              Smallest Difference
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8418   Accepted: 2313

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

10 1 2 4 6 7

Sample Output

28 题意:输入若干数码,将这些数码任意划分成两部分并组合成两个整数,获得一个差值,求所有情况中差最小是多少 思路:首先划分时两个数的位数越接近越好。      思考若有N个数码,切割数码(或者说挑选数码)总共情况有2^N种,可以考虑成二进位制,**...*,共有N个星号,每个星号代表1或者0,分别表示每个数码取或者不取,这样对于每一种情况都能够将数码分成两块,对这两块数码都分别穷举各种排列方式求差的最小值。 AC代码:
#include
#include
#include
#include
#include
using namespace std;int main() { int t; cin >> t; cin.ignore(); while (t--) { string s; getline(cin, s); s.erase(remove(s.begin(),s.end(),' '),s.end()); int length = s.size(); int cut = length / 2; int permute = 1 << length; int result = INT_MAX; do { bitset<10>used = static_cast
<10>>(permute);//将permute转化成有十位的二进制 string s1, s2; for (int i = 0;i < length;i++) { if (used[i])//每一个数码取或者不取 s1 += s[i]; else s2 += s[i]; } if (s1.size() != cut) continue; if (s1[0] == '0'&&s1[0]>1) continue; //切割完毕,穷举 do { int a = atoi(s1.c_str()); do { if (s2[0] == '0'&&s2.size() > 1) continue; int b = atoi(s2.c_str()); int c = abs(a - b); if (c < result) result = c; } while (next_permutation(s2.begin(), s2.end())); } while (next_permutation(s1.begin(), s1.end())); } while (permute--); cout << result << endl; } return 0;}

 

转载于:https://www.cnblogs.com/ZefengYao/p/5826465.html

你可能感兴趣的文章
需求说明书
查看>>
比特币——点对点电子现金系统
查看>>
以 Ext.Net 1.2.0 为例了解网页测试工具 HttpWatch
查看>>
金融资产管理项目-银企直连
查看>>
Python函数式编程
查看>>
UI学习第一篇 (控件)
查看>>
水形文字
查看>>
(C++)虚函数表解析(转)
查看>>
python基础补充
查看>>
Tomcat的工作模式和运行模式
查看>>
计算机网络笔记2
查看>>
简单的sql server连接
查看>>
xmlSpy已经破解了,但是每次启动都会自动关闭,下面的方法可以屏蔽xmlSpy联网。...
查看>>
华为命令笔记
查看>>
十大经典三维动画制作软件
查看>>
JavaMailSender邮箱配置
查看>>
Leetcode 672.灯泡开关II
查看>>
【转】Android Fragment 基本介绍--不错
查看>>
【转】使用Python的IDE:Eclipse+PyDev
查看>>
Mybatis学习笔记--1
查看>>