Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4863 | Accepted: 1336 |
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
给定一个N个数字(N不小于2且不大于10),这些数字取值范围为0到9且互不相同,且升序排列,将这些数字组成两组数,使得这两组数的绝对差最小。注:这两组数字均不能以0开头。
#include<iostream> using namespace std; int main() { int t; int a[20]; cin>>t; while(t--) { int n=0; int k=6; //while(cin>>a[n]) while(k--) { cin>>a[n]; n++; } int f_a[10],f_b[10];//f_a为两个数中较大的数 int aa,bb; bool flag[20]; memset(flag,sizeof(bool),true); cout<<"n"<<n<<endl; if(!n%2) { int min=100; int s=0; if(a[0]==0) s=1; for(int i=s;i<n;i++)//找出差值最小的两个数字,较小的那个作为a的首字母,较大的那个作为b的首字母,分别存储在f_a[0]和f_b[0]中。先判断最小的数字是否为0,如果为0,则从第二个数字开始。 { for(int j=i+1;j<n;j++) { if((a[j]-a[i])<min) { min=abs(a[i]-a[j]); aa=i; bb=j; cout<<"here"<<endl; } } } cout<<"aa"<<aa<<"bb"<<bb<<endl; flag[aa]=false; flag[bb]=false; int m=1; for(int i=0;i<n&&flag[i]&&m!=(n/2);i++)//把剩余的前(n-2)/2小的数字从小到大依次赋给f_a。 { f_a[m++]=a[i]; } int h=n/2-1; for(int i=0;i<n&&flag[i]&&h!=-1;i++)//把剩余的数字从大到小依次赋给f_b。 { f_b[h--]=a[i]; } for(int i=0;i<n/2;i++) cout<<f_a[i]; cout<<endl; for(int i=0;i<n/2;i++) cout<<f_b[i]; cout<<endl; } else { if(a[0]==0) { f_a[1]=a[0]; f_a[0]=a[1]; int k=2; for(int s=2;s<=n/2;s++) { f_a[s++]=a[k++]; } int t=n-1; for(int s=0;s<n/2;s++) { f_b[s++]=a[t--]; } } else { int k=0; for(int s=0;s<=n/2;s++) { f_a[s++]=a[k++]; } int t=n-1; for(int s=0;s<n/2;s++) { f_b[s++]=a[t--]; } } for(int i=0;i<=n/2;i++) cout<<f_a[i]; cout<<endl; for(int i=0;i<n/2;i++) cout<<f_b[i]; cout<<endl; } } return 0; }
参考过网上的博客。主要思想就是如果给定的数字总数为偶数,则首先选出相差最小的两个数字(不包括0),将这两个数字分别作为两组数的最高位元素,对于以这两个数字中以较大的那个为最高位的数,将剩余数字中的前N/2-1个数依次作为该数的第二高位到最低位元素,并将剩下的数字从大到小依次作为另一个数的第二高位到最低位元素。如果给定的数字总数为奇数,先判断最小元素是否为0,如果为0,则将第二个数字作为较大那个数的最高位,0作为该数的第二高位,从第三个数字开始的N/2-1个数字依次作为该数的第三高位至最低位,剩下的数字依次作为另一个数的最低位到最高位;如果最小元素不为0,则将前N/2+1个数字依次作为该数的最高位至最低位,剩下的数字依次作为另一个数的最低位到最高位。
另外,该代码还有问题,明天记得完善。