7-10 Calculate the number of days (15 points)

7-10 Calculate the number of days (15 points)

This question requires writing a program to calculate that a certain month and day in a certain year is the day of the year.

Input format:
Input the date in one line according to the format "yyyy/mm/dd" (ie "year/month/day"). Note: The judgment condition for a leap year is that the year of the year can be divisible by 4 but not by 100, or by 400. There are 29 days in February in a leap year.

Output format: The
output date in one line is the day of the year.

Input sample 1:
2009/03/02
Output sample 1:
61
Input sample 2:
2000/03/02
Output sample 2:
62

C language:

#include <stdio.h>
//用了for循环,逻辑也是很顺畅
int main()
{
    
    
    int yyyy,mm,dd,i,day=0;
    scanf("%d/%d/%d",&yyyy,&mm,&dd);
    for(i=1;i<mm;i++)
    {
    
    
        if(i<=7)
        {
    
    
            if(i%2==1)day+=31;
			else if(i==2)
            {
    
    
				if(yyyy%4==0 && yyyy%100!=0 || yyyy%400==0)day+=29;
				else day+=28;	
			}
            else if(i%2==0 && i>2)day+=30;
        }
        else if(i>7)
        {
    
    
            if(i%2==1 && i>2)day+=31;
            else if(i%2==0 && i!=2)day+=30;
        }
    }
    printf("%d",day+dd);
}

Java:

//待更新

The code can be optimized, there are many ways to solve the problem, welcome to leave a message to discuss~

Guess you like

Origin blog.csdn.net/xiahuayong/article/details/109037520