解题思路:
注意事项:
参考代码:
解法一:用公司算(吉姆拉尔森)
#include<iostream>
#include<string>
using namespace std;
string week[] = {"Mon","Tue","Wen","Thu","Fir","Sat","Sun"};
int y,m,d;
int main(){
while(cin>>y>>m>>d){
if(m < 3){
m += 12;
y--;
}
int w = (y + y /4 + y / 400 - y / 100 + 2 * m + 3 * (m + 1)/5 + d) % 7;
cout<<week[w]<<endl;
}
return 0;
}
解法二:(日期一遍遍前移)
#include<iostream>
using namespace std;
int monthday[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31
};
bool is_leap(int y)
{
return y%4 == 0 && y%100 != 0 || y%400 == 0;
}
int daysOfMonth(int year,int month)
{
if(month == 2){
return 28 + is_leap(year);
}
else return monthday[month];
}
int main()
{
int y,m,d;
cin >> y >> m >> d;
int year = 2011,month = 11,day = 11,
week = 5;
while(year != y || month != m || day != d)
{
day ++;
if(day > daysOfMonth(year,month)){
day = 1;
month ++;
}
if(month == 13){
month = 1;
year ++;
}
week ++;
if(week == 8){
week = 1;
}
}
cout << week << endl;
return 0;
}
0.0分
0 人评分
破解简单密码 (C语言代码)浏览:1866 |
简单的a+b (C语言代码)浏览:783 |
C语言训练-求素数问题 (C语言代码)浏览:1509 |
C语言训练-求素数问题 (C语言代码)浏览:773 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:781 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:949 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:732 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)浏览:822 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:512 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:769 |