排序
【递归】反转链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { stru...
【并查集】Python模板
题目描述 如题,现在有一个并查集,你需要完成合并和查询操作。 输入格式 第一行包含两个整数 N,M ,表示共有 N 个元素和 M 个操作。 接下来 M 行,每行包含三个整数 Zi,Xi,Yi 。 当 Zi=1 时,将...
【递归】斐波那契数
#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; } 数...
【C】五舍六入?
如果题目让你五舍六入,那么就没办法使用C++里的round函数了,只能选择一个通用的方法,示例如下: #include <stdio.h> #define PI 3.1415926 int main(){ double r,ans; scanf('%lf',&r...
【递归】快速幂
class Solution: def myPow(self, x: float, n: int) -> float: if n==0: return 1 if n==1: return x if n==-1: ...
【深基5.习6】蛇形方阵
题目描述 给出一个不大于 9的正整数n,输出n*n的蛇形方阵。 从左上角填上 1开始,顺时针方向依次填入数字,如同样例所示。注意每个数字有都会占用 3个字符,前面使用空格补齐。 输入格式 输入一...