首页 > 试题广场 >

获得月份天数

[编程题]获得月份天数
  • 热度指数:76630 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
KiKi想获得某年某月有多少天,请帮他编程实现。输入年份和月份,计算这一年这个月有多少天。

输入描述:
多组输入,一行有两个整数,分别表示年份和月份,用空格分隔。


输出描述:
针对每组输入,输出为一行,一个整数,表示这一年这个月有多少天。
示例1

输入

2008 2

输出

29
#include <stdio.h>

int main()
{
    int a, b = 0;
    while (scanf("%d %d", &a, &b) != EOF)
    {
        if (a % 400 == 0 || a % 4 == 0 && a % 100 != 0)  //  判断是否是闰年
        {
            switch (b)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    printf("31\n");
                    break;
                case 2:
                    printf("29\n");  // 闰年 2 月
                break;
                case 4:
                case 6:
                case 9:
                case 11:
                    printf("30\n");
                    break;
            }
        }
        else
        {              
            switch (b)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    printf("31\n");
                    break;
                case 2:
                    printf("28\n");  // 平年 2 月
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    printf("30\n");
                    break;
            }
        }
    }

    return 0;
}
发表于 2025-06-10 20:59:05 回复(0)
int main()
{
    int month_day[13] = {0, 31,28,31,30,31,30,31,31,30,31,30,31 };
    int* p = month_day;
    int Year, Month;
    while (scanf("%d %d", &Year, &Month)!=EOF)
    {
        if (((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0) && Month == 2)
            printf("%d\n", *(p + Month) + 1);
        else
            printf("%d\n", *(p + Month));
    }
    return 0;
}
发表于 2025-04-30 21:35:31 回复(0)
#include <stdio.h>
int is_leap_year(int y)
{
	return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}
int main()
{
	int y = 0;
	int m = 0;
	int d = 0;
	int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	while (scanf("%d%d", &y, &m) == 2)
	{
		d = days[m];
		if ((is_leap_year(y) == 1) && (m == 2))
		{
			d++;
		}
			printf("%d\n", d);
	}
	return 0;
}

发表于 2025-01-10 23:26:53 回复(0)
#include <stdio.h>
int main()
{
    int year,month;
    while(scanf("%d %d",&year,&month)!=EOF){
    int a[2][13]={
        {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}
    };
    if(year%4==0&&year%100!=0||year%400==0){
        printf("%d\n",a[1][month]);
    }else{
        printf("%d\n",a[0][month]);
    }
    }
    return 0;
}
发表于 2024-12-13 18:20:18 回复(0)
写的有点复杂
#include <stdio.h>

int main() {
    int a, b,rn;
   while (scanf("%d %d",&a,&b)!=EOF) {
        if (a%4==0) {
        if (a%100) {
            if (a%400) {
                rn=1;
            }
            else {
            rn=0;
            }
        }
        rn=1;
    }
    else {
    rn=0;
    }
    if (rn==1) {
        if (b==2) {
            printf("29\n");
        }
        if (b==1||b==3||b==5||b==7||b==8||b==10||b==12) {
            printf("31\n");
        }
        if (b==4||b==6||b==9||b==11) {
            printf("30\n");
        }
    }
    else {
    if (b==2) {
            printf("28\n");
        }
        if (b==1||b==3||b==5||b==7||b==8||b==10||b==12) {
            printf("31\n");
        }
        if (b==4||b==6||b==9||b==11) {
            printf("30\n");
        }
    }
   }
   
    return 0;
}
发表于 2024-09-09 12:00:01 回复(0)
#include<stdio.h>

int main()
{
    int y,m;
    while(scanf("%d %d",&y,&m)!=EOF){
        if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) printf("31\n");
        else if(m==4||m==6||m==9||m==11) printf("30\n");
        else{
            if(y%400==0||(y%4==0&&y%100!=0)) printf("29\n");//闰年
            else printf("28\n");//平年
        }
    }
    return 0;
}
发表于 2024-09-07 11:10:35 回复(0)
#include <stdio.h>
int main() {
    int year = 0;
    scanf("%d", &year);
    int month = 0;
    do {
        scanf("%d", &month);
        switch (month) {
            case 1 :
                printf("31\n");
                break;
            case 2 : {
                    if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0) {
                        printf("29\n");
                        break;
                    } else {
                        printf("28\n");
                        break;
                    }
                }
            case 3 :
                printf("31\n");
                break;
            case 4 :
                printf("30\n");
                break;
            case 5 :
                printf("31\n");
                break;
            case 6 :
                printf("30\n");
                break;
            case 7 :
                printf("31\n");
                break;
            case 8 :
                printf("31\n");
                break;
            case 9 :
                printf("30\n");
                break;
            case 10 :
                printf("31\n");
                break;
            case 11 :
                printf("30\n");
                break;
            case 12 :
                printf("31\n");
                break;
            default : {
                    printf("输入错误月份,请重新输入:");
                    break;
                }
        }
    } while ((scanf("%d", &year)) != EOF);
    return 0;
}

