九度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
思路:
日期计算类的题目不少,虽然不难,但容易出错。
需要注意的地方主要是闰年的计算。
一般的年是365天,二月是28天,而闰年则366天,2月是29天。
闰年的划定标准是:400的倍数,或者4的倍数但不是100的倍数。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> #define N 10 int 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];
else
return 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;
else
count += 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: 1043
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:916 kb
****************************************************************/
最新文章
- AD域组策略-只显示指定的控制面板选项配置方法
- 大端小端系统_union_栈的增长方向
- EF MYSQL 不能选择实体框架版本
- C语言学习020:可变参数函数
- C#:屏幕显示区域问题
- Selenium2.0介绍
- 后台线程,优先级,sleep,yield
- 如何将EXCEL表导入MYSQL
- c语言10个经典小程序
- iOS开发笔记--使用blend改变图片颜色
- 《Java从入门到放弃》JavaSE篇:程序结构
- 【转】动态规划DP
- elsticsearch在kibanna中的操作
- (转)Centos下,Docker部署Yapi接口管理平台
- Android生成二维码--拍照或从相册选取图片
- Ubuntu集群 配置ntp服务
- 大数据入门第二十二天——spark(三)自定义分区、排序与查找
- go语言最快最好运用最广的web框架比较(大多数人不了解的特性)
- Python访问MySQL数据库
- MySQL数据库如何去掉数据库中重复记录
热门文章
- 解决百度ueditor配置上传目录为外部目录时,项目启动访问不到图片的问题。
- Java原子操作类,你知道多少?
- Apache Openwhisk学习(一)
- BZOJ 4011 HNOI2015 落忆枫音
- Copy Records From One Data Block To Another Data Block In Oracle Forms
- 【spring boot】application.properties官方完整文档【参考使用】
- selenium用法 (python)
- zfighting 的问题
- Performing User-Managed Database-18.7、Performing Complete User-Managed Media Recovery
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)