九度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
****************************************************************/
九度OJ 1043:Day of Week(星期几) (日期计算)的更多相关文章
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
随机推荐
- Fennec VS. Snuke --AtCoder
题目描述 Fennec and Snuke are playing a board game.On the board, there are N cells numbered 1 through N, ...
- POJ 2406 Power Strings KMP算法之next数组的应用
题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...
- BZOJ 1188 [HNOI2007]分裂游戏
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1188 学习SG函数的过程中,我先看了一篇叫做 <2008-贾志豪-组合数学略述... ...
- DedeCMS 列表页调用图集内容多张图片的方法
新做一个以图片为主的网站,采用的DEDECMS图集,列表页要求直接调内容面的大图,解决方法如下:(主要是采用php的正则匹配函数preg_match_all函数来巩固复习下该函数:preg_match ...
- Can''t find the channel handler for deviceType 工行 个人网银 错误
背景描述:系统Win7,浏览器IE8.登录工商银行个人网银的时候,输入帐号密码和验证码后,出现空白页面,上面一句话 Can''t find the channel handler for devic ...
- 在线更新dede程序后 网站出现错误 DedeCMS Error:Tag disabled:"php" more...!
dedecms出现DedeCMS Error:Tag disabled:php原因解决 ------------------------------------------------------- ...
- Hibernate get load的区别
这两个函数都是用来从数据库中加载对象,其区别说起来主要有以下两点: 1.如果数据库中不存在该对象,那么load是抛出一个ObjectNotFound的异常,而get是返回一个空指针 2.加载机制不同 ...
- 2016.7.14 去掉Mybatis Generator生成的一堆 example
参考资料: http://www.cnblogs.com/zerocold/p/4220955.html mybatis generator自动生成的代码里老是有一堆example,需要改的时候, ...
- Maven项目如何将自定义文件添加到META-INF目录下
Maven项目如何将自定义文件添加到META-INF目录下 学习了:https://blog.csdn.net/yangjiegreat/article/details/78698655 <bu ...
- Arduino MEGA 2560找不到驱动怎么办
刚买了Arduino MEGA 2560(比Arduino UNO稍微高级一点的板子),按照视频一步一步操作(似乎插板子也不太一样,不管他,能插上去就完事了),但是到了代码烧录的时候,点击Tools- ...