current position:Home>Give a string representing the date and ask what day of the year it is
Give a string representing the date and ask what day of the year it is
2022-02-02 11:25:20 【CSDN Q & A】
Enter one string at a time , The format of the string is YEAR/MM/DD, namely YEAR year MM month DD Japan . Output is required. This is the day of the year .
Want even multiple sets of data . I don't know what to do with the end of input
Refer to the answer 1:
#include <stdio.h>int monthday[] = { 0, // (1) 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int sumday[13];char str[100];int y, m, d;int main() { int i; while (scanf("%s", str) != EOF) { sscanf(str, "%4d/%2d/%2d", &y, &m, &d); if (y % 4 == 0 && y % 100 || y % 400 == 0) { monthday[2] = 29; // (2) } else { monthday[2] = 28; } sumday[0] = 0; for (i = 1; i <= 12; ++i) { // (3) sumday[i] = sumday[i - 1] + monthday[i]; } int ans = sumday[m - 1] + d; // (4) printf("%d\n", ans); } return 0;}
- $(1)$ Array
monthday[i]
It is stored in normal years , The first $i$ Total number of days in a month ; - $(2)$ If it is a leap year according to the conditions ,2 The total number of days in a month becomes 29;
- $(3)$ Statistics $1$ Month to $i$ The total number of days of the month is stored in
sumday[i]
in ; - $(4)$ Based on the previous month plus the current number of days , Calculate total days ;
Refer to the answer 2:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021125169278.html