一维数组
#include <stdio.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 month of days is %d\n",days[month-1]);
return 0;
}
二维数组
#include <stdio.h>
#define months 12
int main()
{
int days[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",days[1][month-1]);
else//非闰年
printf("the number of days is %d\n",days[0][month-1]);
return 0;
}