第十三次发生在什么日子? USACO [英] On what days does the thirteenth occur? USACO
问题描述
星期五是第13个真的不寻常的活动吗?
Is Friday the 13th really an unusual event?
也就是说,在任何其他>星期几?要回答这个问题,写一个程序,计算
频率,每个月的13日在星期日,星期一,星期二,>星期三,星期四,星期五和星期六在给定的N年期间。 >测试的时间段将为1900年1月1日至1900年12月31日+ N-1,对于给定年数N,N为正,不超过400.
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.
这里是我有:
#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月13ths。
I'm computing the January 13ths then the February 13ths,... December 13ths.
输入:
20
正确的输出:
36 33 34 33 35 35 34
我的输出:
48 36 36 24 24 36 36
我知道是什么问题,从1900年1月13日是一个星期六我做了6 mod 7,但这不是真的在1900年2月13日和其他几个月。
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.
推荐答案
这里是一个Java实现:
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);
}
}
}
从1900到2300:
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
正如预期的那样,13日每周的每一天的频率大致相同。值总和等于(#年)*(每年12个月),因为它应该是。
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.
这篇关于第十三次发生在什么日子? USACO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!