算法共49篇
令人头疼的算法,算法竞赛冲啊
【算法】【Python】二维差分数组与其前缀和(洛谷P3397 地毯) - AI科研 编程 读书笔记 - 小竹の笔记本

【算法】【Python】二维差分数组与其前缀和(洛谷P3397 地毯)

本文讨论了洛谷P3397题“地毯覆盖计数”的两种解法。作者最初用C语言暴力模拟遍历每个地毯覆盖的矩形区域,逐个累加计数,虽通过测试但效率较低。针对大规模数据(n、m≤1000),提出基于二维差...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo3个月前
08815
【C】求整数所有因子 - AI科研 编程 读书笔记 - 小竹の笔记本

【C】求整数所有因子

#include <stdio.h> int main(){    int n;    scanf('%d',&n); printf('%d=',n); //默认使用2开始计算    for (int i=2;i<=n;i++){   //直到n不能被整除        while (n...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo2年前
08415
【算法】【Python】N皇后问题的三种解法 - AI科研 编程 读书笔记 - 小竹の笔记本

【算法】【Python】N皇后问题的三种解法

本文分析了N皇后问题的三种解法:回溯法通过逐行尝试并安全检测实现,时间复杂度O(n!)但实现简单;分支限界法利用列和对角线标记数组将安全判断优化至O(1),通过空间换时间显著提升速度;位运算...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo2个月前
010415
【C】判断回文数 - AI科研 编程 读书笔记 - 小竹の笔记本

【C】判断回文数

#include <stdio.h> bool fun(int n) { int a = 0; int num = n; while (n > 0) { a = a * 10 + n % 10; n = n / 10; } if (a==num) { return true; } else { return false; } } int main() ...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo2年前
09015
【C】保留几位有效数字 - AI科研 编程 读书笔记 - 小竹の笔记本

【C】保留几位有效数字

使用%.6g!! 例子 #include<stdio.h> double getBMI(float m,float h){ return (m/(h*h)); } int main(){ double m,h; scanf('%lf %lf',&m,&h); float BMI=getBMI(m,h); if (BMI<...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo2年前
010215
【C】使用函数递归实现顺序数组二分查找 - AI科研 编程 读书笔记 - 小竹の笔记本

【C】使用函数递归实现顺序数组二分查找

#include <stdio.h> int BinarySearch(int r[],int low,int high,int k){ int mid; if (low>high){ //若找不到,则返回-1,这也是递归终止条件 return -1; } else{ mid=(low+high)/2; //在顺...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo1年前
016915
【深基2.习6】Apples Prologue / 苹果和虫子 - AI科研 编程 读书笔记 - 小竹の笔记本

【深基2.习6】Apples Prologue / 苹果和虫子

题目描述 八尾勇喜欢吃苹果。她现在有m(1<=m<=100)个苹果,吃完一个苹果需要花费 t(0<=t<=100)分钟,吃完一个后立刻开始吃下一个。现在时间过去了s(1<=s<=10000)分钟,...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo2年前
08515
【递归】斐波那契数 - AI科研 编程 读书笔记 - 小竹の笔记本

【递归】斐波那契数

#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的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo1年前
014615
【模拟】二进制求和 - AI科研 编程 读书笔记 - 小竹の笔记本

【模拟】二进制求和

#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的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo1年前
013714
【C++】过河卒问题 - AI科研 编程 读书笔记 - 小竹の笔记本

【C++】过河卒问题

无马情况 #include <iostream> // 定义棋盘大小 #define MAXSIZE 30 using namespace std; int main(){ int a[MAXSIZE][MAXSIZE]={0}; int n,m; // n,m为B点的坐标,A点默认为(0,0) cin >> n...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo1年前
015314
【C】五舍六入? - AI科研 编程 读书笔记 - 小竹の笔记本

【C】五舍六入?

如果题目让你五舍六入,那么就没办法使用C++里的round函数了,只能选择一个通用的方法,示例如下: #include <stdio.h> #define PI 3.1415926 int main(){ double r,ans; scanf('%lf',&r...
SmallBamboo的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo2年前
014413
【C++】高精度减法 - AI科研 编程 读书笔记 - 小竹の笔记本

【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的头像 - AI科研 编程 读书笔记 - 小竹の笔记本SmallBamboo1年前
013013