1.不考虑闰年
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
int main()
{
int days[MONTHS]={31,28,31,30,31,30,31,31,30,31,30,31};
int month;
do{
printf("Input a month:");
scanf("%d",&month);
}while(month<1 || month>12 );
printf("The number of days is %d\n",days[month-1]);
return 0;
}
2、考虑闰年
2.1、
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
int main()
{
int a[2][MONTHS]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
int year,month;
do{
printf("Input year,month:");
scanf("%d,%d",&year,&month);
}while(month < 1||month >12);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("The number of days is %d\n",a[1][month-1]);
else
printf("The number of days is %d\n",a[0][month-1]);
return 0;
}
2.2
简单优化版(用12表示)
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
int main()
{
int a[2][MONTHS+1]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int year,month;
do{
printf("Input year,month:");
scanf("%d,%d",&year,&month);
}while(month < 1||month >12);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("The number of days is %d\n",a[1][month]);
else
printf("The number of days is %d\n",a[0][month]);
return 0;
}