九度OJ 1043:Day of Week(星期几) (日期计算)
来源:互联网 发布:校园网络规划设计 编辑:程序博客网 时间:2024/06/05 23:03
- 题目描述:
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 200114 October 2001
- 样例输出:
TuesdaySunday
- 提示:
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(星期几) (日期计算)
- 九度OJ-1043:Day of Week
- 九度OJ 1043 Day of Week
- 九度OJ 题目1043:Day of Week
- 【王道九度OJ】1043 day of week
- 九度OJ 题目1043:Day of Week
- 九度 oj 题目1043:Day of Week
- 题目1043:Day of Week 九度OJ
- 九度1043 Day of Week
- 九度题目1043:Day of Week
- 【九度】题目1043:Day of Week
- 九度[1043]-Day of Week
- 九度考研真题 2008-上交1043:Day of Week 日期
- 九度OJ 1096:日期差值 (日期计算)
- 九度OJ 1186:打印日期 (日期计算)
- 基姆拉尔森计算公式 (计算一个日期是星期几)
- 蔡勒(Zeller)公式:根据日期计算星期几
- 根据日期判断星期几(使用基姆拉尔森计算公式)
- openstack资源部署策略
- BroadcastReceiver
- Android按两次返回键退出程序
- 生存能力
- WPF中MainWindow与Page通信
- 九度OJ 1043:Day of Week(星期几) (日期计算)
- HDU 5505 GT and numbers(求一个数乘以它的因子得到另一个数的最小步数)
- FragmentTabHost的简单应用
- eclipse -- 工作中经常用到的快捷键(不断完善)
- 境外自然人不得以实际出资为由主张转让其在国内公司中享有的股权
- Android开发规范
- 操你妈的 太麻烦了
- linux-ht开启
- Java基础--面向对象