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. 解决fatal: unable to access '': Failed to connect to 127.0.0.1 port 1181: Connection refused的问题

    今天把项目提交的git远程的时候遇到一个问题 fatal: unable to access '': Failed to connect to 127.0.0.1 port 1181: Connect ...

  2. Orika - 类复制工具

    Orika 前言 类复制工具有很多,比较常用的有 mapstruct.Spring BeanUtils.Apache BeanUtils.dozer 等,目前我所在的项目组中使用的是 mapstruc ...

  3. ASP .Net Core 在 CentOS8 ARM 下连接 SQL Server 2008 R2(Hypervisor)

    本文主要记录在 ARM 系统下无法连接SQL Server 2008 R2 的解决过程. 解决方案是使用 ODBC 的方式连接数据库,进行操作. 手上有公司的华为鲲鹏云计算 ARM 架构的 CentO ...

  4. CF1542E2 Abnormal Permutation Pairs (hard version)

    CF1542E2 Abnormal Permutation Pairs (hard version) good tea. 对于两个排列 \(p,q\),如果 \(p\) 的字典序小于 \(q\),则要 ...

  5. js ajax 请求

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. char*,string,char a[], const char *,之间的转换

    1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可.      EX: const char* tmp = "tsinghu ...

  7. 框架学习-MyBatis(01)

    1.MyBatis是持久层框架 什么是持久化: 狭义:把数据永久性的保存到数据当中 广义:针对于数据库的所有操作都称为持久化操作,CreateReadUpdateDelete操作 2.有哪些持久层框架 ...

  8. python飞机大战

    '''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...

  9. SpringBoot Logback 日志配置

    目录 前言 日志格式 日志输出 日志轮替 日志级别 日志分组 小结 前言 之前使用 SpringBoot 的时候,总是习惯于将日志框架切换为 Log4j2,可能是觉得比较靠谱,也可能年龄大了比较排斥新 ...

  10. Linux网络(网络模型和收发流程)

    网络模型 为了解决网络互联中异构设备的兼容性问题,并解耦复杂的网络包处理流程,国际标准化组织制定的开放式系统互联通信参考模型(Open System Interconnection Reference ...