九度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 ...
随机推荐
- Spring Cloud Stream介绍-Spring Cloud学习第八天(非原创)
文章大纲 一.什么是Spring Cloud Stream二.Spring Cloud Stream使用介绍三.Spring Cloud Stream使用细节四.参考文章 一.什么是Spring Cl ...
- Artix : Arch拥抱OpenRC 使用笔记
轻量桌面Archlinux用户逃离systemd,拥抱Gentoo的openrc. 镜像源:官方镜像源非常慢,曾经一度体验artix后就放弃了,后来发现了清华和腾讯云的镜像,速度非常快,现在又重新安装 ...
- SecureCRT设置和Xshell一样的快速命令集(使用快捷键输入命令和密码)
编辑想要的命令 提示:想要回车直接输入[\r]
- su su- sudo
su的作用是变更为其它使用者的身份,超级用户除外,需要键入该使用者的密码. linux su 命令 建议大家切换用户的时候 使用 su - root 这样,否则可能发现某些命令执行不了 关于su .s ...
- 怎么把一个整数转化为3个十六进制字节 delphi
如何把一个整数转化为3个十六进制字节 delphi比如把整数149259(都是6位数据整型数) 转换为十六进制为2470B然后再分开为三个字节02 47 0B,求实现代码示例var ID: Integ ...
- OpenCV3.1使用SIFT
待完善... Opencv3.1.0+opencv_contrib配置及使用SIFT测试
- myeclipse执行tomcat报错Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
将myeclipse所配置的tomcat的jdk进行设置:-Xms512m -Xmx512m -XX:MaxNewSize=512m -XX:MaxPermSize=512m,例如以下图:
- Git修改IP重新定位的方法
进入已clone项目的.git文件夹,打开config文件 打开config,如图显示,修改url中的IP为192.168.6.102,然后保存 在项目上右击选择属性(R),然后选择Git,即可看到当 ...
- spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注 ...
- 分布式数据库中间件–(1) Cobar初始化过程
Cobar-Server的源代码地址:GitHub 欢迎Fork. 官方文档描写叙述Cobar的网络通信模块见下图. Cobar使用了Java的NIO进行处理读写.NIO是Java中的IO复用.而不须 ...