九度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 ...
随机推荐
- Sharing Cookies --AtCoder
题目描述 Snuke is giving cookies to his three goats.He has two cookie tins. One contains A cookies, and ...
- Unix/Linux提权漏洞快速检测工具unix-privesc-check
Unix/Linux提权漏洞快速检测工具unix-privesc-check unix-privesc-check是Kali Linux自带的一款提权漏洞检测工具.它是一个Shell文件,可以检测 ...
- 构建更好的HashMap
在7月份的那期 Java理论与实践(“并发集合类”)中,我们简单地回顾了可伸缩性的瓶颈,并讨论了怎么用共享数据结构的方法获得更高的并发性和吞吐量.有时候学习的最好方法是分析专家的成果,所以这个月我们将 ...
- BeanFactory和ApplicationContext的异同
相同: Spring提供了两种不同的IOC 容器,一个是BeanFactory,另外一个是ApplicationContext,它们都是Java interface,ApplicationContex ...
- NOI模拟题5 Problem A: 开场题
Solution 注意到\(\gcd\)具有结合律: \[ \gcd(a, b, c) = \gcd(a, \gcd(b, c)) \] 因此我们从后往前, 对于每个位置\(L\), 找到每一段不同的 ...
- Drawable 添加过滤色,改变图片颜色
/** * 更改图片颜色 * @param drawable * @param color * @return */ public Drawable getDrawable(Drawable draw ...
- git错误解决 -- 小结
1.今天 当我 执行 Git add somefile 的时候,出现 如下 错误: If no other git process is currently running, this prob ...
- Codis的了解和操作
1.Codis的基本架构 2.Codis各组件 Codis-server:就是redis服务,可以使用codis修改的reids和原生的redis Codis-proxy:客户端连接的代理服务,客户端 ...
- linux中du的用法
du:Disk Usage的缩写,命令功能为显示目录(或文件)所占磁盘空间的大小. 语 法:du [-abcDhHklmsSx0] [-L][-X File][--block-size=SIZE][- ...
- UE把环境变量Path改了
为了比较个文件,装了UE. 文件比较完了,环境变量也被改了. 改还不是写添加式的改,是写覆盖式的改. 搞得ant都起不动了,一看Path被改的那样(C:\hy\soft\ultraedit\Ultra ...