Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3973 | Accepted: 1110 |
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.
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
题意:
给出 T 个 case,后给出 2 到 10 个数,数字由递增顺序给出。将这些数凑成两个数,求绝对值最小值。
思路:
暴力枚举。运用 next_permutation 不断生成序列即可。给出的数没有固定给出多少个,所以用字符串保存再转成数字。注意不能有前导 0 的生成数。
AC:
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib> using namespace std; int num[15], ans; char str[30]; int main() { int t; scanf("%d", &t); getchar(); while (t--) { ans = 0; gets(str); for (int i = 0; i < strlen(str); ++i) { if (str[i] >= '0' && str[i] <= '9') num[ans++] = str[i] - '0'; } sort(num, num + ans); int Min = 999999999; do { if ((!num[0] || !num[ans / 2]) && ans > 2) continue; int a = 0, b = 0; for (int i = 0; i < ans; ++i) { if (i < ans / 2) a = a * 10 + num[i]; else b = b * 10 + num[i]; } Min = min(Min, abs(a - b) ); } while(next_permutation(num, num + ans)); printf("%d\n", Min); } return 0; }
相关推荐
标题 "the kth smallest elem" 暗示我们要讨论的是如何在数组或集合中找到第k小的元素,这是一个常见的算法问题,在计算机科学和数据结构领域具有重要意义。描述中提到的 "Clion" 是一个流行的C/C++集成开发环境,...
在`Smallest-Difference-In-Unsorted-Arrays-master`这个项目中,很可能包含了这个问题的具体实现代码,你可以查看源文件以了解更多细节。这将帮助你更好地理解和学习如何在实际编程环境中应用这些概念。
"smallest-width"这个概念正是Android系统提供的一种解决方案,用于帮助开发者创建能够适应不同屏幕尺寸的应用。本文将深入探讨"smallest-width"的概念,以及如何利用它来实现Android自定义`dimens.xml`文件,以适应...
373._Find_K_Pairs_with_Smallest_Sums查找和最小的k对数字【LeetCode单题讲解系列】
java java_leetcode题解之Kth Smallest Number in Multiplication Table.java
java java_leetcode题解之K-th Smallest Prime Fraction.java
java java_leetcode题解之Kth Smallest Element in a BST.java
java java_leetcode题解之Find K Pairs with Smallest Sums.java
java java_leetcode题解之Kth Smallest Element in a Sorted Matrix.java
java java_leetcode题解之Find K-th Smallest Pair Distance.java
python python_leetcode题解之2413_Smallest_Even_Multiple.py
c++ C++_leetcode题解之744. Find Smallest Letter Greater Than Target.cpp
"smallest3hq"可能指的是C8051F单片机的最小系统设计,该设计通常包括电源、晶振、复位电路以及必要的I/O接口。在C语言编程中,我们需要初始化这些硬件资源,如设置时钟频率、配置I/O口等。 五、实践与研究 ...
代码:// find K-th Smallest Pair Distancepublic int smallestDistancePair(int[] nums
将您的过程解决方案编码到lib/smallest_multiple.rb文件中。 将您的面向对象的解决方案编码到lib/oo_smallest_multiple.rb文件中。 运行learn直到所有RSpec测试通过。 来源 -- 在Learn.co上查看并开始免费学习编码。
AT_code_festival_2017_qualb_f Largest Smallest Cyclic Shift 的 AC 代码
c++ C++_leetcode题解之668_Kth_Smallest_Number_in_Multiplication_Table
c c语言_leetcode题解之0230_kth_smallest_element_in_a_bst
在这个名为"the-smallest-tree.rar_The Tree"的压缩包中,包含了关于最小生成树算法的源代码,采用的是Visual C++ 6.0编程环境。 在图论中,一个无向图的最小生成树是指包含图中所有顶点的一个子图,且该子图是树形...