poj 2718 Smallest 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

本来是一道不难的题,结果一直wa,wa,wa,wa,wa,wa,wa...开始的代码是这样
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6
 7 int n;
 8 char temp[20];
 9 int num[20];
10 int main(int argc, char const *argv[])
11 {
12     //freopen("input.txt","r",stdin);
13     scanf("%d",&n);
14     getchar();
15     while(n--) {
16         gets(temp);
17         int len = strlen(temp);
18         int m = 0;
19         int ans = 99999;
20         for(int i = 0; i < len; i++) {
21             if(temp[i] != ‘ ‘) {
22                 num[m++] = temp[i] - ‘0‘;
23             }
24         }
25         if(m & 1) {
26             int sn = m / 2;
27             int fn = m - sn;
28             int fir = 0, sec = 0;
29             if(num[0] == 0) {
30                 swap(num[0], num[1]);
31             }
32             for(int i = 0; i < fn; i++) {
33                 fir = fir * 10 + num[i];
34             }
35             for(int i = m; i >= fn; i--) {
36                 sec = sec * 10 + num[i];
37             }
38             ans = fir - sec;
39         }
40         else {
41             int a = 0, b;
42             if(num[0] == 0) {
43                 a = 1;
44             }
45             int fn = m / 2;
46             for(; a < m-1; a++) {
47                 for(b = a + 1; b < m; b++) {
48                     int fir = num[b];
49                     int j = 1;
50                     for(int i = 0; i < m && j < fn; i++) {
51                         if(i != a && i != b) {
52                             fir = fir * 10 + num[i];
53                             j++;
54                         }
55
56                     }
57                     int sec = num[a];
58                     j = 1;
59                     for(int i = m-1; i >= 0 && j < fn; i--) {
60                         if(i != a && i != b) {
61                             sec = sec * 10 + num[i];
62                             j++;
63                         }
64                     }
65                     int tmin = fir - sec;
66                     if(tmin < ans) {
67                         ans = tmin;
68                     }
69                 }
70             }
71
72         }
73         printf("%d\n",ans);
74     }
75     return 0;
76 }

一直提交错误,真是悲哀

最后终于知道是什么错了

如果以0开头,应该多加一个判断。另外防止输入格式的问题

代码如下

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <queue>
 5 #include <iostream>
 6 using namespace std;
 7
 8 int n;
 9 char temp[20];
10 int num[20];
11 int main(int argc, char const *argv[])
12 {
13     //freopen("input.txt","r",stdin);
14     while(scanf("%d",&n) != EOF) {
15         char c;
16         while(c = getchar() != ‘\n‘);
17         while(n--) {
18             gets(temp);
19             int len = strlen(temp);
20             int m = 0;
21             int ans = 99999;
22             for(int i = 0; i < len; i++) {
23                 if(temp[i] <= ‘9‘ && temp[i] >= ‘0‘) {
24                     num[m++] = temp[i] - ‘0‘;
25                 }
26             }
27             if(m & 1) {
28                 int sn = m / 2;
29                 int fn = m - sn;
30                 int fir = 0, sec = 0;
31                 if(num[0] == 0) {
32                     swap(num[0], num[1]);
33                 }
34                 for(int i = 0; i < fn; i++) {
35                     fir = fir * 10 + num[i];
36                 }
37                 for(int i = m - 1; i >= fn; i--) {
38                     sec = sec * 10 + num[i];
39                 }
40                 ans = fir - sec;
41             }
42             else {
43                 int a = 0, b;
44                 int fn = m / 2;
45                 if(num[a] == 0 && fn != 1) {
46                     a =1;
47                 }
48                 for(; a < m-1; a++) {
49                     for(b = a + 1; b < m; b++) {
50                         int fir = num[b];
51                         int j = 1;
52                         for(int i = 0; i < m && j < fn; i++) {
53                             if(i != a && i != b) {
54                                 fir = fir * 10 + num[i];
55                                 j++;
56                             }
57
58                         }
59                         int sec = num[a];
60                         j = 1;
61                         for(int i = m-1; i >= 0 && j < fn; i--) {
62                             if(i != a && i != b) {
63                                 sec = sec * 10 + num[i];
64                                 j++;
65                             }
66                         }
67                         int tmin = fir - sec;
68                         if(tmin < ans) {
69                             ans = tmin;
70                         }
71                     }
72                 }
73
74             }
75             printf("%d\n",ans);
76         }
77     }
78     return 0;
79 }

专门为这个题还写了一个生成测试用例的程序

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <queue>
 5 #include <iostream>
 6 using namespace std;
 7
 8 int flag[12];
 9 int num[10];
