JAVA35. 输出某一年的各个月份的天数
描述
输入任意年份,输出该年份各月天数(请使用 Calendar 类中的方法)输入描述
任意年份(int类型整数)输出描述
示例1
输入:
2021
输出:
2021年1月:31天 2021年2月:28天 2021年3月:31天 2021年4月:30天 2021年5月:31天 2021年6月:30天 2021年7月:31天 2021年8月:31天 2021年9月:30天 2021年10月:31天 2021年11月:30天 2021年12月:31天
C++ 解法, 执行用时: 3ms, 内存消耗: 536KB, 提交时间: 2022-01-12
#include<iostream> #include<cstdio> using namespace std; int dayTable[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} }; bool IsLeapYear(int year){ return(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int main(){ int year,month,day; while(scanf("%d", &year) != EOF){ int number= 0; int row = IsLeapYear(year); for(int i=1;i<=12;++i){ cout<<year<<"年"<<i<<"月:"<<dayTable[row][i]<<"天\n"; } } return 0; }
Java 解法, 执行用时: 30ms, 内存消耗: 10604KB, 提交时间: 2021-11-25
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); int year = console.nextInt(); //write your code here...... if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "年1月:31天"); System.out.println(year + "年2月:29天"); System.out.println(year + "年3月:31天"); System.out.println(year + "年4月:30天"); System.out.println(year + "年5月:31天"); System.out.println(year + "年6月:30天"); System.out.println(year + "年7月:31天"); System.out.println(year + "年8月:31天"); System.out.println(year + "年9月:30天"); System.out.println(year + "年10月:31天"); System.out.println(year + "年11月:30天"); System.out.println(year + "年12月:31天"); } else { System.out.println(year + "年1月:31天"); System.out.println(year + "年2月:28天"); System.out.println(year + "年3月:31天"); System.out.println(year + "年4月:30天"); System.out.println(year + "年5月:31天"); System.out.println(year + "年6月:30天"); System.out.println(year + "年7月:31天"); System.out.println(year + "年8月:31天"); System.out.println(year + "年9月:30天"); System.out.println(year + "年10月:31天"); System.out.println(year + "年11月:30天"); System.out.println(year + "年12月:31天"); } } }
Java 解法, 执行用时: 30ms, 内存消耗: 10712KB, 提交时间: 2021-11-01
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); int year = console.nextInt(); //write your code here...... int mouth = 1; int days = 30; while (mouth <= 12) { if (mouth == 2) { if (isLunarYear(year)) { System.out.print(year + "年" + mouth + "月" + ":" + 29 + "天\n"); } else { System.out.print(year + "年" + mouth + "月" + ":" + 28 + "天\n"); } mouth++; continue; } else if ((mouth <= 7 && mouth % 2 != 0) || (mouth > 7 && mouth % 2 == 0)) { System.out.print(year + "年" + mouth + "月" + ":" + (days+1) + "天\n"); } else { System.out.print(year + "年" + mouth + "月" + ":" + days + "天\n"); } mouth++; } } private static boolean isLunarYear(int year) { if ((year % 4 == 0) && (year % 400 != 0) || (year % 400 == 0)) { return true; } return false; } }
Java 解法, 执行用时: 32ms, 内存消耗: 10560KB, 提交时间: 2021-11-04
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); int year = console.nextInt(); //write your code here...... int flag=0; if(year%4==0&&year%100!=0||year%400==0){ flag=1; } for(int i=1;i<=12;i++){ if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){ System.out.println(year+"年"+i+"月:31天"); }else if(i==4||i==6||i==9||i==11){ System.out.println(year+"年"+i+"月:30天"); }else{ if(flag==1){ System.out.println(year+"年"+i+"月:29天"); }else{ System.out.println(year+"年"+i+"月:28天"); } } } } }
Java 解法, 执行用时: 32ms, 内存消耗: 11036KB, 提交时间: 2021-11-21
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); int year = console.nextInt(); //write your code here...... int mouth = 1; int days = 30; while (mouth <= 12) { if (mouth == 2) { if (isLunarYear(year)) { System.out.print(year + "年" + mouth + "月" + ":" + 29 + "天\n"); } else { System.out.print(year + "年" + mouth + "月" + ":" + 28 + "天\n"); } mouth++; continue; } else if ((mouth <= 7 && mouth % 2 != 0) || (mouth > 7 && mouth % 2 == 0)) { System.out.print(year + "年" + mouth + "月" + ":" + (days+1) + "天\n"); } else { System.out.print(year + "年" + mouth + "月" + ":" + days + "天\n"); } mouth++; } } private static boolean isLunarYear(int year) { if ((year % 4 == 0) && (year % 400 != 0) || (year % 400 == 0)) { return true; } return false; } }