发表于 2024-08-03 11:50:47 回复(0)
#include<stdio.h>
int main()
{
int year=0,month=0;
 while((scanf("%d %d",&year,&month))!=EOF)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: printf("31\n");break;
case 4:
case 6:
case 9:
case 11:printf("30\n");break;

default: {
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
    printf("29\n");
    else
     printf("28\n");
         }
}
}
    return 0;
}
发表于 2024-08-03 09:52:29 回复(0)
#include <stdio.h>

int isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int getDaysInMonth(int year, int month) {
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && isLeapYear(year)) {
        return 29;
    }
    return daysInMonth[month - 1];
}

int main() {
    int year, month;
    while (scanf("%d %d", &year, &month) != EOF) {
        int days = getDaysInMonth(year, month);
        printf("%d\n", days);
    }
    return 0;
}
发表于 2024-06-30 20:50:56 回复(0)
#include <stdio.h>
//C语言中处理离散型数据 switch是不二之选

int main()
{
    int year, month;
    while (scanf("%d %d", &year, &month) != EOF)
    {
        if (year % 4 == 0 || year % 400 == 0)
        {
            switch (month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                printf("31\n");
                break;
            case 2:
                printf("29\n");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                printf("30\n");
                break;
            default:
                printf("输入错误");
                break;

            }
        }
        else
        {
            switch (month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                printf("31\n");
                break;
            case 2:
                printf("28\n");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                printf("30\n");
                break;
            default:
                printf("输入错误");
                break;
            }
        }
    }
    return 0;
}
发表于 2024-06-04 10:33:10 回复(0)
#include <stdio.h>

int main() {
    int a, b;
    while (scanf("%d %d", &a, &b) != EOF)
    {
   

        if(a%400==0 || ( a%4==0 && a%100 != 0))
        {
            if (b==2)
            printf("29\n");
        }
        else if (b==2)
        {
            printf("28\n");
        }
        switch(b)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                printf("31\n");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                printf("30\n");
                break;
        }

    }
       
   
    return 0;
}
发表于 2024-04-30 09:44:02 回复(0)
#include <stdio.h>

int main() 
{
    int y,m;
    while(scanf("%4d %d",&y,&m) !=EOF)
    //if either happens before any data could be successfully read, EOF is returned.
    {
        switch(m)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:    //31天的月份
            printf("31\n");
            break;
        case 4:
        case 6:
        case 9:
        case 11:    //30天的月份
            printf("30\n");
            break;
        case 2:
            if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))//判断闰年
                printf("29\n");   //闰年2月为28天
            else
                printf("28\n");   //平年为29天
        }
    }
    return 0;
}

编辑于 2024-04-19 10:54:49 回复(0)
月份只有闰年的2月会有变化,所以只需要判断2点:
  1. 是否为2月
  2. 是否为闰年
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main()
{
    int day[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int y = 0;
    int d = 0;
    int input = 0;

    while (scanf("%d%d", &y, &d) != EOF)
    {
        //是2月,进入闰年判断
        if (2 == d)
        {
            //是闰年
            if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
            {
                input = day[d] + 1;
            }   
            //不是闰年
            else
            {
                input = day[d];
            }
        }
        //不是2月
        else
        {
            input = day[d];
        }
        //输出结果
        printf("%d\n", input);
    }

    return 0;
}

发表于 2024-03-19 10:14:44 回复(0)
#include <stdio.h>

int main()
{
    int arr1[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int arr2[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int year = 0;
    int month = 0;
    while (scanf("%d %d", &year, &month) != EOF)
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            printf("%d\n", arr2[month - 1]);
        }
        else
        {
            printf("%d\n", arr1[month - 1]);
        }
       
    }
    return 0;
}
发表于 2024-03-12 21:27:36 回复(0)
#include <stdio.h>
void YearDay(int a, int b) {
    int arr[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int day = *(arr + b);// *(arr + b) == arr[b] 这两个相等
    if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) {
        if (b == 2) {
            day++;
        }  
    }
    printf("%d\n", day); 
}
int main() {
    int a, b;
    while (scanf("%d %d", &a, &b) != EOF) {
        YearDay(a, b);
    }
    return 0;
}

发表于 2024-02-29 17:01:28 回复(0)
#include <stdio.h>

int main() {
   int year;
   int month;
   while(scanf("%d%d",&year,&month)!=EOF){
    if(year%4==0||year%400==0){
       if(month==2){
        printf("29\n");
       }
       else if(month==1||month ==3||month==5||month==7||month==8||month==10||month==12){
        printf("31\n");
       }
       else{
        printf("30\n");
       }
       
    }
    else{
        if(month==2){
        printf("28\n");
       }
       else if(month==1||month ==3||month==5||month==7||month==8||month==10||month==12){
        printf("31\n");
       }
       else{
        printf("30\n");
       }
    }

   }

    return 0;
}
编辑于 2024-02-19 14:10:00 回复(0)
#include <stdio.h>
int main()
{
    int day=0,year,month;
    int arr[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    while(scanf("%d %d",&year,&month)!=-1)
    {
        if(year%4==0&&year%100!=0||year%400==0)
            {
                arr[1]=29;
                printf("%d\n",arr[month-1]);
                arr[1]=28;//判断闰年2月变为29之后,重新变回28
            }//不影响后面一组数据输出
        else
            printf("%d\n",arr[month-1]);
    }
    return 0;
}
发表于 2024-01-23 16:10:11 回复(0)