函数法求天数:本题要求实现一个函数sumday,可计算输入的日期是这年中的第几天。函数接口定义:int sumday(int year,int month,int day);

#include

int sumday(int year,int month,int day);

int main()
{
    int year,month,day,days;
    scanf("%d/%d/%d",&year,&month,&day);
    days=sumday(year,month,day);
    printf("%d",days);
    return 0;
}


/* 请在这里填写答案 */
int sumday(int year,int month,int day){
    int sum=0,flag=0;
     switch(month)
    {
        case 1: sum = 0; break;
        case 2: sum = 31; break;
        case 3: sum = 59; break;
        case 4: sum =90; break;
        case 5: sum = 120; break;
        case 6: sum = 151; break;
        case 7: sum = 181; break;
        case 8: sum = 212; break;
        case 9: sum = 243; break;
        case 10: sum = 273; break;
        case 11: sum = 304; break;
        default: sum = 334; break;
    }
    sum += day;
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        flag = 1;
    if(flag == 1 && month > 2)
        sum++;
return sum;
}

你可能感兴趣的:(算法,c++,数据结构)