im having trouble to find good approach for this problem.
i need to find what day of date in calendar
the problem is there are two type of year which are ordinary year and leap year.
for example if in one year there are 365 days, and in one month there are 50 days, there are 365/50 = 7 month in a year, and remainder of 365 % 50 = 15 days in that year, the rule to count leap year is every remainder of day in a year, we have to add it to next year, so leap year will happen if the days is greater than days in month of the year such as 15+15+15+15 = 60 > 50(daysinmonth) , so leap year will happen at fourth year, which means in the fourth year there will be 8 months and the remainder is 60 days-50days =10 days will be added to fifth year to count for the next leap year again, from here we also know that the next leap year will be in 10+15+15+15 > 50 which is 3 years after fourth year, so the next leap year will happen at seventh years.
my task is to find what is the day in the date 0001-01-01 means what is the day of the first day of the first month of the first year.
im not sure how to approach this problem, but the solution that i can think although im not sure correct or not is to change the date to total days first and substract it by days in year , and also detect the leap year, substract until total days is smaller than days in a year and find the day by divide it with total days in a week?
is there a better solution?
input rule:
daysinyear daysinmonth daysinweek date
1 <= daysinyear <= 2000
1 <= daysinmonth <= 200
1 <= daysinweek <= 26
all month and days no more than 2 digit. so month or days that is 3 digit, is not applicable.
the days in calendar are A-Z , but will be reset according to how many days in week, if there is 5 days in a week, then A,B,C,D,E and the 6th day will be A again
input example :
daysinyear daysinmonth daysinweek date
100 10 10 0001-01-01
this means there is 100 days in 1 year, 10 days in 1 month, and 10 days in a week,
date format :YYYY-MM-DD
output: A
input : 100 10 10 0001-01-11
output: -1 (because 11 > 10)
input : 100 10 10 0001-01-10
output: J
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
char arr[]={'A', 'B', 'C', 'D', 'E', 'F', 'G','H', 'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int main(int argc, char *argv[]) {
int daysinyear,daysinmonth,dayinweek;
cin>>daysinyear;
cin>>daysinmonth;
cin>>dayinweek;
cin>>str;
int flag=false;
vector<int>month;
std::replace(str.begin(),str.end(),'-',' ');
std::stringstream s(str);
int temp;
while(s>>temp)
month.push_back(temp); // to push year, month, date in month vector
int totalmonth=daysinyear/daysinmonth;
int rem=daysinyear%daysinmonth;
//from month vector, to get year,month,date
int year=month[0];
int mont=month[1];
int day=month[2];
if(daysinyear>2000 || daysinmonth>200||day > daysinmonth) //to make sure date is not wrong
{
flag=true;
cout<<-1;
return 0;
}
int yearcount=0;
int daysremainder=0;
int remaindertonextyear=0;
int totalday=((year-1)*daysinyear)+((mont-1)*daysinmonth)+day;
//count how many days in the given dte
while(totalday)
{
if(daysinmonth< (daysremainder*yearcount)+remaindertonextyear){ // for leap year
if(totalday-(totalmonth*daysinmonth)>0) //check to make sure totalday not negative
{
totalday-=(totalmonth+1)*daysinmonth; //substract with total days in a year
remaindertonextyear=(daysremainder*yearcount)-daysinmonth;
//find the remainder to sum for the next year
yearcount++;
yearcount=0; //reset year
}else{
int realday=((totalday-1)%daysinmonth)%dayinweek; //find what day from array,
cout<<arr[realday];
return 0;
}
}else{ //for not leap year
daysremainder=daysinyear%daysinmonth;
if(totalday-(totalmonth*daysinmonth)>0){
totalday-=(totalmonth*daysinmonth);
yearcount++;
}else{
int realday=((totalday-1)%daysinmonth)%dayinweek; //minus 1 because array 0-25
cout<<arr[realday];
return 0;
}
}
}
return 0;
}
|
this is the only solution that i can think of and at the same time i dont know is it right or wrong for all cases too.