ABS written fabs led WA to a pitch.
About Next_permutation () the basic application of the function in 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 namespacestd;7 intt,n,a,b;8 inta[ -];9 Chars[ -];Ten Const intinf=0x3f3f3f; One voidinput () A { -n=0; - gets (s); the intlen=strlen (s); - - for(intI=0; i<len;i++) - { + if('0'<= S[i] && s[i] <='9') - { +a[n++] = s[i]-'0'; A } at } - - } - voidSolve () - { - if(n==2) in { -printf"%d\n", ABS (a[0]-a[1])); to return ; + } - the intMid= (n+1)/2; * intans=inf; $ DoPanax Notoginseng { -a=b=0; the if(a[0]!=0&& a[mid]!=0) + { A for(intI=0; i<mid;i++) the { +a=a*Ten+A[i]; - } $ for(inti=mid;i<n;i++) $ { -b=b*Ten+A[i]; - } the intRes=max (A-B,B-A);//>=0 -ans=min (ans,res);Wuyi } the -} while(Next_permutation (a,a+n)); Wu - Aboutprintf"%d\n", ans); $ } - intMain () - { -scanf"%d",&T); A GetChar (); + while(t--) the { - input (); $ solve (); the } the return 0; the}
POJ 2718 Smallest difference (brute force enumeration) (greedy to add)