2201 - 计算天数(case语句)

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

输入年、月,输出该月的天数。

题目输入

输入年、月

题目输出

输入天数

输入/输出样例

输入格式

1298 2

输出格式

28

C语言解答

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int y,m,a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    scanf("%d%d",&y,&m);
    if(m!=2)
        printf("%d",a[m]);
    if(m==2)
        if(y%4==0 || y%400==0)
            printf("29");
        else
            printf("28");
	return 0;
}

C++解答

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int year,month;
    cin>>year>>month;
    switch (month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            cout<<31<<endl;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            cout<<30<<endl;
            break;
        default:
            //2月
            if( (year%100 != 0  && year%4 == 0) || (year%400 == 0) )
            {
                //如果是闰年,2月29天
                cout<<29<<endl;
            }
            else
            {
                //如果是平年,2月28天
                cout<<28<<endl;
            }
            break;
    }
    return 0;
}