1154.cpp 717 B

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: zhuyijun
  5. * @Date: 2021-12-21 10:29:00
  6. * @LastEditTime: 2021-12-21 10:55:22
  7. */
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. using namespace std;
  13. int dayOfYear(string date) {
  14. if (date.size() != 10) {
  15. return 0;
  16. }
  17. string year = date.substr(0, 4);
  18. string month = date.substr(5, 2);
  19. string day = date.substr(8, 2);
  20. int y = stoi(year);
  21. int m = stoi(month);
  22. int d = stoi(day);
  23. int a[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  24. if (y != 1990 && (y % 4 == 0)) a[2] += 1;
  25. int ret = 0;
  26. for (int i = 1; i < m; i++) {
  27. ret += a[i];
  28. }
  29. return d + ret;
  30. }
  31. int main() { cout << dayOfYear("2020-12-21"); }