10 int ans;
11 void dfs(int n, int step) {
12     if(step == n) {
13         for(int i = 0; i < n; i++) {
14             printf("%d ",num[i]);
15         }
16         puts("");
17         ans++;
18         return;
19     }
20     for(int i = 0; i <= 9; i++) {
21         if(flag[i] == 0 && (step == 0 || i > num[step-1])) {
22             num[step] = i;
23             flag[i] = 1;
24             dfs(n, step+1);
25             flag[i] = 0;
26         }
27
28     }
29 }
30 int main(int argc, char const *argv[])
31 {
32     freopen("output.txt","w",stdout);
33     ans = 0;
34
35     memset(flag, 0, sizeof(flag));
36     for(int n = 2; n <= 10; n++) {
37         dfs(n, 0);
38     }
39     printf("%d\n",ans);
40     return 0;
41 }

附上测试用例如下

1013
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 3
2 4
2 5
2 6
2 7
2 8
2 9
3 4
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9
5 6
5 7
5 8
5 9
6 7
6 8
6 9
7 8
7 9
8 9
0 1 2
0 1 3
0 1 4
0 1 5
0 1 6
0 1 7
0 1 8
0 1 9
0 2 3
0 2 4
0 2 5
0 2 6
0 2 7
0 2 8
0 2 9
0 3 4
0 3 5
0 3 6
0 3 7
0 3 8
0 3 9
0 4 5
0 4 6
0 4 7
0 4 8
0 4 9
0 5 6
0 5 7
0 5 8
0 5 9
0 6 7
0 6 8
0 6 9
0 7 8
0 7 9
0 8 9
1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 2 8
1 2 9
1 3 4
1 3 5
1 3 6
1 3 7
1 3 8
1 3 9
1 4 5
1 4 6
1 4 7
1 4 8
1 4 9
1 5 6
1 5 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9
0 1 4 5 6 7 8 9
0 2 3 4 5 6 7 8
0 2 3 4 5 6 7 9
0 2 3 4 5 6 8 9
0 2 3 4 5 7 8 9
0 2 3 4 6 7 8 9
0 2 3 5 6 7 8 9
0 2 4 5 6 7 8 9
0 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 9
1 2 3 4 5 6 8 9
1 2 3 4 5 7 8 9
1 2 3 4 6 7 8 9
1 2 3 5 6 7 8 9
1 2 4 5 6 7 8 9
1 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 9
0 1 2 3 4 5 6 8 9
0 1 2 3 4 5 7 8 9
0 1 2 3 4 6 7 8 9
0 1 2 3 5 6 7 8 9
0 1 2 4 5 6 7 8 9
0 1 3 4 5 6 7 8 9
0 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 

结果如下

