【poj】2718全排列next—permutation
2017-03-16 20:00
309 查看
D - D
Crawling in process...
Crawling failed
Time Limit:1000MS
Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
给出0~9 若干个数,分成两组,组成两个数,使得差值最小
code:
Crawling in process...
Crawling failed
Time Limit:1000MS
Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
给出0~9 若干个数,分成两组,组成两个数,使得差值最小
code:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define INF 0xfffffff; using namespace std; int a[12]; int n; int main() { int t,l,i,j; scanf("%d", &t); while(t--) { n = 0; char c; while(scanf("%d", &a[n++])) { c = getchar(); if(c == '\n') break; } if(n==2) printf("%d\n", a[1]-a[0]); else { while(a[0] == 0) next_permutation(a, a + n); int mid = (n+1) / 2; int ans = INF; do { int sum1=0,sum2=0; if (a[mid]) { for (int i = 0; i < mid; ++ i) sum1 = sum1 * 10 + a[i]; for (int i = mid ; i < n; ++ i) sum2 = sum2 * 10 + a[i]; ans = min(ans, abs(sum1 - sum2));} } while(next_permutation(a, a + n)); printf("%d\n", ans); } } return 0; }
相关文章推荐
- POj-2718 Smallest Difference--全排列next_permutation()的应用文章标题
- 【poj】2718全排列next—permutation
- poj 1256 按一定顺序输出全排列(next_permutation)
- poj-1833-排列 stl next_permutation(a,a+n)
- poj 1833 排列 库函数 next_permutation 的调用
- POJ 1833 排列(字典序全排列、next_permutation()、copy+流迭代器)
- POJ-2718-Smallest Difference(next_permutation)
- POj 1883 数字排列 next_permutation解决之
- 有关next_permutation()函数的用法, poj 2718
- poj 题目1883 排列(STL----next_permutation)
- POJ 1833 排列(next_permutation)
- STL(next_permutation)poj 1833:排列
- POJ 2718 Smallest Difference(暴力,全排列,next_permutation)
- poj 1833 排列 next_permutation
- POJ 1833:排列 [#使用STL的next_permutation函数生成全排列#]
- HDU1716 -- 全排列 -- STL -- next_permutation()
- POJ 1731 Orders 按序输出一个字符串的全排列 next_permutation()
- 详谈全排列next_permutation() 函数的用法(推荐)
- 全排列 next_permutation
- 每日一题 No.14 全排列(利用next_permutation)