`
水木清华77
  • 浏览: 36265 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Problem19

阅读更多
package com.shui.mu.yao.io.algorithm;

import java.util.Calendar;

/**
 * 
 * @author shuimuqinghua77 @date 2011-12-9上午09:58:49
 */
public class Problem19 {

	public static void main(String[] args) {
		int count = oneDayTotal(1901,2000,/** 1901年1月1日是周二 **/Calendar.TUESDAY,/**期望获取每个月第一天周日的总数**/Calendar.SUNDAY);
		System.out.println(count);

	}
/**
 * 
 * @param startYear开始年份
 * @param endYear结束年份
 * @param startWeek开始年份1月1日是星期几
 * @param wantWeek 期望每月第一天为星期几
 * @return
 */
	private static int oneDayTotal(	int startYear,int endYear ,int startWeek,int wantWeek) {
		
		int week = startWeek;
		int count = 0;/**记录下个月第一天是周日的总数**/
		int week_size = 7;/**一周7天 **/
		/***判断 startYear年1月1日是不是周日**/
		if (week == wantWeek)
			count++;
		for (int year = startYear; year <= endYear; year++) {
			for (int month = 1; month <= 12; month++) {
				int days = getDays(month, year);
				week = (week + days % week_size) % week_size;
				if (week == wantWeek)
						count++;
			}
		}
		/***判断 endYear年1月1日是不是周日,是的话减去**/
		if (week == wantWeek)
			count--;
		return count;
	}

	private static boolean isLeapYear(int year) {
		if (year % 4 == 0 && year % 100 > 0)
			/** 不是世纪,但可以被4整除,为闰年 **/
			return true;
		else if (year % 400 == 0)
			/** 世纪,但可以被400整除,为闰年 **/
			return true;
		else
			return false;

	}

	private static int getDays(int month, int year) {
		if (month == 2) {
			if (isLeapYear(year))
				return 29;
			else
				return 28;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11)
			return 30;
		else
			return 31;
	}
}
分享到:
评论

相关推荐

    计算机网络第六版答案

    19. a) 500 kbps b) 64 seconds c) 100kbps; 320 seconds 20. End system A breaks the large file into chunks. It adds header to each chunk, thereby generating multiple packets from the file. The header...

    MySQL数据库考试试题.docx

    19. 数据操纵语句:SELECT、INSERT、UPDATE、DELETE 等(Problem 19) SQL 语言中的数据操纵语句包括 SELECT、INSERT、UPDATE、DELETE 等。 20. 实体完整性:设置外键(Problem 20) 设置外键是一种实现实体完整...

    Computer-Based.Problem.Solving.Process

    Chapter 19. Problem of Protection Chapter 20. Timing Program Execution Chapter 21. Efficiency of Batch Operating Systems Chapter 22. Convenience of the BOS Chapter 23. Real-Time Systems

    量化投资-以R语言为工具-课后题元数据

    例如,problem28-4.RData、problem19-5.RData等可能是课程中关于特定问题的解答,如回测策略、风险评估等。R的`load`函数可以用于恢复这些数据,接着我们可以研究其中的代码和结果,学习如何使用R进行金融建模和模拟...

    英语七年级(上册)--必须掌握的单词和短语.doc

    18. **问题** - problem 19. **头痛** - headache 通过学习这些单词和短语,七年级的学生将能够更好地理解课本内容,提高听说读写的能力,并逐渐掌握日常英语交流的基础。记住,持续练习和使用这些词汇是掌握它们的...

    Graphics Gems (Vol.2)

    6. Appolonius’s 10th Problem 19 Jon Rokne 7. A Peano Curve Generation Algorithm C 25 Ken Musgrave 8. Space-Filling Curves and a Measure of Coherence C 26 Douglas Voorhies 9. Scanline Coherent ...

    Prime Ring Problem 深度探索

    int M, g, a[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, ag[20], c[20]; ``` - 初始化数组`a`,用于存储可能填入环中的数字。 - `ag`数组用于标记数字是否已被使用。 - `c`...

    The Steiner Tree Problem(扫描版)

    施泰因尔树问题(Steiner Tree Problem),作为图论中的一个重要问题,主要研究如何在一张带权图中找到一个连接所有指定顶点的最小权重树,并允许该树包含额外的顶点(称为施泰因尔顶点)。这个问题在多个领域都有...

    Problem Solving in Data Structures & Algorithms Using Java

    CHAPTER 19: DYNAMIC PROGRAMMING CHAPTER 20: BACKTRACKING AND BRANCH-AND-BOUND CHAPTER 21: COMPLEXITY THEORY AND NP COMPLETENESS CHAPTER 22: INTERVIEW STRATEGY CHAPTER 23: SYSTEM DESIGN

    string-problem(POJ).rar_POJ 19_poj

    标题中的"string-problem(POJ).rar_POJ 19_poj"表明这是一个与ACM编程竞赛相关的压缩包,特别关注的是字符串处理问题。在ACM编程竞赛中,字符串问题是一个常见的类别,通常涉及到字符串的查找、比较、操作、模式匹配...

    project euler problem 5

    具体来说,N=2^4×3^2×5^1×7^1×11^1×13^1×17^1×19^1=232792560。 通过这种方式,我们不仅解决了Project Euler问题5,还了解了如何高效地计算较大范围内的最小公倍数。这种方法不仅适用于特定的数学问题,也...

    0-1-knapsack-problem-master (19).zip

    【标题】"0-1-knapsack-problem-master (19).zip" 提到的是一个与0-1背包问题相关的代码库。0-1背包问题是计算机科学领域经典的动态规划问题,广泛应用于资源优化和决策制定。在HarmonyOS应用开发的高级阶段,理解并...

    0-1-knapsack-problem-master (19)c.zip

    在“0-1-knapsack-problem-master (19)c.zip”这个压缩包中,我们可以推测它包含了一个用C语言实现的0-1背包问题的代码库。这可能是为了教学或研究目的,让学习者了解如何用编程解决这类问题。C语言是一种底层、高效...

    Problema19

    在C#编程中,"Problema19"可能是一个特定的编程挑战或者项目,而提供的文件"Problema19-main"很可能包含了这个项目的主程序代码。在深入讨论之前,我们先了解一下C#的基础知识。 C#是微软公司开发的一种面向对象的...

    GTM277 The Moment Problem, Konrad Schmüdgen, Springer 2017.pdf

    《矩问题》(The Moment Problem)是数学领域的一本重要著作,由Konrad Schmüdgen撰写,并于2017年由Springer出版社出版。本书属于Springer的《研究生数学系列》(Graduate Texts in Mathematics,简称GTM),该...

    Problem.Solving.in.Data.Structures.and.Algorithms.Using.Cplusplus.epub

    Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This is the skill which tech companies like Google, Amazon, Microsoft, Adobe and many others ...

    matlab代码循环运行-tsp2019:tsp2019

    问题19有解析解,因此,求解十分容易,通过problem19.m 函数求解 现在代码写的还是太乱,后面要改进一下,避免出现这么多函数。 所用杨老师代码 使用了杨老师toeplitz矩阵的范德蒙德矩阵分解函数,VanDec.m 这里要...

    matlab函数公式 19个

    (19)Rotated Schwefel’s Problem1.2 Rotated Schwefel’s Problem1.2 是一种典型的非线性优化问题,定义为: f(x) = ∑[i=1 to n] (xᵢ - oᵢ)² + ∑[i=1 to n-1] (xᵢ - x₋₁ - oᵢ + o₋₁)² 其中 oᵢ 是...

    openssl-libs-1.0.2k-19.el7.x86_64

    本文将深入探讨在CentOS8系统上安装RabbitMQ时遇到的依赖问题,以及如何解决这个问题,特别是与"openssl-libs-1.0.2k-19.el7.x86_64"这个组件相关的知识点。 首先,让我们理解一下标题中的"openssl-libs-1.0.2k-19....

Global site tag (gtag.js) - Google Analytics