Content

输入两个数 \(m,d\),请输出 \(2017\) 年 \(m\) 月的日历【其中第一天是星期 \(d\)(如果 \(d=7\) 就是星期天)】需要印的列数。

格式见题目所述。

数据范围:\(1\leqslant m\leqslant 12,1\leqslant d\leqslant 7\)。

Solution

你们怎么都这么麻烦啊,什么循环啊,什么讨论 \(4,5,6\) 啊,这题目不很简单就完事了吗?

首先我们可以看到,这里不需要考虑闰年,所以,我们把所有月份的天数通通用一个数组保存下来:

const int mo[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

那么这样的话就可以直接调用每个月份的天数了。那么第 \(m\) 月的天数就是 \(mo_m\)。

我们又可以通过自己模拟发现,前面的一列仅有 \(7-d+1\) 天数,所以我们通过先减去第一列得到剩下的天数,也就是 \(mo_m-(7-d+1)\)。

又因为后面每列有 \(7\) 天,所以我们后面需要的列数是 \(\left\lceil\dfrac{mo_m-(7-d+1)}{7}\right\rceil\),注意因为最后的一列不一定都是 \(7\) 天,所以我们需要向上取整。

所以,答案就是 \(\left\lceil\dfrac{mo_m-(7-d+1)}{7}\right\rceil+1\)。

然而,既然这道题目的范围是如此之小,我们为什么不用打表做呢?很简单,就以生成答案的程序为 generator,直接生成出所有在这个数据范围里面的答案,然后直接输出结果就好了。

Code

#1

#include <cstdio>
#include <cmath>
using namespace std; int n, m;
const int mo[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() {
scanf("%d%d", &n, &m);
printf("%d", (int)(ceil((mo[n] - (7 - m + 1)) / 7.0)) + 1);
}

#2

Generator

#include <cstdio>
#include <cmath>
using namespace std; int n, m;
const int mo[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() {
printf("int ans[13][8] = {{0, 0, 0, 0, 0, 0, 0, 0}");
for(int i = 1; i <= 12; ++i) {
printf(", {0, ");
for(int j = 1; j <= 7; ++j) {
printf("%d", (int)(ceil((mo[i] - (7 - j + 1)) / 7.0)) + 1);
if(j != 7) printf(", ");
}
printf("}");
}
return printf("};"), 0;
}

Answer

#include <cstdio>
using namespace std; int ans[13][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 5, 5, 5, 5, 5, 6, 6}, {0, 4, 5, 5, 5, 5, 5, 5}, {0, 5, 5, 5, 5, 5, 6, 6}, {0, 5, 5, 5, 5, 5, 5, 6}, {0, 5, 5, 5, 5, 5, 6, 6}, {0, 5, 5, 5, 5, 5, 5, 6}, {0, 5, 5, 5, 5, 5, 6, 6}, {0, 5, 5, 5, 5, 5, 6, 6}, {0, 5, 5, 5, 5, 5, 5, 6}, {0, 5, 5, 5, 5, 5, 6, 6}, {0, 5, 5, 5, 5, 5, 5, 6}, {0, 5, 5, 5, 5, 5, 6, 6}}; int n, m; int main() {
scanf("%d%d", &n, &m);
return printf("%d", ans[n][m]), 0;
}

CF760A Petr and a calendar 题解的更多相关文章

  1. 【codeforces 760A】Petr and a calendar

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. Codeforces 760A Petr and a calendar

    题目链接:http://codeforces.com/problemset/problem/760/A 题意:日历需要多少列. #include <bits/stdc++.h> using ...

  3. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. CodeForces760A

    A. Petr and a calendar time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  5. Codeforces Round #393 (Div. 2)

    A. Petr and a calendar time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  6. CF1097B Petr and a Combination Lock 题解

    Content 有一个锁,它只有指针再次指到 \(0\) 刻度处才可以开锁(起始状态如图所示,一圈 \(360\) 度). 以下给出 \(n\) 个操作及每次转动度数,如果可以通过逆时针或顺时针再次转 ...

  7. [LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 题解

    题目描述 MyCalendar主要实现一个功能就是插入指定起始结束时间的事件,对于重合的次数有要求. MyCalendar I要求任意两个事件不能有重叠的部分,如果插入这个事件会导致重合,则插入失败, ...

  8. LightOJ 1393 Crazy Calendar(博弈)题解

    题意:r*c方格中,每个格子有一定石子,每次移动每格任意数量石子,只能向下或者向右动一格,不能移动为败 思路:显然是Nim,到右下曼哈顿距离为偶数的不用管,因为先手动一下后手动一下最后移到右下后还是先 ...

  9. CF139A Petr and Book 题解

    Content 小 P 有一本 \(n\) 页的书,现给出他一周七天每天的阅读页数,求它在星期几读完这本书. 数据范围:\(1\leqslant n\leqslant 1000\). Solution ...

随机推荐

  1. 实用QPS和TPS高的高效分析方法

    现在主库的MySQL的QPS一直在3K/s左右,想知道其到底执行了那些SQL,或者是那些SQL执行的次数比较多: 腾讯云的后台监控: 开启腾讯云的SQL审计后,下载几分钟SQL日志文件, 下列语句在M ...

  2. Java 插入html字符串到PPT幻灯片

    通过Java后端代码操作PPT幻灯片时,可直接在幻灯片中绘制形状,并在形状中添加文本字符串内容.本篇文章,介绍一种通过html字符串来添加内容到PPT幻灯片的的方法,可添加文字.图片.视频.音频等.下 ...

  3. Codeforces 1413F - Roads and Ramen(树的直径+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...

  4. Peaks Gym 100365H

    Peaks ( Gym 100365H ) 这题nk做法还挺正常的..后面那个循环就很恶心了 考虑 dp[i][j] 表示长度为i的排列,恰好有k个峰的方案数量. 然后转移就是把 i 插入 i-1 的 ...

  5. 在windows下使用shell,运行shell脚本

    在Windows操作系统下运行Shell脚本,缺少的只是一个Git软件.其下载路径为Git - Downloading Package. 安装之后,将安装路径下的bin文件夹的路径作为环境变量.于是我 ...

  6. javaSE高级篇5 — java8新特性详解———更新完毕

    java8新特性 在前面已经见过一些东西了,但是:挖得有坑儿 1.lambda表达式 lambda表达式是jdk1.8引入的全新语法特性 它支持的是:只有单个抽象方法的函数式接口.什么意思? 就是说: ...

  7. 学习java的第十一天

    一.今日收获 1.学习java完全学习手册2.9.3循环结构的内容并验证例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.基本没有 三.明日目标 1.继续完成2.9.3循环结构的例题 2.哔哩哔哩 ...

  8. 日常Java 2021/10/17

    今天开始Javaweb编译环境调试,从tomcat容器开始,然后mysql的下载,连接工具datagrip,navicat for mysql,然后就是编写自己的sql,安装jdbc,eclipse连 ...

  9. A Child's History of England.25

    It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...

  10. day30线程(Threads)

    day30线程(Threads) 1.开启线程 一.什么是线程: 1.进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程. 2.主进程中的线程称为主线程,其他开启的线程称 ...