Hello ! There is such a task, it passes 50%. Help please find my mistake, nor how can I find! Here is the task:
Integer A contains no more than 1000 decimal digits. Denote by B and C respectively the maximum and minimum of the numbers composed of the digits A. The numbers B and C should have the same sign as A. Find the difference between B and C.
Input data
The only line contains the decimal notation of A.
Output
In one line, the desired difference.
Sample input
418 Sample output
693 Sample input
-1032 Sample output
2187 Sample input
8814 Sample output
7353 My code is:
#include <bits/stdc++.h> #define MAXN 200010 using namespace std; long long sto_int(string s) { stringstream ss(s); long long n; ss >> n; return n; } bool sorthus(char i, char j) { return (i > j); } int main() { string cis, cis2, cis3; char temp; cin >> cis; cis3 = cis; sort(cis3.begin(), cis3.end()); long long cnt = 0; for (long long i = 0; i < cis3.size(); i++) { if (cis3[i] == '0')cnt++; } if (cis3[0] == '-') { if (cis3[1] == '0') { swap(cis3[1], cis3[1 + cnt]); } } else { if (cis3[0] == '0') { swap(cis3[0], cis3[0 + cnt]); } } cis2 = cis3; if (cis[0] == '-') { sort(cis2.begin() + 1, cis2.end(), sorthus); } else { sort(cis2.begin(), cis2.end(), sorthus); } long long cis22 = sto_int(cis2); long long cis33 = sto_int(cis3); if (cis22 < 0)cout << cis33 - cis22 << endl; else cout << cis22 - cis33 << endl; cout << cis33 << " " << cis22 << endl; cout << cis3 << endl; return 0; }
long longtype is enough for a 1000-digit number? - Harry