排序
【C】对一个分数约分
int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } void simplifyFraction(int *a, int *b) { int divisor = gcd(*...
【算法】【Python】使用动态规划(DP)解决最长公共子序列(LCS)问题
使用动态规划计算 LCS 长度后,从dp[m][n]回溯构造 LCS 字符串:若text1[i-1] == text2[j-1],加入 LCS 并向左上移动,否则向dp值较大的方向移动。最终反转 LCS 输出。时间复杂度 O(m×n)。
【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...
【C】时间差计算(表达式:运算符和算子,取余运算)
时间差计算代码 //计算时间差 #include<stdio.h> int main() { int hour1,minute1,hour2,minute2; printf('请输入第一个时间\n'); scanf('%d %d', &hour1,&minute1); printf('请输入...
【递归】2的幂
递归法: class Solution: def isPowerOfTwo(self, n: int) -> bool: if n==1: return True if n<=0 or n%2!=0: return Fals...
【C】五舍六入?
如果题目让你五舍六入,那么就没办法使用C++里的round函数了,只能选择一个通用的方法,示例如下: #include <stdio.h> #define PI 3.1415926 int main(){ double r,ans; scanf('%lf',&r...