大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.1 KiB

  1. /*
  2. A的寝室里放着一个时钟hh:mm:ss
  3. A得到了一系列被记录的时间
  4. A想让你算算: 24 1 60 1 60
  5. n(2 n < 10e5)n个字符串hh:mm:ss(0 hh< 24, 0 mm< 60, 0 ss < 60).
  6. :n -1i个数表示第i到i十1时间点秒针转过的圈数 10e-5
  7. */
  8. #include <iostream>
  9. #include <vector>
  10. #include <string>
  11. #include <cmath>
  12. #include <iomanip>
  13. using namespace std;
  14. // Helper function to convert time in "hh:mm:ss" format to total seconds from midnight
  15. int timeToSeconds(const string& time) {
  16. int hh = stoi(time.substr(0, 2));
  17. int mm = stoi(time.substr(3, 2));
  18. int ss = stoi(time.substr(6, 2));
  19. return hh * 3600 + mm * 60 + ss;
  20. }
  21. int main() {
  22. int n;
  23. cin >> n;
  24. vector<string> times(n);
  25. // Input all time points
  26. for (int i = 0; i < n; ++i) {
  27. cin >> times[i];
  28. }
  29. // Calculate and print the number of full rotations of the second hand between consecutive time points
  30. for (int i = 1; i < n; ++i) {
  31. int t1 = timeToSeconds(times[i - 1]);
  32. int t2 = timeToSeconds(times[i]);
  33. // If t2 is smaller, it means the time passed to the next day
  34. if (t2 < t1) {
  35. t2 += 24 * 3600; // add 24 hours in seconds
  36. }
  37. int secondsDiff = t2 - t1;
  38. double rotations = secondsDiff / 60.0; // calculate the full rotations
  39. printf("%.*f", 10, rotations);
  40. // cout << setprecision(10) << rotations << endl; // print the result
  41. }
  42. return 0;
  43. }