fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. int N;
  10. cin >> N;
  11.  
  12. vector<int> digits(N);
  13. for (int i = 0; i < N; i++) {
  14. cin >> digits[i];
  15. }
  16.  
  17. //看定義學會的函數sort(start, end, 比較元素(不放就預為小到大))
  18. sort(digits.begin(), digits.end(), greater<int>());
  19. string maxNumStr = "";
  20. for (int digit : digits) {
  21. maxNumStr += to_string(digit);
  22. }
  23. long long maxNum = stoll(maxNumStr);
  24.  
  25. // 先小到大排
  26. sort(digits.begin(), digits.end());
  27. string minNumStr = "";
  28. //check the top ?= 0 than change the number
  29. if(digits[0] == 0){
  30. int zero_change =1;
  31. int t = 1;
  32. while(digits[zero_change] == 0){
  33. zero_change++;
  34. }
  35. t = digits[zero_change];
  36. digits[zero_change] = 0;
  37. digits[0] = t;
  38. }
  39.  
  40. for (int digit : digits) {
  41. minNumStr += to_string(digit);
  42. }
  43. long long minNum = stoll(minNumStr);
  44.  
  45. cout << maxNum + minNum << endl;
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5304KB
stdin
10
0 1 0 0 9 0 0 0 0
stdout
10100000009