2019秋

mac2025-05-16  14

问题描述: 设计函数int getDays(int year,int month),根据给定的年year和月份month,计算该月的天数并返回。要注意闰年的判断

输入与输出要求: 输入两个整数y,m,即年份与月份。输出该年内该月的天数,占一行。

程序运行效果: Sample 1: 1995 7↙ There are 31 days in month 7 year 1995. Sample 2: 2000 2↙ There are 29 days in month 2 year 2000.

#include<stdio.h> int main() { int y,m,d; int t; scanf("%d %d",&y,&m); if(y%400==0||(y%100!=0&&y%4==0)){ //注意闰年的判断条件 t=1; }else t=0; if(t==1&&m==2) d=29; else if(t==0&&m==2) d=28; switch(m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:d=31; break; case 4: case 6: case 9: case 11:d=30; break; default:break; } printf("There are %d days in month %d year %d.\n",d,m,y); return 0; }

Attachment: 关于计算日期的另一道题目(点击查看)

最新回复(0)