第十三天是几天?USACO

On what days does the thirteenth occur? USACO

本文关键字:几天 USACO 十三 三天      更新时间:2023-10-16

13号星期五真的是一个不同寻常的事件吗?

也就是说,一个月的13号在周五降落的频率是否比一周中的任何其他>天都低?要回答这个问题,请编写一个程序来计算在给定的N年内,每个月的13日在周日、周一、周二、>周三、周四、周五和周六着陆的频率。测试的>时间段将从1900年1月1日至1900年12月31日+N-1,持续>给定的年份,N.N为阳性,不会超过400。

这是我的:

#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;
}

我计算的是1月13日,然后是2月13日,。。。12月13日。

输入如下:

20

正确输出:

36 33 34 33 35 35 34

我的输出:

48 36 36 24 24 36 36 

我想我知道怎么了,因为1900年1月13日是一个星期六,我把它定为6月7日,但1900年2月13日和其他月份的情况并非如此。我必须改变方程并创建一个if语句,但这将是非常长的。

下面是一个Java实现:

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);
        }
    }
}

以下是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 with exit code 0

正如预期的那样,13日在一周中每一天的频率大致相同。值的总和等于(#年(*(每年12个月(,应该是这样。