Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. // you can use includes, for example:
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5.  
  6.  
  7. // 1. iterate on range A to B
  8. // 2. convert int to string
  9. // 3. count occurences of lovely numbers
  10. // 4. return number
  11.  
  12.  
  13. int solution(int A, int B)
  14. {
  15.     int cnt;
  16.     std::string num_string;
  17.     int lovely_cnt = 0;
  18.     bool is_lovely;
  19.    
  20.     // iterates on range [A, B]
  21.     for(int i = A ; i <= B ; i++)
  22.     {
  23.         bool is_lovely = true;
  24.         cnt = 0;
  25.         num_string = std::to_string(i);
  26.        
  27.         // must be lovely
  28.         if(num_string.length() < 3)          
  29.         {
  30.             lovely_cnt++;
  31.             continue;
  32.         }
  33.        
  34.         // checking number's loveliness
  35.         for(int j = 0 ; j < num_string.length() ; j++)
  36.         {
  37.             cnt = std::count(num_string.begin(), num_string.end(), num_string[j]);
  38.             if(cnt >= 3)
  39.             {
  40.                 is_lovely = false;
  41.                 break;
  42.             }
  43.         }
  44.        
  45.         if(is_lovely)
  46.         {
  47.             lovely_cnt++;
  48.         }
  49.     }
  50.  
  51.     return lovely_cnt;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement