-
讨论 (0)
这个用户还没有发布过讨论 -
贡献 (0)
啊哦,这个用户还没贡献过题目和题解~ -
递交 (4)
最近递交
状态 题目 递交者 时间 内存 语言 递交时间 P1000 A+B Problem Unique_AC 27ms 532.0 KiB C++ 2025-07-23 08:54:29 P1070 新年趣事之游戏 Unique_AC 528ms 1.898 MiB C++ 2025-07-23 08:49:32 P1070 新年趣事之游戏 Unique_AC 136ms 652.0 KiB C++ 2025-07-23 08:49:05 P1070 新年趣事之游戏 Unique_AC 255ms 1.801 MiB C++ 2025-07-23 08:48:13
个人简介
般天高千效钢命副宁一乎?
\[
\color{#069}
\raisebox{60pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{16.99pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{76.99pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{6.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{83.68pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{4.88pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{88.55pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{3.90pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{92.45pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{3.24pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{95.69pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{2.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{98.44pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{2.36pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{100.8pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{2.03pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{102.83pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{1.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{104.58pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{1.50pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{106.08pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{1.27pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{107.35pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{1.06pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{108.42pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{0.87pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{109.29pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{0.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{109.97pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{0.5pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{110.48pt}{$\rule{4pt}{0pt}$}\raisebox{110.81pt}{$\rule{4.00pt}{0pt}$}\raisebox{110.98pt}{$\rule{4pt}{0pt}$}\raisebox{110.81pt}{$\rule{4pt}{0pt}$}\raisebox{110.48pt}{$\rule{4.00pt}{0pt}$}\raisebox{109.97pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{0.5pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{109.29pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{0.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{108.42pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{0.87pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{107.35pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{1.06pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{106.08pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{1.27pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{104.58pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{1.50pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{102.83pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{1.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{100.8pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{2.03pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{98.44pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{2.36pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{95.69pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{2.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{92.45pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{3.24pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{88.55pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{3.90pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{83.68pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{4.88pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{76.99pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{6.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{60pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{16.99pt}\kern{-4pt}}}\kern{2pt}$}\kern{-140pt}\raisebox{43pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{16.99pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{36.32pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{6.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{31.45pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{4.88pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{27.55pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{3.90pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{24.3pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{3.24pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{21.56pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{2.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{19.20pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{2.36pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{17.17pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{2.03pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{15.42pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{1.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{13.92pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{1.50pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{12.65pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{1.27pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{11.58pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{1.06pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{10.71pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{0.87pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{10.03pt}{$\kern{2pt}\bcancel{\phantom{\rule{4.00pt}{0.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{9.52pt}{$\kern{2pt}\bcancel{\phantom{\rule{4pt}{0.5pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{9.19pt}{$\rule{4pt}{0pt}$}\raisebox{9.02pt}{$\rule{4.00pt}{0pt}$}\raisebox{9.02pt}{$\rule{4pt}{0pt}$}\raisebox{9.02pt}{$\rule{4pt}{0pt}$}\raisebox{9.19pt}{$\rule{4.00pt}{0pt}$}\raisebox{9.52pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{0.5pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{10.03pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{0.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{10.71pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{0.87pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{11.58pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{1.06pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{12.65pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{1.27pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{13.92pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{1.50pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{15.42pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{1.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{17.17pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{2.03pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{19.2pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{2.36pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{21.56pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{2.75pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{24.3pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{3.24pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{27.55pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{3.90pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{31.45pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{4.88pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{36.32pt}{$\kern{2pt}\cancel{\phantom{\rule{4pt}{6.68pt}\kern{-4pt}}}\kern{2pt}$}\raisebox{43pt}{$\kern{2pt}\cancel{\phantom{\rule{4.00pt}{16.99pt}\kern{-4pt}}}\kern{2pt}$}\\[-103pt]
\Large中\raisebox{4pt}{国}
\raisebox{8pt}{收钱}
\raisebox{4pt}协会\\[3pt]
\Huge¥\\[1pt]
\kern{4pt}€\kern{6pt}\raisebox{-5pt}€\raisebox{-1pt}£
\]
学习时间到
保留小数位数(四舍五入,两位)
printf("%.2lf", f);
cout << fixed << setprecision(2) << f;
(需要 iomanip 头文件)
常用基本数据类型
int
short
long
long long
(以上可加 unsigned, signed 等)
float
double
long double
char
bool
...
%d -> int, long
%lld -> long long
%f -> float
%lf -> double
%Lf -> long double
字符串流:stringstream
例题:
现在有 \(n\) 行数字,每行数字的个数不一,求出这些数字的总和。
样例输入
5
1 2 3 4 5
5 4
样例输出
24
思路
使用 stringstream 字符串流处理每一行。
其中,stringstream 在 sstream 头文件中。
Code
#include <iostream>
#include <sstream>
using namespace std;
int main(){
int n;
cin >> n;
getchar(); //cin 后要加 getchar
int sum = 0;
while(n--){
string s;
getline(cin, s);
stringstream ss(s); // 字符串流
int k;
while(ss >> k)
sum += k;
}
cout << sum;
return 0;
}
随机函数
rand
生成一个在 \([0,\rm{RAND\_MAX}]\) 中的随机非负整数。使用时需要 #include <cstdlib>
,需要用 srand()
设置种子,有一种办法是用现在时间来当作种子:srand(time(nullptr))
。
rand()%n
不能保证均匀性,因为 \([0,n)\) 中的每个数在 \(0\bmod n,1\bmod n,2\bmod n,\dots,\rm{RAND\_MAX}\bmod n\) 中的出现次数可能不相同。
注意,在 Windows 系统下 \(\rm{RAND\_MAX}=2^{15}-1\)。所以要生成比 \(2^{15}-1\) 更大的数字,建议用 (rand() << 15 | rand())
。
mt19937
注意,本内容仅在 C++11 及以后标准中开始使用。
是一个随机数生成器类,效用同 rand,随机数的范围同 unsigned int 类型的取值范围。
使用时用其定义一个随机数生成器即可:std::mt19937 myrand(seed)
,seed 可不填,不填 seed 则会使用默认随机种子。需要生成随机数时调用 myrand()
即可返回一个随机数。
另一个类似的生成器是 mt19937_64,使用方式同 mt19937,但随机数范围扩大到了 unsigned long long 类型的取值范围。
```cpp
// 此程序随机输出一个 unsigned int。
#include <ctime>
#include <iostream>
#include <random>
using namespace std;
int main() {
mt19937 rng(time(nullptr));
cout << rng() << endl;
return 0;
}
```
shuffle
注意,本内容仅在 C++11 及以后标准中开始使用。
用于随机打乱指定序列。使用时需要 #include<algorithm>
。
必须使用自定义的随机数生成器:std::shuffle(first, last, myrand)
。
```cpp
// 此程序输出打乱后的数组 a。
#include <ctime>
#include <iostream>
#include <algorithm>
#include <random>
using namespace std;
int main() {
mt19937 rng(time(nullptr));
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
shuffle(a, a + 10, rng);
for(int i = 0; i < 10; i++)
cout << a[i] << " ";
return 0;
}
```
uniform_int_distribution
注意,本内容仅在 C++11 及以后标准中开始使用。
生成随机整数值 \(i\),均匀分布于闭区间 \([a,b]\)。使用自定义的随机数生成器,将生成器的随机数变换为范围内的随机数。
```cpp
// 此程序随机输出 10 个在 [1,100] 范围内的 int。
#include <ctime>
#include <iostream>
#include <random>
using namespace std;
int main() {
mt19937 rng(time(nullptr));
uniform_int_distribution<int> distrib(1, 100);
for(int i = 1; i <= 10; i++)
cout << distrib(rng) << " ";
return 0;
}
```
数据结构模板(更新中)
template<typename T>
class Stack{
int len, maxl;
T *a;
public:
Stack(int maxn){
a = new T[maxn + 5];
maxl = maxn;
memset(a, 0, sizeof a);
len = 0;
}
bool empty(){
return len == 0;
}
bool full(){
return len >= maxl;
}
void clear(){
len = 0;
}
void push(T x){
if(full())
throw "The stack is full!";
a[++len] = x;
}
void pop(){
if(empty())
throw "The stack is empty!";
len--;
}
T top(){
if(empty())
throw "The stack is empty!";
return a[len];
}
int size(){
return len;
}
~Stack(){
delete a;
}
};
template<typename T>
class Queue{
int L, R, maxl, len;
T *a;
public:
Queue(int maxn){
maxn++;
a = new T[maxn + 5];
maxl = maxn;
memset(a, 0, sizeof a);
L = R = len = 0;
}
bool empty(){
return L == R;
}
bool full(){
return (R + 1) % maxl == L;
}
void clear(){
L = R;
len = 0;
}
void push(T x){
if(full())
throw "The queue is full!";
R = (R + 1) % maxl;
a[R] = x;
len++;
}
void pop(){
if(empty())
throw "The queue is empty!";
L = (L + 1) % maxl;
len--;
}
T front(){
if(empty())
throw "The queue is empty!";
return a[R];
}
T back(){
if(empty())
throw "The queue is empty!";
return a[(L + 1) % maxl];
}
int size(){
return len;
}
~Queue(){
delete a;
}
};
template<typename T>
class BinaryHeap{
// 默认实现小根堆
int n, maxn;
T *a;
public:
BinaryHeap(int k){
a = new T[k + 5];
maxn = k;
n = 0;
}
void up(int x){
while(x > 1 && a[x] < a[x >> 1]) {
std::swap(a[x], a[x >> 1]);
x >>= 1;
}
}
void down(int x){
while((x << 1) <= n){
int tmp = x << 1;
if((tmp | 1) <= n && a[tmp | 1] < a[tmp])
tmp |= 1;
if(a[x] < a[tmp])
break;
std::swap(a[x], a[tmp]);
x = tmp;
}
}
bool full(){
return n >= maxn;
}
bool empty(){
return n == 0;
}
int size(){
return n;
}
void push(T x){
if(full())
throw "The binary-heap is full!";
a[++n] = x;
up(n);
}
void pop(){
if(empty())
throw "The binary-heap is empty!";
std::swap(a[1], a[n]);
n--;
down(1);
}
T top(){
if(empty())
throw "The binary-heap is empty!";
return a[1];
}
~BinaryHeap(){
delete a;
}
};
class DSU{
int maxn;
int* f;
public:
DSU(int k){
f = new int[k + 5ll];
maxn = k;
for(int i = 0; i <= maxn; i++)
f[i] = i;
}
void clear(){
for(int i = 0; i <= maxn; i++)
f[i] = i;
}
int find(int x){
return f[x] == x ? x : f[x] = find(f[x]);
}
bool same(int x, int y){
return find(x) == find(y);
}
void merge(int x, int y){
x = find(x), y = find(y);
if(x != y)
f[x] = y;
}
};