当前位置:网站首页>1154. day of the year

1154. day of the year

2022-06-13 06:40:00 Mr Gao

1154. The day of the year

Give you a string date , Press YYYY-MM-DD Format represents a The current chronology date . Returns the day of the current year .

Example 1:

Input :date = “2019-01-09”
Output :9
explain : The given date is 2019 The ninth day of the year .

Example 2:

Input :date = “2019-02-10”
Output :41
There is no difficulty in this problem , Just do it as usual , Pay attention to the judgment conditions for leap years . The solution code is as follows :

int dayOfYear(char * date){
    
    int year=0;
    int month=0;
    int day=0;
    int target_day=0;
    int i;
    int num=0;
    int months[12]={
    31,28,31,30,31,30,31,31,30,31,30,31};
    i=0;
   while(date[i]!='\0'){
    
       if(date[i]=='-'){
    
           num++;
           i++;
           continue;
       }
       else{
    
           if(num==0){
    
               year=year*10+date[i]-'0';

           }
           if(num==1){
    
               month=month*10+date[i]-'0';
           }
           if(num==2){
    
               day=day*10+date[i]-'0';
           }
       }
       i++;
       

   }
   if((year%4==0&&year%100!=0)||year%400==0){
    
       months[1]=29;
   }
   for(i=0;i<month-1;i++){
    
       target_day=target_day+months[i];
   }
   target_day=target_day+day;
   return target_day;


}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130620210567.html