递归与递推递推算法是一种简单的算法,递推通过已知条件,利用特定关系得出中间推论,直至得到结果的算法。
相对于递归算法,递推算法免除了数据进出栈的过程,不需要函数不断的向边界值靠拢,而直接从边界出发,直到求出函数值。
比如阶乘函数:。
递归中的表现是这...
一眼贪心,为什么呢?
从题面中我们可以看见纵向切一刀横向就多一刀的代价,所以我们把所有代价混一起排序从大到小切割即可。
(结尾附证明)
注:要开 long long,最坏情况约为:,肯定炸 。
代码如下:
1234567891011121314151...
递归注:此算法与 函数 有关,如不了解请移步。
在 wiki pedia 中,递归的解释是这样的:
在 C++ 中,递归就是指在函数中调用函数本身;递归的作用类似于分治,将一个大问题分解为很多小问题进行求解。
但是递归的时间复杂度极高,因为要解决很多...
本来以为字符串多大,结果就这点,直接暴力。
枚举起始点,对于每个起始点枚举后面 位有没有能用的即可。
最后答案为 。
附:计算代码
枚举代码如下:
12345678910for (int i = 0; i < n; ++i) { f...
枚举和模拟是 C++ 中最为基础的算法,也是之后赛时部分分的算法首选。
枚举顾名思义,枚举就是将所有值全部扫一遍。
枚举算法的流程图如下:
我们很容易就可以写出伪代码:
12345678for (枚举区间){ 代码,例: if (条件) { 输出 ...
前言作者看着 200 行的主函数大模拟返回了 WA,陷入了沉思……
咋办?把每个模块包装一下就知道哪里出错了,所以,今天讲函数。
(太生硬了吧。。。)
定义一个函数格式:
1234返回值类型 函数名(变量 * n){ 代码}
例如:
1234int...
前言之前讲过变量,讲了数据类型(如 int 等),而结构体就相当于创造一个类型。
定义结构体首先,写上一个神圣不可侵犯的(bushi) struct。
好了,不开玩笑了。
在程序外围定义(一般写在命名空间后面)。
1234struct 名字{ 含有的...
cnblogs食用也行
题目大意有一个初始化为 的 长度为 的序列,现有 个操作,每次将区间 中的数量加 ,求如果不做某个操作将会有多少个数量为 的量。
解题思路通过将区间内所有数量增加这句话我们就能很直接的想到差分。
这题我们可以使用差...
当你想判断某个条件时,怎么办呢?
当当当当(日常发疯),if 语句就派上用场了。
使用方法不多废话,使用格式如下:
123456789101112if (条件){ 代码}else if (条件){ 代码}else{ 代码}
注:else if/el...
变量没啥好讲的,讲下类型和格式得了。
变量类型:
int: 整数类型;范围大约为 。
long long: 长整型;范围大约是 。
unsigned int/long long: 不包括负数,整数范围扩大一倍。
double: 小数类型(比 floa...