Advertisement
Spocoman

1. MinBy

Jan 29th, 2024 (edited)
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void minLexicographical(vector<string>& v) {
  9.     string result = v[0];
  10.     for (auto& el : v) {
  11.         if (result > el) {
  12.             result = el;
  13.         }
  14.     }
  15.     cout << result << endl;
  16. }
  17.  
  18. void minLength(vector<string>& v) {
  19.     string result = v[0];
  20.     for (auto& el : v) {
  21.         if (result.length() > el.length()) {
  22.             result = el;
  23.         }
  24.     }
  25.     cout << result << endl;
  26. }
  27.  
  28. void maxLength(vector<string>& v) {
  29.     string result = v[0];
  30.     for (auto& el : v) {
  31.         if (result.length() < el.length()) {
  32.             result = el;
  33.         }
  34.     }
  35.     cout << result << endl;
  36. }
  37.  
  38. int main()
  39. {
  40.     string line, word;
  41.     getline(cin, line);
  42.  
  43.     istringstream ss(line);
  44.  
  45.     vector<string> words;
  46.  
  47.     while (ss >> word) {
  48.         words.push_back(word);
  49.     }
  50.  
  51.     int n;
  52.     cin >> n;
  53.  
  54.     n == 1 ? minLexicographical(words) : n == 2 ? minLength(words) : maxLength(words);
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement