Java实现 蓝桥杯VIP 算法提高 任意年月日历输出
算法提高 任意年月日历输出
时间限制: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("-");
}
}
Java实现 蓝桥杯VIP 算法提高 任意年月日历输出的更多相关文章
- Java实现 蓝桥杯VIP 算法提高 数字黑洞
算法提高 数字黑洞 时间限制:1.0s 内存限制:256.0MB 问题描述 任意一个四位数,只要它们各个位上的数字是不全相同的,就有这样的规律: 1)将组成该四位数的四个数字由大到小排列,形成由这四个 ...
- Java实现 蓝桥杯VIP 算法提高 产生数
算法提高 产生数 时间限制:1.0s 内存限制:256.0MB 问题描述 给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: 规则 ...
- Java实现 蓝桥杯VIP 算法提高 淘淘的名单
算法提高 淘淘的名单 时间限制:100ms 内存限制:8.0MB 问题描述 by ZBY- ? 淘淘拿到了一份名单,他想对上面的名字进行处理,挑出一些特殊的名字,他请你来帮忙. 淘淘关注以下名字: 如 ...
- Java实现 蓝桥杯VIP 算法提高 项链
算法提高 项链 时间限制:1.0s 内存限制:512.0MB 问题描述 由 n(1≤n≤100) 个珠子组成的一个项链,珠子有红.蓝.白三种颜色,各种颜色的珠子的安排顺序由键盘输入的字符串任意给定.蓝 ...
- Java实现 蓝桥杯VIP 算法提高 Quadratic Equation
算法提高 Quadratic Equation 时间限制:1.0s 内存限制:512.0MB 问题描述 求解方程ax2+bx+c=0的根.要求a, b, c由用户输入,并且可以为任意实数. 输入格式: ...
- Java实现 蓝桥杯VIP 算法提高 研究兔子的土豪
试题 算法提高 研究兔子的土豪 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 某天,HWD老师开始研究兔子,因为他是个土豪 ,所以他居然一下子买了一个可以容纳10^18代兔子的巨大 ...
- Java实现 蓝桥杯VIP 算法提高 3-2求存款
算法提高 3-2求存款 时间限制:1.0s 内存限制:256.0MB 问题描述 见计算机程序设计基础(乔林)P50第5题. 接受两个数,一个是用户一年期定期存款金额,一个是按照百分比格式表示的利率,计 ...
- Java实现 蓝桥杯VIP 算法提高 3-3求圆面积表面积体积
算法提高 3-3求圆面积表面积体积 时间限制:1.0s 内存限制:256.0MB 问题描述 接受用户输⼊的数值,输出以该值为半径的(1)圆面积,(2)球体表面积,(3)球体体积.pi 取值3.1415 ...
- Java实现 蓝桥杯VIP 算法提高 5-3日历
算法提高 5-3日历 时间限制:1.0s 内存限制:256.0MB 问题描述 已知2007年1月1日为星期一.设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印.为 ...
随机推荐
- Bootstrap:Bootstrap_table第一篇:快速用bootstrap_table(支持参数)筛选并展示数据,固定表格前几列,实现表格单元格编辑
1.准备好css和js文件 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstr ...
- 错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplic
错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误. 如果 ...
- chrome安装工具
0x00 简介 今天在知识星球的小迪渗透吧对外交流群里看到Web安全从业者必备Chrome插件这篇帖子,看完之后,我虽然还是个学生,但我也是个垃圾啊.我的chrome上面没有一个上面描述的工具,真的是 ...
- RobotFramework Selenium2Library 关键字详解
*** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arguments] ${locator} Che ...
- javascript----放大模式
放大模式 1.介绍:放大模式降低模块之间直接的联系,降低耦合,当一个模块出现错误,不会影响另一个模块的功能
- spark机器学习从0到1决策树(六)
一.概念 决策树及其集合是分类和回归的机器学习任务的流行方法. 决策树被广泛使用,因为它们易于解释,处理分类特征,扩展到多类分类设置,不需要特征缩放,并且能够捕获非线性和特征交互. 诸如随机森林和 ...
- vs2015 cppunit配置及使用
目录 第一步 第二步 第三步 编译生成lib库 使用 calculator类测试 代码部分 第一步 下载源代码 http://sourceforge.net/projects/cppunit/file ...
- easyui及读取xml
本地测试地址例如http://localhost:6541/TreeExam/AuthorityTree TreeExam 是TreeExamController AuthorityTree是Tree ...
- hdu4757 可持续字典树
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Sub ...
- python常见面试题讲解(二)计算字符个数
题目描述 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数.不区分大小写. 输入描述: 第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字 ...