算法提高 任意年月日历输出

时间限制:1.0s 内存限制:512.0MB

已知2007年1月1日为星期一。

设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。

为完成此函数,设计必要的辅助函数可能也是必要的。其中输入为年分和月份。

样例输入:

2007 1

样例输出:

Calendar 2007-01

Su Mo Tu We Th Fr Sa

 1  2  3  4  5  6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

样例输入:

2010 9

样例输出:

Calendar 2010-09

Su Mo Tu We Th Fr Sa

       1  2  3  4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

注意:短线“-”个数要与题目中一致,否则系统会判为错误。



数据规模和约定

  输入数据中每一个数的范围。

  例:年 2007-3000,月:1-12。

import java.util.Scanner;

public class 日历 {
public static boolean isLeap(int year) {
boolean flag = false;
if(year % 4 == 0 && year % 100 != 0)
flag = true;
else if(year % 400 == 0)
flag = true;
else
flag = false;
return flag;
}
public static int getDay(int month, int year) {
int day = 0;
if (month == 2) {
if (isLeap(year))
day = 29;
else
day = 28;
} else if (month < 8) {
if(month % 2 == 0)
day = 30;
else
day = 31;
} else {
if(month % 2 == 0)
day = 31;
else
day = 30;
}
return day;
}
public static int getFirst(int month, int year) {
int first = 0;
int distance = year - 2007;
int day = 0; while (distance / 4 >= 1) {
day += (365 * 4 + 1);
distance -= 4;
}
int count = 0;
while (distance > 0) {
if (count == 1)
day += 366;
else
day += 365;
count++;
distance--;
}
int fre = 0;
if(isLeap(year))
fre = 31 + 29;
else
fre = 31 + 28;
int mon[] = {0, 0, 31, fre, fre+31, fre+61, fre+92, fre+122, fre+153, fre+184, fre+214, fre+245, fre+275};
day += mon[month];
first = day % 7;
return first+1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
sc.close(); if(year < 2007)
return; int day = getDay(month, year);
int firstDay = getFirst(month, year); if(month < 10)
System.out.println("Calendar " + year + " - 0" + month);
else
System.out.println("Calendar " + year + " - " + month);
for (int i = 0; i < 21; i++)
System.out.print("-");
System.out.print("\n");
System.out.println("Su Mo Tu We Th Fr Sa ");
for (int i = 0; i < 21; i++)
System.out.print("-");
System.out.print("\n");
int count = 0;
if(firstDay != 7)
for (int i = 0; i < firstDay; i++) {
System.out.print(" ");
count++;
}
for (int i = 1; i <= day; i++) {
if(i < 10)
System.out.print(" " + i + " ");
else
System.out.print(i + " ");
count++;
if(count % 7 == 0)
System.out.print("\n");
}
System.out.print("\n");
for (int i = 0; i < 21; i++)
System.out.print("-");
} }

最新文章

  1. linux显示git commit id,同时解决insmod模块时版本不一致导致无法加载问题
  2. java.sql.SQLException: 无效的列索引
  3. $.extend()了解心得
  4. 图像分割之(五)活动轮廓模型之Snake模型简介
  5. SqlSever基础 ltrim函数 除去字符串左边的空格,右边的中间的不管
  6. angularjs中ng-route和ui-router简单用法的代码比较
  7. Android 手机配office365邮箱
  8. apache开源项目--Synapse
  9. What version of .NET Framework is integrated into what version of OS?
  10. material风格的日期/时间选择:SublimePicker
  11. Go语言基础之反射
  12. Shadow Copying导致ASP.NET应用启动很慢的解决办法
  13. json数组,前后端传值问题,与data时间转毫秒
  14. ZKW线段树入门
  15. [转载]C#堆栈讲解
  16. 《深入理解JVM虚拟机》读书笔记
  17. 跟着未名学Office - 高效工作Outlook
  18. vue搭配axios踩坑
  19. gradle使用总结
  20. MongoDB YAML格式的配置文件

热门文章

  1. [hdu5225]逆序对统计
  2. hdoj 1874 dijkstra
  3. neo4j企业版集群搭建
  4. dedecms织梦建站后怎么防止被黑,加强安全漏洞措施?
  5. layui 关闭弹出层方法
  6. 如何搭建一个WEB服务器项目(六)—— 上传图片至服务器
  7. Codeforces1144A(A题)Diverse Strings
  8. CF948B Primal Sport
  9. vim的基础命令
  10. Verilog代码和FPGA硬件的映射关系(四)