首页 > 试题广场 >

获得月份天数

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

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


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

输入

2008 2

输出

29
#include <iostream>
using namespace std;

int main()
{
    int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int y, m;
    while (cin >> y >> m)
    {
        if(y % 400 == 0 || y % 4 == 0 && y % 100 != 0) a[1] = 29;
        else a[1] = 28;
        cout << a[m - 1] << endl;
    }

}

发表于 2022-02-26 14:13:23 回复(0)
#include <stdio.h>
int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF){
        if(b==1||b==3||b==5||b==7||b==8||b==10||b==12){
            b=31;
        }else if(b==2){
            b=28;
            if((a%400==0)||(a%4==0&&a%100!=0)){
                b++;
            }
        }else{
            b=30;
        }
        printf("%d\n",b);
    }
}

发表于 2020-04-10 22:44:36 回复(1)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int year = sc.nextInt();
            int month = sc.nextInt();
            switch (month){
                case 2:
                    if ((year%100==0 && year%400!=0) || year%4!=0){ 
                        System.out.println(28); 
                    }else { 
                        System.out.println(29); 
                    };
                break;
                case 4:System.out.println(30);break;
                case 6:System.out.println(30);break;
                case 9:System.out.println(30);break;
                case 11:System.out.println(30);break;
                default: System.out.println(31);
            }
        }
    }
}

发表于 2020-04-19 00:10:50 回复(0)
# 查资料
# 首先要清楚月份天数的分类
# 每年的4,6,9,11月的天数都是30天
# 每年的1,3,5,7,8,10,12月都是31天
# 然后计算2月的天数
# 2月的天数有两种,闰年为29天,非闰年28天
# 判断是否为闰年的标准:
# 四年一润,百年不一定润,四百年一定润。
#
# 先判断百年吗?-->是-->是四百年吗?-->是-->闰年
# 先判断百年吗?-->是-->是四百年吗?-->不是-->平年
# 先判断百年吗?-->不是-->是4的倍数吗?-->是-->闰年
# 先判断百年吗?-->不是-->是4的倍数吗?-->不是-->平年
while True:
    try:
        m,n = map(int,input().split()) # m 为年, n 为月
        a = [4,6,9,11]
        b = [1,3,5,7,8,10,12]
        if n in a:
            print(30)
        elif n in b :
            print(31)
        elif n ==2:
            if m % 100 ==0: # 是 百年

                q = m /100  # 取前两位
                if q % 4 ==0:   # 是 四百年 -->闰年 2月29天
                    print(29)
                else:           # 不是 四百年 -->平年 2月28天
                    print(28)

            else:            # 不是 百年

                 if m % 4 ==0 : # 是 4的倍数 -->闰年 2月29天
                    print(29)
                 else:          # 不是 4的倍数 -->平年 2月28天
                    print(28)
    except:
        break
# 扩展 
# 判断可以合并 if (year % 4 == 0 and year % 100 != 0)&nbs***bsp;(year % 400 == 0): 符合就是闰年

发表于 2021-09-15 11:16:35 回复(0)
C语言:
#include<stdio.h>
int main(){
    int y,m;
    while(~scanf("%d %d",&y,&m)){
        if(m==4||m==6||m==9||m==11) printf("30\n");
        else if(m==2){
                if(((y%4==0)&&(y%100!=0))||(y%400==0)) 
                     printf("29\n");
                else printf("28\n");
        }
        else printf("31\n");
    }
}

发表于 2021-06-01 23:38:30 回复(0)
# 运用了字典
day=0
while True:
    try:
        year,month=map(int,input().split())
        if (year%4==0 and year%100!=0)&nbs***bsp;year%400==0:
            if month==2:
                day=29
        else:
            if month==2:
                day=28
        dic={1:31,2:day,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
        print(dic[month])
    except:
        break


发表于 2024-10-20 17:47:46 回复(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)
#include<stdio.h>
int main(){
    int year=0;
    int month=0;
    int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    while(scanf("%d %d",&year,&month)!=EOF){
        if((year%400==0)||(year%4==0&&year%100!=0)){
            days[2]=29;
        }
        else
            days[2]=28;
        printf("%d\n",days[month]);
        
    }
    return 0;
}
发表于 2022-08-27 21:20:35 回复(0)
#include<stdio.h>
int fun(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)
    {
        int d = days[m];
        if (fun(y) == 1 && m == 2)
        {
            d++;
        }
        printf("%d\n", d);
    }
    return 0;
}
发表于 2022-07-02 17:33:00 回复(1)
#include<stdio.h>
int main(){
    int year,month,day;
    while(scanf("%d %d",&year,&month)!=EOF){
        //判断闰年,2月份多一天
        if( (year % 4 == 0 && year % 100 !=0) || (year % 400 ==0) ){
            if(month == 2) day=29;
        }
        else{
            if(month == 2) day=28;
        }
        switch(month){
            case 1:day=31;break;
            case 3:day=31;break;
            case 4:day=30;break;
            case 5:day=31;break;
            case 6:day=30;break;
            case 7:day=31;break;
            case 8:day=31;break;
            case 9:day=30;break;
            case 10:day=31;break;
            case 11:day=30;break;
            case 12:day=31;break;
        }
        printf("%d\n",day);
    }
    return 0;
}

发表于 2022-06-14 13:07:28 回复(0)
#include <stdio.h>

int main(){
    int year, month = 0;
    while(scanf("%d %d", &year, &month) != EOF){
        switch(month){
            default:
                printf("31\n");
                break;    
            case 4:
            case 6:
            case 9:
            case 11:
                printf("30\n");
                break;
            case 2:
                if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    printf("29\n");
                else
                    printf("28\n");
                break;
        }
    }
    return 0;
}

发表于 2022-05-23 10:51:05 回复(0)
#include<stdio.h>
int main()
{
    int year,month;
    while((scanf("%4d %2d",&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;
        case 2://2月单独拿出来
            if(year%4==0&&year%100!=0||year%400==0)
               {
                 printf("29\n");
                 break;
                }
            else
               {
                printf("28\n");
                break;
               }
    }
    }
        
        
        
}

发表于 2022-03-09 15:06:19 回复(1)
#include<stdio.h>
int main()
{
    int year,month,day;
    while(~scanf("%d %d",&year,&month))
    {
        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%400==0||(year%4==0&&year%100!=0))
                    printf("29\n");
                else
                    printf("28\n");
        }
        
    }
    return 0;
}

发表于 2021-10-30 17:13:01 回复(0)
#include<stdio.h>
#include<stdio.h>
int days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int a,b;
int main()
{
    while(scanf("%d %d",&a,&b)!=EOF)
    {
        int day=days[b-1];//数组检索
        if((a%100!=0 && a%4==0) | a%400==0 && b==2)
        {
            day++;//闰年2月+1
        }
        printf("%d\n",day);
        getchar();
    }
     return 0;
}

发表于 2021-10-22 14:04:13 回复(0)
#include<stdio.h>
int main ()
{ int day = 0;
  int mouth = 0;
  int year = 0;
  while(scanf("%d %d",&year,&mouth)!=EOF)
  {
      //判断是否为闰年
      if((year%4 == 0 && year%100 != 0) || year%400 == 0)
      {
            if(mouth == 2)
            {
                printf("%d\n",29);
            }
          else if(mouth == 4||mouth == 6|| mouth == 9|| mouth == 11)
          {
              printf("%d\n",30);
          }
          else
          {
              printf("%d\n",31);
          }
      }
      else
      {
            if(mouth == 2)
            {
                printf("%d\n",28);
            }
          else if(mouth == 4||mouth == 6|| mouth == 9|| mouth == 11)
          {
              printf("%d\n",30);
          }
          else
          {
              printf("%d\n",31);
          }
      }
  }
    return 0;
}

发表于 2021-08-24 10:41:41 回复(0)
/*获得月份天数*/
#include<cstdio>
(802)#include<iostream>
using namespace std;
int main()
{
	int ping[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	int run[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
	int year;
	int month;
	int day;
	while(cin>>year>>month)
	{
		if(year%400==0||year%4==0&&year%100!=0)
	{
		day = run[month];
		cout<<day<<endl;
	}
	else
	{
		day = ping[month];
		cout<<day<<endl;
	}
	}
	
	
	return 0;
 } 

发表于 2020-04-17 22:17:43 回复(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 y = 0;
	int m = 0;
	int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	while (~scanf("%d %d", &y, &m) )
	{
		int day = days[m - 1];
		if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
		{
			if (m == 2)
				day++;

		}
		printf("%d\n", day);
	}
	
	return 0;
}

发表于 2024-11-22 23:30:07 回复(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>
//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)