Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - On what days does the thirteenth occur? USACO

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other >day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, >Wednesday, Thursday, Friday, and Saturday over a given period of N years. The >time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a >given number of years, N. N is positive and will not exceed 400.

Here's what I 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, and current_day to 1(Monday)
    int daycounter[7] = { 0 };  //this will record how many times a day occurs on the 13th
    int current_month = 1;
    int day;
    for (current_month = 1; current_month <= 12; current_month++){
        for (current_year = 1900; current_year <= end_year; current_year++){    //jan 13=saturday
            int yp = current_year - 1900;
            if (current_year < 2000){   //2000 is a leap year 
                day = (6 + yp + yp / 4 - yp / 100) % 7;
                daycounter[day]++;  //increment the day counter
            }
            else if (current_year > 2000){  //check if it's after 2000, if it is add 1 to 6 to get 0 (mod 7)
                day = (yp + yp / 4 - yp / 100) % 7;
                daycounter[day]++;  //increment the day counter
            }
        }
    }
    int i;
    for (i = 0; i < 7; i++){
        fout << daycounter[i] << ' ';
    }


    return 0;
}

I'm computing the January 13ths then the 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 I know what's wrong, since January 13th, 1900 is a Saturday I made it 6 mod 7 but that's not true for February 13th, 1900 and the other months. I'd have to change the equations and create an if statement but that'd be extremely long.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's a 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 frequently the 13th falls on each day of the week
 * @author Michael
 * @link https://stackoverflow.com/questions/31231343/on-what-days-does-the-thirteenth-occur-usaco
 * @since 7/5/2015 10:31 AM
 */
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));
            for (int i = 0; i < maxYears; ++i) {
                for (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();
         } finally {
            System.out.println(frequencies);
        }
    }
}

Here's the output for the years from 1900 to 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 with exit code 0

As expected, the frequencies with which the 13th falls on each day of the week are roughly the same. Sum of values equals (# years)*(12 months per year), as it should be.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...