今天是2012年4月12日星期四,编写程序,输入今天开始到12月31日之间的任意日期,输出那一天是星期几。如: 输入“5 20”即5月20日,输出应该为sunday.
代码:
#include <cstdio>
#include <string>
using namespace std;
int days[] = { 0,30,31,30,31,31,30,31,30,31 };//4月-12月每月的天数
int sum_day(int m, int d) {//计算输入日期到4月12的天数int sum = 0;for (int i = 0; i <= m - 4; ++i) {sum += days[i];}sum += d;sum -= 12;return sum;
}
int main() {string week[] = { "Sunday","Monday","Tuesday","wednesday","Thursday","Friday","Saturday" };int cur = 4;int m, d;scanf_s("%d%d", &m, &d);cur += sum_day(m, d);cur = cur % 7;//printf("%d\n", cur);printf("%s\n", week[cur]);
}
运行结果: