Java实现 蓝桥杯VIP 算法提高 5-3日历
算法提高 5-3日历
时间限制:1.0s 内存限制:256.0MB
问题描述
已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。为完成此函数,设计必要的辅助函数也是必要的。
样例输入
一个满足题目要求的输入范例。
例:
2050 3
样例输出
与上面的样例输入对应的输出。
例:

数据规模和约定
输入数据中每一个数的范围。
例:年 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 算法提高 5-3日历的更多相关文章
- Java实现 蓝桥杯VIP 算法提高 任意年月日历输出
算法提高 任意年月日历输出 时间限制:1.0s 内存限制:512.0MB 已知2007年1月1日为星期一. 设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印. 为 ...
- 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 算法提高 11-2删除重复元素
算法提高 11-2删除重复元素 时间限制:10.0s 内存限制:256.0MB 问题描述 为库设计新函数DelPack,删除输入字符串中所有的重复元素.不连续的重复元素也要删除. 要求写成函数,函数内 ...
- Java实现 蓝桥杯VIP 算法提高 P0401
算法提高 P0401 时间限制:1.0s 内存限制:256.0MB 输入一个无符号整数x,输出x的二进制表示中1的个数. 输入: 76584 输出: 7 import java.util.Scanne ...
- Java实现 蓝桥杯VIP 算法提高 理财计划
算法提高 理财计划 时间限制:1.0s 内存限制:256.0MB 问题描述 银行近期推出了一款新的理财计划"重复计息储蓄".储户只需在每个月月初存入固定金额的现金,银行就会在每个月 ...
- Java实现 蓝桥杯VIP 算法提高 解二元一次方程组
算法提高 解二元一次方程组 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个二元一次方程组,形如: a * x + b * y = c; d * x + e * y = f; x,y代 ...
- Java实现 蓝桥杯VIP 算法提高 陶陶摘苹果2
算法提高 陶陶摘苹果2 时间限制:1.0s 内存限制:256.0MB 问题描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出n个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个30厘米高的板凳, ...
随机推荐
- 【hdu5100】棋盘覆盖
http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目大意: 用1*k的木块铺n*n的棋盘,求多铺满多少个单位格. 方法: n < k,显然无解:n ...
- 《C程序设计语言》 练习3-3
问题描述 编写expand(s1,s2),将字符串s1中类似于a-z类的速记符号在字符串s2中扩展为等价的完整列表abc.....xyz.该函数可以处理大小写字母和数字,并可以处理a-b-c,a-z0 ...
- 在linux下执行git clone、git pull 、git push等操作免密
1. 通过ssh密钥实现 ssh-keygen -t rsa -C "你的邮箱" -f "自己定义的目录" 打开: id_rsa.pub ,将文件内容复制到 g ...
- Redux:Reducers
action只是描述了“发生了什么事情(导致state需要更新)”,但并不直接参与更新state的操作.state的更新由reducer函数执行. 其基本模式是:(state,action)=> ...
- Codeforces1183C(C题)Computer Game
Vova is playing a computer game. There are in total nn turns in the game and Vova really wants to pl ...
- PART(Persistent Adaptive Radix Tree)的Java实现源码剖析
论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...
- Go语言学习目录
第一章 Go环境搭建 1.1 Linux搭建Go环境 1.2 Mac搭建Go环境 1.3 Windows搭建Go环境 第二章 Go语言特性 2.1 Go特征 2.2 Go变量类型 2.3 Go内置函数 ...
- 使用基于MVC2模式创建新闻网站
1.什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示 ...
- redis起步 zz
Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介 Redis是一个key-value存储系统.和Memcached类似,但是解决了 ...
- 高效、可维护、组件化的CSS
如何写出更加高效的CSS? 主要有以下四个关键点: 高效的CSS 可维护的CSS 组件化的CSS hack-free CSS 1.书写高效的CSS代码 * 使用 外联样式 替代行间 样式或者内嵌样式. ...