时间限制: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 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(星期几) (日期计算)的更多相关文章

  1. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  2. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  3. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  4. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  5. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  6. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  7. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  8. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  9. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. KMP算法之我见

    预备谈谈下面这些,可能有补充 KMP算法的用途: KMP算法之前的暴力: KMP算法预备知识与概念: KMP算法模板: KMP算法的习题. 1.KMP算法的用途: 主要用于模式匹配(字符串匹配).给定 ...

  2. Java 8 Comparator: 列表排序

    在本文中,我们将看到几个关于如何在Java 8中对List进行排序的示例. 1.按字母顺序排序字符串列表 List<String> cities = Arrays.asList( &quo ...

  3. 3)nginx的启动与停止、重启,linux配置对外端口

    [启动] 启动代码格式:nginx安装目录地址 -c nginx配置文件地址例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /u ...

  4. JSONModel解析Dictionary To Model /JSON To Model

    你在把字典转成object的时候还在按下面这样: self.id = [jsonDict objectForKey:@"id"]; self.name = [jsonDict ob ...

  5. Drools环境搭建

    Eclipse3.5安装Drools6.5.0.Final插件 到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html) -下载并解压Dro ...

  6. leetcode笔记:Ugly Number II

    一. 题目描写叙述 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prim ...

  7. JAVA Eclipse的Android的进程和生命周期是什么

    安卓程序的生命周期是不受自己控制的,安卓的程序根据不同的重要性做了一些区分,最重要的进程仅仅在安卓已经崩溃或者卡死的情况下才会终止前台进程.   Activity就是表现层的界面,它有三种常见的状态, ...

  8. 朴素贝叶斯分类算法-----java

    1.贝叶斯分类的基础--贝叶斯定理 已知某条件概率.怎样得到两个事件交换后的概率,也就是在已知P(A|B)的情况下怎样求得P(B|A). 这里先解释什么是条件概率: 表示事件B已经发生的前提下,事件A ...

  9. 怎样给filter加入自己定义接口

    .在Cfilter类的定义中实现Interface接口的函数的定义: //-----------------------Interface methods----------------------- ...

  10. C++成员不通过对象调用的直接调用写法

    C++成员不通过对象调用(.或->方式)的另类(C式)调用写法 #include <iostream> using namespace std; /* 我们知道,成员函数和普通函数最 ...