c++ - On what days does the thirteenth occur? USACO -
is friday 13th unusual event?
that is, 13th of month land on friday less on other >day of week? answer question, write program compute frequency 13th of each month lands on sunday, monday, tuesday, >wednesday, thursday, friday, , saturday on given period of n years. >time period test january 1, 1900 december 31, 1900+n-1 >given number of years, n. n positive , not exceed 400.
here's have:
#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int main(){ ifstream fin("fridayin.txt"); ofstream fout("fridayout.txt"); int n; fin >> n; int current_year, end_year = 1900 + n - 1, current_day = 1; //set current_year, end_year, , current_day 1(monday) int daycounter[7] = { 0 }; //this record how many times day occurs on 13th int current_month = 1; int day; (current_month = 1; current_month <= 12; current_month++){ (current_year = 1900; current_year <= end_year; current_year++){ //jan 13=saturday int yp = current_year - 1900; if (current_year < 2000){ //2000 leap year day = (6 + yp + yp / 4 - yp / 100) % 7; daycounter[day]++; //increment day counter } else if (current_year > 2000){ //check if it's after 2000, if add 1 6 0 (mod 7) day = (yp + yp / 4 - yp / 100) % 7; daycounter[day]++; //increment day counter } } } int i; (i = 0; < 7; i++){ fout << daycounter[i] << ' '; } return 0; }
i'm computing january 13ths february 13ths,... december 13ths.
here's input:
20
correct output:
36 33 34 33 35 35 34
my output:
48 36 36 24 24 36 36
i think know what's wrong, since january 13th, 1900 saturday made 6 mod 7 that's not true february 13th, 1900 , other months. i'd have change equations , create if statement that'd extremely long.
here's java implementation:
package time; import java.text.dateformat; import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.linkedhashmap; import java.util.map; /** * superstition calculates how 13th falls on each day of week * @author michael * @link https://stackoverflow.com/questions/31231343/on-what-days-does-the-thirteenth-occur-usaco * @since 7/5/2015 10:31 */ public class superstition { public static final dateformat default_format = new simpledateformat("yyyy-mmm-dd"); public static final int default_max_years = 400; public static final string start_date = "1900-jan-13"; public static final int months_per_year = 12; public static void main(string[] args) { map<integer, integer> frequencies = new linkedhashmap<integer, integer>() {{ put(calendar.sunday, 0); put(calendar.monday, 0); put(calendar.tuesday, 0); put(calendar.wednesday, 0); put(calendar.thursday, 0); put(calendar.friday, 0); put(calendar.saturday, 0); }}; try { int maxyears = args.length > 0 ? integer.parseint(args[0]) : default_max_years; calendar calendar = calendar.getinstance(); calendar.settime(default_format.parse(start_date)); (int = 0; < maxyears; ++i) { (int j = 0; j < months_per_year; ++j) { int dayofweek = calendar.get(calendar.day_of_week); frequencies.put(dayofweek, (frequencies.get(dayofweek) + 1)); calendar.add(calendar.month, 1); } } } catch (parseexception e) { e.printstacktrace(); } { system.out.println(frequencies); } } }
here's output years 1900 2300:
com.intellij.rt.execution.application.appmain time.superstition {1=687, 2=685, 3=685, 4=687, 5=684, 6=688, 7=684} process finished exit code 0
as expected, frequencies 13th falls on each day of week same. sum of values equals (# years)*(12 months per year), should be.
Comments
Post a Comment