1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
1
2
3
4
5
6
1
2
3
4
5
1
2
3
4
1
2
3
1
2
1
8
7
6
5
4
3
2
1
17
16
15
14
13
12
11
26
25
24
23
22
21
35
34
33
32
31
44
43
42
41
53
52
51
62
61
71
9
8
7
6
5
4
3
9
8
7
6
5
4
9
8
7
6
5
9
8
7
6
9
8
7
9
8
9
19
18
17
16
15
14
19
18
17
16
15
19
18
17
16
19
18
17
19
18
19
29
28
27
26
25
29
28
27
26
29
28
27
29
28
29
39
38
37
36
39
38
37
39
38
39
49
48
47
49
48
49
59
58
59
69
7
6
5
4
3
2
1
9
15
14
13
12
11
9
19
23
22
21
9
19
29
31
9
19
29
9
19
9
6
5
4
3
2
1
8
14
13
12
11
8
18
22
21
8
18
28
8
18
8
5
4
3
2
1
7
13
12
11
7
17
21
7
17
7
4
3
2
1
6
12
11
6
16
6
3
2
1
5
11
5
2
1
4
1
7
6
5
4
3
2
9
8
7
6
5
9
8
7
6
9
8
7
9
8
9
6
5
4
3
2
8
14
13
12
8
18
17
8
18
8
5
4
3
2
7
13
12
7
17
7
4
3
2
6
12
6
3
2
5
2
7
6
5
4
3
9
8
7
6
9
8
7
9
8
9
6
5
4
3
8
14
13
8
18
8
5
4
3
7
13
7
4
3
6
3
7
6
5
4
9
8
7
9
8
9
6
5
4
8
14
8
5
4
7
4
7
6
5
9
8
9
6
5
8
5
7
6
9
6
7
59
49
39
29
19
9
48
38
28
18
8
37
27
17
7
26
16
6
15
5
4
49
39
29
19
9
38
28
18
8
27
17
7
16
6
5
39
29
19
9
28
18
8
17
7
6
29
19
9
18
8
7
19
9
8
9
149
139
129
119
109
138
128
118
108
127
117
107
116
106
105
139
129
119
109
128
118
108
117
107
106
129
119
109
118
108
107
119
109
108
109
239
229
219
209
228
218
208
217
207
206
229
219
209
218
208
207
219
209
208
209
329
319
309
318
308
307
319
309
308
309
419
409
408
409
509
69
59
49
39
29
58
48
38
28
47
37
27
36
26
25
59
49
39
29
48
38
28
37
27
26
49
39
29
38
28
27
39
29
28
29
69
59
49
39
58
48
38
47
37
36
59
49
39
48
38
37
49
39
38
39
69
59
49
58
48
47
59
49
48
49
69
59
58
59
69
169
159
149
139
158
148
138
147
137
136
159
149
139
148
138
137
149
139
138
139
169
159
149
158
148
147
159
149
148
149
169
159
158
159
169
269
259
249
258
248
247
259
249
248
249
269
259
258
259
269
369
359
358
359
369
469
47
37
27
17
7
36
26
16
6
25
15
5
14
4
3
39
29
19
9
28
18
8
17
7
6
29
19
9
18
8
7
19
9
8
9
36
26
16
6
25
15
5
14
4
3
28
18
8
48
104
48
18
8
38
8
25
15
5
14
4
3
17
7
37
7
14
4
3
6
3
37
27
17
7
26
16
6
15
5
4
29
19
9
18
8
7
19
9
8
9
26
16
6
15
5
4
18
8
38
8
15
5
4
7
4
27
17
7
16
6
5
19
9
8
9
16
6
5
8
5
17
7
6
9
6
7
47
37
27
17
36
26
16
25
15
14
39
29
19
28
18
17
29
19
18
19
36
26
16
25
15
14
28
18
48
18
25
15
14
17
14
37
27
17
26
16
15
29
19
18
19
26
16
15
18
15
27
17
16
19
16
17
47
37
27
36
26
25
39
29
28
29
36
26
25
28
25
37
27
26
29
26
27
47
37
36
39
36
37
47
369
269
169
69
259
159
59
149
49
39
258
158
58
148
48
38
147
47
37
36
259
159
59
149
49
39
148
48
38
37
149
49
39
38
39
269
169
69
159
59
49
158
58
48
47
159
59
49
48
49
169
69
59
58
59
69
1269
1169
1069
1159
1059
1049
1158
1058
1048
1047
1159
1059
1049
1048
1049
1169
1069
1059
1058
1059
1069
2169
2069
2059
2058
2059
2069
3069
469
369
269
359
259
249
358
258
248
247
359
259
249
248
249
369
269
259
258
259
269
469
369
359
358
359
369
469
1469
1369
1359
1358
1359
1369
1469
2469
247
147
47
137
37
27
136
36
26
25
139
39
29
28
29
136
36
26
25
28
25
137
37
27
26
29
26
27
147
47
37
36
39
36
37
47
247
147
137
136
139
136
137
147
247
1469
469
369
359
358
359
369
469
10469
2469
247
时间: 2024-10-12 16:28:42

poj 2718 Smallest Difference的相关文章

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

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

原题链接: http://poj.org/problem?id=2718 题意: 给你几个数字,可以分成两个子集,然后分别按一定顺序排列组成一个数,求出这两只值差的绝对值的最小值. 分析: 反正也是刷着玩,果断先交一波全排列枚举的代码,果断TLE,然后开始想正解. 稍微想想,既然要差最小,肯定是两个数各一半.所以只要深搜出所有n/2(n为给定数字的个数)的组合,另外个n-n/2个数就有了. 但是枚举出来后的操作又想了很久,想过很多算法,都不怎么满意,最终用二分解决. 先把n/2和n-n/2全排列

POJ 2718 Smallest Difference (穷竭搜索)

http://poj.org/problem?id=2718 将一个数切一刀拆成两个数,两个数每一位数字的顺序都可改变,但是不能有前导0.求这两个数之差的最小值. 我使用了搜索并且避免了递归,自认为是比较好的算法. 一开始我想到的是枚举,并且很快给出了实现: #ifndef ONLINE_JUDGE #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <al

穷竭搜索: POJ 2718 Smallest Difference

题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一刀,比如  n1:[1 4 5],n2:[6 8 9]这样,然后 abs(n1- n2),对n1 和 n2的所有可能的排列 n1: [1 4 5][1 5 4]...这样,要算出来的最小的差,显然从中间切一刀才会出现这种解. 题解: 这里可以用来练习 STL,算法不会也没有关系,可以在这题学到 bi

POJ - 2718 Smallest Difference(全排列)

题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举n个数字的全排列. 2.当两组数字个数相同或只差1时组成的两个数字才可能出现最小差值. 3.0~cnt/2 - 1为前半组数字,cnt/2~cnt-1为后半组数字. 4.注意getchar()的位置. #pragma comment(linker, "/STACK:102400000, 102400

poj 2718 Smallest Difference(暴力枚举)(贪心待补充)

abs写成fabs导致WA了一发. 关于next_permutation()这个STL里面的函数的基本应用 http://www.cnblogs.com/luosuo10/p/5479188.html 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace s

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