算法共21篇
令人头疼的算法
【深进1.例1】求区间和 - Python AI C++笔记 - 小竹の笔记本

【深进1.例1】求区间和

题目描述 给定 $n$ 个正整数组成的数列 $a_1, a_2, \cdots, a_n$ 和 $m$ 个区间 $[l_i,r_i]$,分别求这 $m$ 个区间的区间和。 对于所有测试数据,$n,m\le10^5,a_i\le 10^4$ 输入格式 第一行,为...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo10个月前
0859
【差分与前缀和】Python模板 - Python AI C++笔记 - 小竹の笔记本

【差分与前缀和】Python模板

N, Q = map(int,input().split()) nlist = list(map(int, input().split())) nlist.insert(0, 0) cf = [0 for _ in range(N+1)] print(nlist) for _ in range(Q):   l,r,x = map(int, input()....
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo10个月前
09210
【并查集】Python模板 - Python AI C++笔记 - 小竹の笔记本

【并查集】Python模板

题目描述 如题,现在有一个并查集,你需要完成合并和查询操作。 输入格式 第一行包含两个整数 N,M ,表示共有 N 个元素和 M 个操作。 接下来 M 行,每行包含三个整数 Zi,Xi,Yi 。 当 Zi=1 时,将...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo10个月前
0815
【DP】使用最小花费爬楼梯 - Python AI C++笔记 - 小竹の笔记本

【DP】使用最小花费爬楼梯

题目 给你一个整数数组 cost ,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。 你可以选择从下标为 0 或下标为 1 的台阶开始爬楼...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo11个月前
09210
【模拟】回文日期 - Python AI C++笔记 - 小竹の笔记本

【模拟】回文日期

题目 我的代码 import datetime ipt = input() begin = datetime.datetime(int(ipt[0:4]), int(ipt[4:6]), int(ipt[6:8])) flag1=0 flag2=0 while True:  if flag1==1 and flag2==1:    brea...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo11个月前
0867
【递归】快速幂 - Python AI C++笔记 - 小竹の笔记本

【递归】快速幂

class Solution:    def myPow(self, x: float, n: int) -> float:        if n==0:            return 1        if n==1:            return x        if n==-1:  ...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo11个月前
0789
【递归】2的幂 - Python AI C++笔记 - 小竹の笔记本

【递归】2的幂

递归法: class Solution:    def isPowerOfTwo(self, n: int) -> bool:        if n==1:            return True        if n<=0 or n%2!=0:            return Fals...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo11个月前
08411
【递归】斐波那契数 - Python AI C++笔记 - 小竹の笔记本

【递归】斐波那契数

#include<iostream> using namespace std; int fib(int n){ if (n<2){ return n; } return fib(n-1) + fib(n-2); } int main(){ int n; cin >> n; cout << fib(n); return 0; } 数...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo12个月前
08115
【模拟】旋转矩阵 - Python AI C++笔记 - 小竹の笔记本

【模拟】旋转矩阵

#include<stdio.h> int main(){ int n; scanf('%d',&n); int a[n][n]; int top=0,down=n-1,left=0,right=n-1,count=1; while(count<=n*n){ for (int i=left;i<=right;i++){ a[top]...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo12个月前
05110
【模拟】各位相加 - Python AI C++笔记 - 小竹の笔记本

【模拟】各位相加

#include<iostream> using namespace std; int main(){ int num; cin >> num; while(num>9){ int total=0; while(num!=0){ total+=num%10; num/=10; } num = total; total = 0; } cout <&...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo12个月前
0766
【模拟】二进制求和 - Python AI C++笔记 - 小竹の笔记本

【模拟】二进制求和

#include <iostream> using namespace std; string addBinary(string a, string b){ string res; int carry = 0;  // 进位 int i = a.size() - 1; int j = b.size() - 1; while(i >= 0 || j...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo12个月前
06714
【C++】高精度减法 - Python AI C++笔记 - 小竹の笔记本

【C++】高精度减法

#include <iostream> using namespace std; int main(){ string s1,s2; cin >> s1; cin >> s2; int a1[210]={0}; int a2[210]={0}; int a3[210]={0}; char flag='+'; if (s1.size()<s2.si...
SmallBamboo的头像 - Python AI C++笔记 - 小竹の笔记本SmallBamboo1年前
05613