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厘米高的板凳, ...
随机推荐
- [csu1508 地图的四着色]二分图染色
抽象后的题意:给一个不超过30个点的图,A从中选不超过5个点涂红绿两种颜色,B用黑白两种颜色把剩下的涂完,任意一条边两端的颜色不同,求每种颜色至少用涂一次的方案数 思路:枚举A涂的点的集合,将原图分成 ...
- Spring Boot Admin实现服务健康预警
Over View 上一篇文章主要介绍了Spring Boot Admin的概况以及我们如何在系统中引入和使用Spring Boot Admin,以此来帮助我们更加了解自己的系统,做到能快速发现.排查 ...
- 第二篇:TTS(Text To Speech)语音合成
TTS(Text To Speech)语音合成: 百度AI语音合成SDK文档--python SDK: https://ai.baidu.com/docs#/TTS-Online-Python-SDK ...
- Flow Control
Exercise-01 Max Of Two Numbers Write a function that takes two numbers and returns the maximum of th ...
- Smarty模板引擎原理概述
smarty(模板引擎,模板技术) 使用smarty主要是为了实现逻辑和外在内容的分离: 特点: 1.速度快(因为第二次执行的时候使用第一次执行时生成的编译文件) 2.缓存技术(正是因为缓存技术,使得 ...
- WPF之数据绑定Data Binding
一般情况下,应用程序会有三层结构:数据存储层,数据处理层(业务逻辑层),数据展示层(UI界面). WPF是“数据驱动UI”. Binding实现(通过纯C#代码) Binding分为source和ta ...
- 设计MyTime类 代码参考
#include <iostream> #include <cstdio> using namespace std; class MyTime { private: int h ...
- git status 命令详解
git status命令表示:文件,文件夹在工作区,暂存区的状态,下图就是文件,文件夹三种状态: Changes to be committed: (use "git restore --s ...
- python爬取王者荣耀全英雄皮肤
import os import requests url = 'https://pvp.qq.com/web201605/js/herolist.json' herolist = requests. ...
- bootstrap table Showing 1 to 5 of 5 rows 页码显示英文
注意导包先后顺序bootstrap-table-zh-CN.js链接:https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.16.0/locale/b ...