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. Python学习手册——第二部分 类型和运算(1)之字符串

    Python全景 1.程序由模块构成. 2.模块包含语句. 3.语句包含表达式. 4.表达式建立并处理对象. 在python中数据是以对象的形式出现的!!! 为什么使用内置类型 内置对象使程序更容易编 ...

  2. excel的表格数据插入到数据库

    看到有excel保存insert的数据,自己照着教程弄了一下,可以的. 表格数据 接下来我们在d3的位置输入 =CONCATENATE("insert into user(code, nam ...

  3. HTML四种定位-固定定位

    固定定位 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset=&q ...

  4. Go语言核心36讲(Go语言实战与应用十八)--学习笔记

    40 | io包中的接口和工具 (上) 我们在前几篇文章中,主要讨论了strings.Builder.strings.Reader和bytes.Buffer这三个数据类型. 知识回顾 还记得吗?当时我 ...

  5. python-面向过程、面向对象、类

    目录 python-面向过程.面向对象.类 面向过程编程 面向对象 类和对象 二者区别 定义格式: 定义类发生的事情 定义类的标准格式 属性的查找顺序 python-面向过程.面向对象.类 面向过程编 ...

  6. 【Pathview web】通路映射可视化

    前言 pathview是一个通路可视化友好的R包,最主要的是它支持多组学数据映射(基因/蛋白-代谢).自己用过它的R包,后来发现有网页版的,果断介绍给学员.因为不常用,记录要点,以后温习备用. 目前w ...

  7. adjust, administer

    adjust to just, exact. In measurement technology and metrology [度量衡学], calibration [校准] is the compa ...

  8. HTTP请求 Java API

    1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...

  9. 【leetcode】153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example ...

  10. STM32 部分重映射和完全重映射(查看数据手册)

    数据手册如何查找对应的映射: 打开官网直接搜索STM32F可以看到数据手册,里面有关于重映射的表格,输入第6页的页码,点击9.3中的9.3x可打开对应的链接.  举例说明: STM32中拥有重映射功能 ...