shushus

Спринт 13. Функция vs метод класса.

Apr 20th, 2023
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. ebook.cpp
  3. -----------------------------------------------------------------------
  4. #include <iostream>
  5. #include <deque>
  6.  
  7. static const int MAXIMUM_USER_ID = 1e+5 + 1;
  8. static const size_t MAXIMUM_PAGE_NUMBER = 1000;
  9.  
  10. class StimulatePeople {
  11. public:
  12.    
  13.     StimulatePeople() : users_count_(MAXIMUM_PAGE_NUMBER, 0),
  14.                         pages_to_user_id_(MAXIMUM_USER_ID, 0) {}
  15.    
  16.     void read(int user_id, size_t pages);    
  17.     double cheer(int user_id);
  18.    
  19. private:
  20.     std::deque<int> users_count_;
  21.     std::deque<size_t> pages_to_user_id_;
  22.    
  23.     int people_ = 0;
  24. };
  25.  
  26. void StimulatePeople::read(int user_id,
  27.                            size_t pages) {
  28.  
  29.     size_t pages_before = 0;
  30.  
  31.     if (pages_to_user_id_[user_id]) {
  32.         pages_before = pages_to_user_id_[user_id];
  33.        
  34.     } else {
  35.         ++people_;
  36.     }
  37.  
  38.     pages_to_user_id_[user_id] = pages;
  39.  
  40.     for (int i = pages_before; i < pages; ++i) {
  41.         users_count_[i] += 1;
  42.        
  43.     }
  44. }
  45.  
  46. double StimulatePeople::cheer(int user_id) {
  47.  
  48.     if (!pages_to_user_id_[user_id]) {
  49.         return 0.0;
  50.  
  51.     } else if (!(people_ - 1)) {
  52.         return 1.0;
  53.  
  54.     } else {
  55.         size_t pages_to_user = pages_to_user_id_[user_id];
  56.         if (people_ == users_count_[pages_to_user - 1]) {
  57.             return 0.0;
  58.            
  59.         } else {
  60.             return (people_ - users_count_[pages_to_user - 1]) / (people_ * 1.0 - 1.0);
  61.            
  62.         }          
  63.     }
  64. }
  65.  
  66. void request_handler(std::istream& input,
  67.                      std::ostream& output,
  68.                      StimulatePeople& stimulation) {
  69.    
  70.     std::string str;
  71.     getline(input, str);
  72.    
  73.     int requests_amount = std::stoi(str);
  74.  
  75.     for (int i = 0; i < requests_amount; ++i) {
  76.        
  77.         std::string request_type;
  78.         getline(input, request_type, ' ');
  79.        
  80.         if (request_type == "READ") {
  81.            
  82.             std::string user_id;
  83.             getline(input, user_id, ' ');
  84.            
  85.             std::string pages;
  86.             getline(input, pages);
  87.            
  88.             stimulation.read(std::stoi(user_id),
  89.                              std::stoi(pages));
  90.         } else {
  91.            
  92.             std::string user_id;
  93.             getline(input, user_id);
  94.            
  95.             output << stimulation.cheer(std::stoi(user_id)) << std::endl;
  96.         }
  97.     }
  98. }
  99.  
  100. int main() {
  101.     StimulatePeople stimulation;
  102.     request_handler(std::cin,
  103.                     std::cout,
  104.                     stimulation);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment