Smallest Difference(暴力全排列)
2017-03-14 16:34
183 查看
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 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
Sample Output
Source
Rocky Mountain 2005
题解:给一串0到9的数,选择几个组成num1,剩下的组成num2,问最小的差值,当有两个数字时不能0开始;暴力,先选数字然后全排列
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10387 | Accepted: 2836 |
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
题解:给一串0到9的数,选择几个组成num1,剩下的组成num2,问最小的差值,当有两个数字时不能0开始;暴力,先选数字然后全排列
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ans; int vis[10]; int p[10] = {0,1,10,100,1000,10000,100000,1000000,10000000,100000000}; int get(int* a){ int i = 0; while(1){ char c = getchar(); if(c == ' ')continue; if(c == '\n')return i; a[i ++] = c - '0'; vis[c - '0'] = 1; } } int cal(int *a, int n){ int x = 0; for(int i = 0; i < n; i++){ // printf("%d ", a[i]); x = x * 10 + a[i]; }//printf("\n计算的结果是:\n", x); return x; } void distribute(int* a, int an, int* l, int ln, int* r, int rn, int i){ if(i == an){ // puts("l的元素是:"); // for(int j = 0; j < ln; j++){ // printf("%d ", l[j]); // }puts(""); // // puts("r的元素是:"); // for(int j = 0; j < rn; j++){ // printf("%d ", r[j]); // }puts(""); if(ln == 0 || rn == 0){ return; } if(abs(rn - ln) > 1){ return; } do{ do{ if(ln > 1 && l[0] == 0){ continue; } if(rn > 1 && r[0] == 0){ continue; } int x = cal(l, ln); int y = cal(r, rn); // printf("x = %d\ny = %d\n", x, y); if(abs(x - y) <= ans){ ans = abs(x - y); } }while(next_permutation(r, r + rn)); }while(next_permutation(l, l + ln)); return; } l[ln] = a[i]; distribute(a, an, l, ln + 1, r, rn, i + 1); r[rn] = a[i]; distribute(a, an, l, ln, r, rn + 1, i + 1); } int main(){ int T; scanf("%d", &T); getchar(); int a[10], l[10], r[10]; while(T--){ memset(vis, 0, sizeof(vis)); int n = get(a); ans = 0x3f3f3f3f; // if(n == 10){ // puts("247"); // continue; // }else if(n == 9){ // int m[10] = {2469,10469,469,369,359,358,359,369,469,1469}; // for(int i = 0; i < 10; i++){ // if(!vis[i]){ // printf("%d\n", m[i]); // } // } // continue; // } distribute(a, n, l, 0, r, 0, 0); printf("%d\n", ans); } return 0; }
相关文章推荐
- Smallest Difference (全排列 + 暴力?)
- POJ2718 Smallest Difference (暴力搜索+全排列)
- POJ 2718 Smallest Difference(暴力,全排列,next_permutation)
- poj 2718 Smallest Difference(暴力)
- POJ:2718 Smallest Difference(暴力枚举)
- poj 2718 Smallest Difference 穷竭搜索之全排列
- POJ-----2718---Smallest Difference---暴力
- poj 2718 Smallest Difference 【dfs(全排列变形题)】
- 【POJ】2718 - Smallest Difference(枚举全排列)
- Smallest Difference:POJ - 2718(全排列)
- POJ 2718 Smallest Difference(暴力枚举)
- POJ 2718 Smallest Difference (dfs)
- 历届试题 带分数 (暴力+全排列)
- POJ 2718 Smallest Difference
- poj2718 Smallest Difference【贪心】
- poj 2718 Smallest Difference
- Smallest Difference-模拟
- HDU1755 A Number Puzzle【全排列+暴力】
- poj 2718 Smallest Difference
- 深度优先搜索(DFS):POJ2718--Smallest Difference(解三)