+ 2
according to Gregorian calendar it was Monday on the date 1/1/1990 . write a program to find out what is the day on 1st January of the given yr
3 Antworten
0
#include <iostream>
using namespace std;
int main() {
int year;
int x;
int days;
int whichDay;
cin>>year;
year -= 1900;
x= year % 4;
year /= 4;
days = year*1461+x*365;
whichDay = days%7;
switch(whichDay)
{
case 0:
cout<<"Monday";
break;
case 1:
cout<<"Tuesday";
break;
case 2:
cout<<"Wednesday";
break;
case 3:
cout<<"Thursday";
break;
case 4:
cout<<"Friday";
break;
case 5:
cout<<"Saturday";
break;
case 6:
cout<<"Sunday";
break;
}
return 0;
}
0
/* Comments Part 1
Ok so 1 1 1900 was a Monday.
In that case, that day plus seven days, it will be Monday again. So, no matter how many days we add, if we divide that number by seven, and the remainder is 0, ITS A MONDAY YAY. If the remainder is one, it's a Tuesday, two, a Wednesday,... six, a Sunday.
So when we enter a year, we need to know how many days have passed since 1900. First, how many YEARS have passed?
int year;
cin>>year;
year -= 1900
make the user input a year, then take 1900 away from that number: now 'year' contains the number of years since 1900. thus, our code won't work for years before 1900.
0
Comments Part 2
Now, a year has 365 days, but every fourth year has one more, 366, so we will check how many groups of four go into our years and multiply that number by 3*365+1*366=1461. Before, we store the remainder in int x, in case there are years left after dividing by four. Those remaining years, which can only be 0, 1, 2 or 3, are multiplied by 365, and everything is added into int days. Finally we can divide the number of days by seven and store the remainder in int whichDay.
Then, a switch statement to print the day, and everyone is happy.
NOTE: Because of leap years, this code only works for years from 1900-2099. --*/