九度OJ 1043:Day of Week(星期几) (日期计算)

2024-04-02 02:38

本文主要是介绍九度OJ 1043:Day of Week(星期几) (日期计算),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5349

解决:1923

题目描述:

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入:

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出:

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

样例输入:
9 October 2001
14 October 2001
样例输出:
Tuesday
Sunday
提示:

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

来源:
2008年上海交通大学计算机研究生机试真题

思路:

日期计算类的题目不少,虽然不难,但容易出错。

需要注意的地方主要是闰年的计算。

一般的年是365天,二月是28天,而闰年则366天,2月是29天。

闰年的划定标准是:400的倍数,或者4的倍数但不是100的倍数。


代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define N 10int compare(int y[2], int m[2], int d[2])
{if (y[0] != y[1])return y[0]-y[1];else if (m[0] != m[1])return m[0]-m[1];else if (d[0] != d[1])return d[0]-d[1];elsereturn 0;
}void swap(int a[2])
{int tmp;tmp = a[0];a[0] = a[1];a[1] = tmp;
}int days(int y, int m, int d)
{int count = 0;//printf("y=%d, m=%d, d=%d\n", y, m, d);count += y*365;count += (y-1)/4+1;count -= (y-1)/100+1;count += (y-1)/400+1;//printf("count=%d\n", count);if (m > 1)count += 31;if (m > 2){if ((y%4 == 0 && y%100 != 0) || y%400 == 0)count += 29;elsecount += 28;}if (m > 3)count += 31;if (m > 4)count += 30;if (m > 5)count += 31;if (m > 6)count += 30;if (m > 7)count += 31;if (m > 8)count += 31;if (m > 9)count += 30;if (m > 10)count += 31;if (m > 11)count += 30;if (m > 12)count += 31;//printf("count=%d\n", count);count += d;//printf("count=%d\n", count);return count;
}int month(char s[])
{char a[12][20] = {"January", "February", "March", "April","May", "June", "July", "August","September", "October", "November", "December"};int i;for (i=0; i<12; i++){if (strcmp(s, a[i]) == 0)break;}   return i+1;
}   void pweek(int w1)
{   char s[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};printf("%s\n", s[w1]);
}   int main(void)
{       char s[N];int y[2], m[2], d[2], w[2];y[0] = 2001; m[0] = 10;d[0] = 9; w[0] = 2;while (scanf("%d%s%d", &d[1], s, &y[1]) != EOF){m[1] = month(s);w[1] = w[0];int cmp = compare(y, m, d);if (cmp < 0){w[1] = (w[0] + days(y[1], m[1], d[1])- days(y[0], m[0], d[0])) % 7;}else if (cmp > 0){w[1] = (w[0] + 7 - (days(y[0], m[0], d[0])- days(y[1], m[1], d[1])) % 7) % 7;}           pweek(w[1]);}return 0;
}       
/**************************************************************Problem: 1043User: liangrx06Language: CResult: AcceptedTime:0 msMemory:916 kb
****************************************************************/


这篇关于九度OJ 1043:Day of Week(星期几) (日期计算)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/868752

相关文章

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

Python中经纬度距离计算的实现方式

《Python中经纬度距离计算的实现方式》文章介绍Python中计算经纬度距离的方法及中国加密坐标系转换工具,主要方法包括geopy(Vincenty/Karney)、Haversine、pyproj... 目录一、基本方法1. 使用geopy库(推荐)2. 手动实现 Haversine 公式3. 使用py

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java日期类详解(最新推荐)

《Java日期类详解(最新推荐)》早期版本主要使用java.util.Date、java.util.Calendar等类,Java8及以后引入了新的日期和时间API(JSR310),包含在ja... 目录旧的日期时间API新的日期时间 API(Java 8+)获取时间戳时间计算与其他日期时间类型的转换Dur

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

Python日期和时间完全指南与实战

《Python日期和时间完全指南与实战》在软件开发领域,‌日期时间处理‌是贯穿系统设计全生命周期的重要基础能力,本文将深入解析Python日期时间的‌七大核心模块‌,通过‌企业级代码案例‌揭示最佳实践... 目录一、背景与核心价值二、核心模块详解与实战2.1 datetime模块四剑客2.2 时区处理黄金法

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti