fork(3) download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. void sorting(int *arr, int i)
  7. {
  8. while(arr[i] < arr[i - 1] && i > 0)
  9. {
  10. swap(arr[i], arr[i - 1]);
  11. i--;
  12. }
  13. }
  14.  
  15. bool repeats(int number, int *arr, int i)
  16. {
  17. for(int j = 0; j <= i; j++)
  18. if(number == arr[j])
  19. return 1;
  20.  
  21. return 0;
  22. }
  23.  
  24. int main()
  25. {
  26. int n;
  27. while(cin >> n)
  28. {
  29. cin.clear();
  30. cin.ignore();
  31.  
  32. int i = 0, temp, number[n] = {};
  33. string line, num;
  34.  
  35. getline(cin, line);
  36. while(line != "")
  37. {
  38. if(line.find(' ') != string::npos)
  39. {
  40. num = line.substr(0, line.find(' '));
  41. temp = stoi(num);
  42. line.erase(0, line.find(' ') + 1);
  43. }
  44. else
  45. {
  46. num = line.substr(0);
  47. temp = stoi(num);
  48. line.erase(0);
  49. }
  50.  
  51. if(i == 0 || !repeats(temp, number, i - 1))
  52. {
  53. if(i < n)
  54. {
  55. number[i] = temp;
  56. sorting(number, i);
  57. i++;
  58. }
  59. else if(temp > number[0])
  60. {
  61. for(int j = 1; j < n; j++)
  62. number[j - 1] = number[j];
  63.  
  64. number[n - 1] = temp;
  65. sorting(number, n - 1);
  66. }
  67. }
  68. }
  69.  
  70. if(i < n)
  71. cout << "-\n";
  72. else
  73. cout << number[0] << "\n";
  74. }
  75.  
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0s 5656KB
stdin
3 10 20 30
4 10 20 30 30
2 1 2 6 8 9
1 16 16 18
stdout
10
-
8
18