第三章第二十一题(科学:某天是星期几)(Science: day of the week)
**3.21(科学:某天是星期几)泽勒一致性是由克里斯汀泽勒开发的用于计算某天是星期几的算法。这个公式是:
h = (q+(26(m+1)/10+k+k/4+j/4+5/j))%7
其中:
- h是一个星期中的某一天(0为星期六;1为星期天;2为星期一;3为星期二;4为星期三;5为星期四;6为星期五)。
- q是某月的第几天。
- m是月份(3为三月,4为四月,……,12为十二月)。一月和二月分别记为上一年的13和14月。
- j是year/100。
- k是该世纪的第几年(即year%100)。
注意:公式中的除法执行一个整数相除。编写程序,提示用户输入年、月、和该月的哪一天,然后显示它是一周中的星期几。
下面是一些运行示例:
Enter year:(e.g.,2012): 2015
Enter month:1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Sunday
Enter year:(e.g.,2012): 2012
Enter month:1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
**3.21(Science: day of the week) Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is
h = (q+(26(m+1)/10+k+k/4+j/4+5/j))%7
- h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, and 6: Friday).
- q is the day of the month.
- m is the month (3: March, 4: April, . . ., 12: December). January and February are counted as months 13 and 14 of the previous year.
- j is year / 100
- k is the year of the century (i.e., year % 100).
Enter year:(e.g.,2012): 2015
Enter month:1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Sunday
Enter year:(e.g.,2012): 2012
Enter month:1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
下面是参考答案代码:
import java.util.*;public class DayOfWeekQuestion21 {public static void main(String[] args){int year, month, day, h, q, m, j, k;// Prompt the user to enter the year,the month and the daySystem.out.print("Enter year: (e.g.,2012):");Scanner input = new Scanner(System.in);year = input.nextInt();System.out.print("Enter month: 1-12:");month = input.nextInt();System.out.print("Enter the day of the month: 1-31:");day = input.nextInt();q = day;if(month == 1 || month == 2){month += 12;year -= 1;}m = month;j = year / 100;k = year % 100;// Calculate the day of the weekh = (q + (26 * (m + 1) / 10) + k + k / 4 + j / 4 + 5 * j) % 7;// Display the day of the weekswitch(h){case 0:System.out.println("Day of the week is Saturday");break;case 1:System.out.println("Day of the week is Sunday");break;case 2:System.out.println("Day of the week is Monday");break;case 3:System.out.println("Day of the week is Tuesday");break;case 4:System.out.println("Day of the week is Wednesday");break;case 5:System.out.println("Day of the week is Thursday");break;case 6:System.out.println("Day of the week is Friday");break;}input.close();}
}
运行效果:
注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)
第三章第二十一题(科学:某天是星期几)(Science: day of the week)相关推荐
- 第四章第二十一题(检查 SSN)(Check SSN)
*4.21(检查 SSN)编写一个程序,提示用户输入一个社保号码,它的格式是DDD-DD-DDDD,其中D是一个数字.你的程序应该判断输入是否合法. 下面是一个运行示例: Enter a SSN: 2 ...
- 1个球从100m落下,每次时,反跳原高度的一半,再落,再反弹,求第10次落地共经过多少m,第10次反弹多高。 谭浩强《c语言程序设计》第五章第十一题
题目 本题是谭浩强<c语言程序设计>第五章第十一题 题目:1个球从100m落下,每次时,反跳原高度的一半,再落,再反弹,求第10次落地共经过多少m,第10次反弹多高. 提示:以下是本篇文章 ...
- 哥伦比亚大学 NLP 第三章(第二部分)
哥伦比亚大学 NLP 第三章(第二部分) 目录 概率上下文无关 CKYCKYCKY 算法 本部分关于上下文无关相关符号的约定均基于第一部分,本部分将继续沿用不再定义 第三章第一部分传送门:https: ...
- 《软件方法》第三章 自测题
UMLChina软件方法各章练习题自测(三) 关于UMLChina 前言 <软件方法>第三章自测题 自测题1 自测题2 关于UMLChina 前言 笔者为在校大三生,初次接触UML建模语言 ...
- 【数据聚类】第三章第二节2:K-Means算法及其Python实现(算法实现、结果展示)
pdf下载(密码:7281) 本文上接:[数据聚类]第三章第二节1:K-Means算法及其Python实现(距离度量方式.目标函数和算法流程) 本文下接:[数据聚类]第三章第二节3:K-Means算法 ...
- 51NOD L4-第三章 树 刷题记录-zgw
第三章 树 2423 叶子节点的数量 AC 3.7 2282 树的深度 AC 3.7 2281 树的Size之和 AC 3.7 2627 树的深度及子树大小 AC 3.10 3039 叶子节点的路径 ...
- 51NOD L4-第三章 树 刷题记录-zyz
第三章 树 2423 叶子节点的数量 2282 树的深度 2281 树的Size之和 2627 树的深度及子树大小 3039 叶子节点的路径 2599 最近公共祖先(LCA) 2621 树上距离 26 ...
- 高红梅:第三章 第二节 身份焦虑与英雄梦
第二节 身份焦虑与英雄梦 海明威一生都在世界各地旅行,其足迹遍及欧洲.非洲.美洲和亚洲,有世界公民的美誉.正如学者卡洛斯·贝克(Baker ,Carlos)所言,"早在他五十五岁荣获诺贝尔文 ...
- 经典算法题每日演练——第二十一题 十字链表
上一篇我们看了矩阵的顺序存储,这篇我们再看看一种链式存储方法"十字链表",当然目的都是一样,压缩空间. 一:概念 既然要用链表节点来模拟矩阵中的非零元素,肯定需要如下5个元素(ro ...
- 牛客网(剑指offer) 第二十一题 栈的压入、弹出序列
题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...
最新文章
- 一文览尽基于激光雷达点云(lidar)的目标检测方法
- spark重要端口号
- golang sync.Map 使用
- Windows下rsync软件配置和使用【数据同步】
- 又拍云刘平阳,理性竞争下的技术品牌提升之道
- hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))
- ParaFi Capital资产管理规模超1亿美元,至少投资22家公司或协议
- 对话CDN巨头Akamai:携手金山云,意欲何为?
- C# 电子白板软件开发
- 我的世界服务器物品定价,我的世界商店特性官方解答 创作者决定最初定价
- 计算机网络中协议分层的目的是什么意思,网络协议分层的作用是什么
- ABAQUS INP文件详解
- 微信小程序各门类需申请资质
- ACM之Java输入输出
- ARM + RISC-V双核锁步DCLS Lockstep技术总结
- Oracle 18c体系架构图创作之路 - 设计者说(精品海报大放送)
- 如何下载、使用英文期刊的LaTeX模板(以TIE为例)
- jmp 指令的几种寻址方式
- Tensorflow Win10 stage.2
- 名帖172 李建中 行书《土母帖》