Description
The number A is connected by n numerical digit a1a2a3a4……an.
You can separate it into two parts as a1a2a3..ak and ak+1ak+2…an and subtract the smaller one from the bigger one to get the number B.
Please find the minimum result of number B.
Input
First line of each case contains a number A.( 10 <= A <= 10^20 )
Output
Output the answer in a single line for each case.
Sample Input
457
75462
734794666
Sample Output
38
387
68813
先开始想着分奇偶从中间截取两段,这样相减一定最小,但是之后举出了反例1001,于是还是每个位置都截一下做比较了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
long long n;
while(cin >> n)
{
long long a,b,c = 1e18;
long long temp = 10,i;
for(i = 0; temp < n ; i++) //从第i个数开始截
{
a = n % temp; //a为截取的前一部分
b = n / temp; //b为截取的后一部分
c = min(c,abs(a - b));
temp = temp * 10;
}
cout << c << endl;
}
return 0;
}