C语言实验——某年某月的天数

本文介绍了一个简单的C语言程序,用于判断指定年份中某个月份的具体天数,并特别注意处理闰年的特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

输入年和月,判断该月有几天?

Input

输入年和月,格式为年\月。

Output

输出该月的天数。

Example Input

2009\1

Example Output

31

Hint

注意判断闰年啊

Author

C语言实验——某年某月的天数(http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/1160.html)

题目代码:

#include <stdio.h>
#define MONTHS 12
int main()
{
    int days[2][MONTHS] = { {31,29,31,30,31,30,31,31,30,31,30,31},
                            { 31,28,31,30,31,30,31,31,30,31,30,31 } };
    int year, month;
    do {
        scanf_s("%d\\%d", &year, &month);
    } while (month < 1 || month>12);/*处理不合法数据的输入*/
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)/*闰年*/
    {
        printf("%d", days[0][month - 1]);
    }
    else/*非闰年*/
    {
        printf("%d", days[1][month - 1]);
    }
    return 0;
}

2018/6/1 这道题注意反义字符。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int y,m;
    int month[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    scanf("%d\\%d",&y,&m);
    if(m!=2)
       printf("%d\n",month[m-1]);
    else if(y%4==0&&y%100!=0||y%400==0){
        printf("29\n");
    }else{
        printf("28\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值