第十三天是几天?USACO
On what days does the thirteenth occur? USACO
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个月(,应该是这样。
相关文章:
- 为什么我的代码在输出中增加了93天
- 在C++中,如何通过几种类型从元组中选择多个元素
- Usaco第1.6节主要回文
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 了解每月第一天函数的代码
- 为我的 c++ 类介绍制作一个三角形分类器.我有几个问题
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 通过字符串来计算长度需要更长的时间,而不是移动字符串几次?
- 如何返回或护理项目清单基于几个类别
- 以天C++为单位的两个时间戳之间的差异
- C++ Chrono 确定一天是否是周末?
- 编写了一个C++代码来检查表达式是否具有平衡括号并且我的代码未运行.我已经卡了一天了
- C++从字符串中检索几行
- 我们如何在文本文件中找到C++中的几个单词?
- 如何递归地解决USACO 2013-周长-银
- 我如何编写一个告诉星期几的程序?(泽勒全等)
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 将几秒钟转换为几天,小时,分钟,秒格式(C )
- C++ 在一周中的几天内增加工作时间的公式
- 第十三天是几天?USACO