排序
【C++】四舍五入
#include <iostream> #include <cmath> using namespace std; float a[100000]; int main(){ int n; cin >> n; for (int i=0;i<n;i++){ cin >> a[i]; } for (int i=0;i<n;i++){ cou...
【模拟】回文日期
题目 我的代码 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...
【算法】【Python】邻接表和迪杰斯特拉Dijkstra算法求解单源最短路径问题
Dijkstra算法求解单源最短路径问题,适用于带权有向图。算法以节点1为起点,采用邻接表存储图结构以节省内存。核心步骤包括:初始化距离数组(起点设为0,其余为无穷大),通过优先队列每次选取...
【算法】【Python】好数
我的题解 def isGood(x): x = str(x) lenx = len(x) for i in range(lenx): # 奇数位 if i % 2 == 0: if int(x[-i-1]) % 2 == 0: return False else: if int(x[-i-1]) % 2 == 1: return False r...
【C】二维数组每行冒泡排序
void sort2D(int arr[][cols], int rows, int cols) { int temp; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols - 1; j++) { for (...
【模拟】各位相加
#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 <&...
【深基5.习6】蛇形方阵
题目描述 给出一个不大于 9的正整数n,输出n*n的蛇形方阵。 从左上角填上 1开始,顺时针方向依次填入数字,如同样例所示。注意每个数字有都会占用 3个字符,前面使用空格补齐。 输入格式 输入一...
【算法】【Python】合数个数(素数筛法)
埃拉托色尼筛法通过标记法筛除合数,高效找出素数。算法从 2 开始,将其倍数标记为合数,避免重复计算,时间复杂度为 O(n log log n)。代码统计 1 到 2020 的合数个数,优化点包括从 i*i 开始筛...
【递归】反转链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { stru...
【算法】【Python】使用动态规划(DP)解决最长公共子序列(LCS)问题
使用动态规划计算 LCS 长度后,从dp[m][n]回溯构造 LCS 字符串:若text1[i-1] == text2[j-1],加入 LCS 并向左上移动,否则向dp值较大的方向移动。最终反转 LCS 输出。时间复杂度 O(m×n)。
【C】P2415 集合求和
集合求和 题目描述 给定一个集合 s(集合元素数量 <=30),求出此集合所有子集元素之和。 输入格式 集合中的元素(元素 <=1000) 输出格式 s 所有子集元素之和。 样例 #1 样例输入 #1 2 3...
【算法】【Python】itertools包的妙用
本文系统解析Python标准库itertools在算法竞赛中的高效应用,重点剖析排列组合、笛卡尔积、前缀和等核心功能。permutations处理全排列问题,combinations实现子集枚举,product替代多重嵌套循环...