home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.programming      Programming issues that transcend langua      57,431 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 56,763 of 57,431   
   Richard Heathfield to Dmitry A. Kazakov   
   Re: Another little puzzle   
   14 Dec 22 14:10:11   
   
   From: rjh@cpax.org.uk   
      
   On 14/12/2022 1:35 pm, Dmitry A. Kazakov wrote:   
   > On 2022-12-14 14:10, Richard Heathfield wrote:   
   >> On 14/12/2022 1:06 pm, Dmitry A. Kazakov wrote:   
   >>> On 2022-12-14 13:24, Stefan Ram wrote:   
   >>>>    Given n times of the 24-hour day, print their average.   
   >>>>   
   >>>>    For example, the average of "eight o'clock" and   
   >>>>    "ten o'clock" (n=2) would be "nine o'clock".   
   >>>   
   >>> You probably missed to require the interesting part: doing all   
   >>> that in the modular type (modulo 24) arithmetic:   
   >>>   
   >>>     20 + 5 = 1 (mod 24)   
   >>   
   >> ...which will give you the wrong answer. Chase that goose!   
   >   
   > Right, you must count the wrap-ups.   
      
   No, you don't. You're given n times of the 24-hour day, so all   
   values are already in the hour range [0-24). Convert to seconds   
   from midnight, add all values, divide by n to give a number t   
   guaranteed (assuming no leap seconds) to be in the range   
   [0-86400), and convert to the representation of your choice. (And   
   even if there is a leap second, it doesn't matter more than half   
   a sixpence.)   
      
   e.g.   
      
   int h, m, s;   
      
   h = t / 3600;   
   m = (t - h*3600) / 60;   
   s = (t - h*3600 - m * 60);   
      
   So why do you need mod?   
      
   --   
   Richard Heathfield   
   Email: rjh at cpax dot org dot uk   
   "Usenet is a strange place" - dmr 29 July 1999   
   Sig line 4 vacant - apply within   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca