We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Warmup
  4. Time Conversion
  5. Discussions

Time Conversion

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • agee1 4 years ago+ 3 comments

    Same number of lines, less variables, no string comparisons. One atoi though.

    int main() {

    char time[11] = {0};
    scanf("%s", time);
    int n_hour = atoi(time);
    char plus12 = n_hour >= 12;
    n_hour += time[8] == 'P' ? (plus12 ? 0 : 12) : (plus12 ? -12 : 0);
    printf("%02d:%.*s:%.*s", n_hour, 2, time + 3, 2, time + 6);
    return 0;
    

    }

    -7|
    ParentPermalink
    • saurav8 4 years ago+ 1 comment

      There are ways to make code even shorter but this is far less readable than the above one, without any reduction in complexity since constants dont count. This is really short problem but 'readability' is really important factor in designing large softwares and shouldnt be ignored like this.

      21|
      ParentPermalink
      • agee1 4 years ago+ 0 comments

        Can't agree more about readability in real projects. I just wanted to add more challenge for myself for this particular problem.

        6|
        ParentPermalink
    • atulkukreja 4 years ago+ 0 comments

      unable to get your code...please if you could explain a bit how it is working.

      0|
      ParentPermalink
    • neal0892 4 years ago+ 0 comments

      CAN YOU PLEASE EXPLAIN THE BELOW CODE: char plus12 = n_hour >= 12; n_hour += time[8] == 'P' ? (plus12 ? 0 : 12) : (plus12 ? -12 : 0); printf("%02d:%.*s:%.*s", n_hour, 2, time + 3, 2, time + 6);

      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature