首页 > 试题广场 >

获得月份天数

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

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


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

输入

2008 2

输出

29
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()){
            int y = in.nextInt(), m = in.nextInt();
            
            if (m == 2){
                if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){
                    System.out.println("29");
                } else {
                    System.out.println("28");
                }
            }
            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
                System.out.println("31");
            } else if (m == 4 || m == 6 || m == 9 || m == 11) {
                System.out.println("30");
            }
        }
    }

发表于 2024-08-03 19:29:19 回复(0)
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int year = in.nextInt();
            String month = in.next();
            String[] big = new String[] {"1", "3", "5", "7", "8", "10", "12"};
            List<String> bigList = Arrays.asList(big);
            // 2月
            if ("2".equals(month)) {
                //闰年
                if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) {
                    System.out.println(29);
                } else {
                    System.out.println(28);
                }
            //大月
            } else if (bigList.contains(month)) {
                System.out.println(31);
            } else {
                System.out.println(30);
            }
        }
    }
}
我好蠢na
发表于 2024-02-29 19:49:38 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int year = in.nextInt();
            int month = in.nextInt();

            // 判断是否闰年
            boolean isRunNian = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

            switch(month){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    System.out.println(31);
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    System.out.println(30);
                    break;
                case 2:
                    System.out.println(isRunNian ? 29: 28);        
            }
        }
    }
}

发表于 2023-02-03 18:05:05 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int year = in.nextInt();
            int month = in.nextInt();
            // 31天月份的正则表达式
            String day31 = "[13578]|10$|12$";
            // 30天月份的正则表达式
            String day30 = "[469]|11$"; 
            String strMonth = String.valueOf(month);
            
            if(month == 2){
                if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
                    System.out.println(29);
                }else{
                    System.out.println(28);
                }
            }else {
                if(strMonth.matches(day31)){
                    System.out.println(31);
                }
                if(strMonth.matches(day30)){
                    System.out.println(30);
                }
            }
        }
    }
}

发表于 2022-10-31 20:58:07 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int y = in.nextInt();
            int m = in.nextInt();
            if(y%4==0&&y%100!=0){
                if(m==2){
                    System.out.println(29);
                }
            }else if(y%400==0){
                if(m==2){
                    System.out.println(29);
                }
            }else if(m==2){
                System.out.println(28);
            }
            
            if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
                System.out.println(31);
            }else if(m==4||m==6||m==9||m==11){
                System.out.println(30);
            }
        }
    }
}

发表于 2022-10-28 21:31:35 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int year = scanner.nextInt();
            int month = scanner.nextInt();
            if(month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12){
                System.out.println("31");
            }else if(month == 4 | month == 6 | month == 9 | month == 11){
                System.out.println("30");
            }else if(month == 2){
                if((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0){
                    System.out.println("29");
                }else{
                    System.out.println("28");
                }
            }
      }
    }
}

发表于 2022-06-27 15:54:16 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int y = sc.nextInt();
            int run = 0;


            if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
                System.out.println("31");
            } else {
                if (y == 2) {
                    if (n % 4 == 0 && n % 100 != 0) {
                        System.out.println("29");
                    } else if (n % 1000 == 0) {
                        System.out.println("29");
                    } else {
                        System.out.println("28");
                    }
                } else {
                    System.out.println("30");
                }
            }
        }
    }
}

发表于 2022-06-17 17:46:14 回复(0)
import java.util.*;
public class Main{
    public static int getday(int y,int m){
        int day=0;
        if(m==1|m==3|m==5|m==7|m==8|m==10|m==12){
            return day=31;
        }
        else if(m==4|m==6|m==9|m==11){
            return day=30;
        }
        else if(m==2){
            if(y%4==0&&y%100!=0|y%400==0){
                return day=29;
            }
            else
                return day=28;
        }
    return day;    
    }
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        while(input.hasNext()){
        int year=input.nextInt();
        int mon=input.nextInt();
        System.out.println(getday(year,mon));
        }

    }
}

发表于 2022-05-02 11:28:16 回复(0)
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @Title: 获得月份天数
 * @Remark: KiKi想获得某年某月有多少天,请帮他编程实现。输入年份和月份,计算这一年这个月有多少天。
 * @Author: ijunfu
 * @Version: 1.0.0
 * @Date: 2022-03-19
 */
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNextLine()) {
            List<Integer> list = Arrays.stream(in.nextLine().split(" ")).map(x -> Integer.valueOf(x)).collect(Collectors.toList());

            int year = list.get(0);
            int month = list.get(1)-1;

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);

            int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            System.out.println(day);
        }
    }
}

发表于 2022-03-19 20:04:48 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextInt()) {
            int year = scan.nextInt();
            int month = scan.nextInt();
            //判断月份
            switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        System.out.println("31");
                        break;
                    case 2:
                        if (isLeapYear(year)) {
                            System.out.println("29");
                        } else {
                            System.out.println("28");
                        }
                        break;
                    default:
                        System.out.println("30");
                        break;     
            }
        }
    }
    public static boolean isLeapYear(int year) {
        if((year % 100 != 0 && year % 4 == 0)||(year %400 == 0)) {
            return true;
        } else {
            return false;
        }
    }
}

发表于 2021-10-18 15:30:11 回复(0)
import java.util.Scanner;
import java.util.Arrays;
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();
       boolean flg=(year%4==0 && year%100!=0) || (year%400==0);
        String[] str1=new String[]{"1","3","5","7","8","10","12"};
        String[] str2=new String[]{"4","6","9","11"};
        if(flg && month==2){
            System.out.println(29);
        }else if(flg && Arrays.asList(str1).contains(month+"")){
            System.out.println(31);
        }else if(flg && Arrays.asList(str2).contains(month+"")){
            System.out.println(30);
        }else if(!flg && month==2){
            System.out.println(28);
        }else if(!flg && Arrays.asList(str1).contains(month+"")){
             System.out.println(31);
        }else if(!flg && Arrays.asList(str2).contains(month+"")){
             System.out.println(30);
        }
        }
        
    }
}



发表于 2021-10-11 19:44:18 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
           Integer year = sc.nextInt();
           Integer month = sc.nextInt();
               switch (month) {
                   case 1:
                   case 3:
                   case 5:
                   case 7:
                   case 8:
                   case 10:
                   case 12:
                       System.out.println("31");
                       break;
                   case 4:
                   case 6:
                   case 9:
                   case 11:
                       System.out.println("30");
                       break;
                   case 2:
                       if(year%4==0){ System.out.println("29");}
                       else {System.out.println("28");}
                       break;
               }
        }
    }
}不要忘记break;就行
发表于 2021-08-16 13:59:06 回复(0)
建议看看题解,比我写的强
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int yy=input.nextInt();
            int MM=input.nextInt();
            if(MM==2){
                if(yy%400==0||yy%4==0&&yy%100!=0)System.out.println(29);
                else System.out.println(28);
                continue;
            }
            if(MM==1||MM==3||MM==5||MM==7||MM==8||MM==10||MM==12){
                System.out.println(31);
                continue;
            }
            System.out.println(30);
        }
    }
}

发表于 2020-10-30 11:30:20 回复(0)
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)