当前位置: 首页 > news >正文

C++的一些模版

1、不限制次数的输入数据

   vector<int> nums;int num;while (cin >> num) {nums.push_back(num);if (cin.get() == '\n') break;}

2、取模模版

template<int kcz>
struct ModInt {
#define T (*this)int x;ModInt() : x(0) {}ModInt(int y) : x(y >= 0 ? y : y + kcz) {}ModInt(LL y) : x(y >= 0 ? y % kcz : (kcz - (-y) % kcz) % kcz) {}inline int inc(const int &v) {return v >= kcz ? v - kcz : v;}inline int dec(const int &v) {return v < 0 ? v + kcz : v;}inline ModInt &operator+=(const ModInt &p) {x = inc(x + p.x);return T;}inline ModInt &operator-=(const ModInt &p) {x = dec(x - p.x);return T;}inline ModInt &operator*=(const ModInt &p) {x = (int) ((LL) x * p.x % kcz);return T;}inline ModInt inverse() const {int a = x, b = kcz, u = 1, v = 0, t;while (b > 0)t = a / b, std::swap(a -= t * b, b), std::swap(u -= t * v, v);return u;}inline ModInt &operator/=(const ModInt &p) {T *= p.inverse();return T;}inline ModInt operator-() const {return -x;}inline friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {return ModInt(lhs) += rhs;}inline friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {return ModInt(lhs) -= rhs;}inline friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {return ModInt(lhs) *= rhs;}inline friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {return ModInt(lhs) /= rhs;}inline bool operator==(const ModInt &p) const {return x == p.x;}inline bool operator!=(const ModInt &p) const {return x != p.x;}inline ModInt qpow(LL n) const {ModInt ret(1), mul(x);while (n > 0) {if (n & 1)ret *= mul;mul *= mul, n >>= 1;}return ret;}inline friend std::ostream &operator<<(std::ostream &os, const ModInt &p) {return os << p.x;}inline friend std::istream &operator>>(std::istream &is, ModInt &a) {LL t;is >> t, a = ModInt<kcz>(t);return is;}static int get_mod() {return kcz;}inline bool operator<(const ModInt &A) const {return x < A.x;}inline bool operator>(const ModInt &A) const {return x > A.x;}
#undef T
};// kcz 就是要取的模
const int kcz = 1'000'000'007;
// 可以用Z定义整型数据,每次操作都会取模
using Z = ModInt<kcz>;

3、进制转换

#include<bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int n;cin>>n;while(n--){unsigned int a,b;cin>>a>>b;while(!(a&1))a>>1;while(!(b&1))b>>1;if(a<b){cout<<"No\n";return 0;} // 因为输入的是10进制,所以我们要转化这两个数的二进制状态vector<int> A,B;while(a>0){A.push_back(a&1);a=>>1;}while(b>0){B.push_back(b&1);b=>>1;}  //判断 b 是否在 a 里面int n = (int) A.size(), m = (int) B.size();// 字符串匹配 for (int i = 0; i + m - 1 < n; i++) {int fl = 0;for (int j = 0; j < m; j++) {// 对于 A 来讲 肯定是 每次移动,B 从头开始的 if (A[i + j] != B[j]) {fl = 1;break;}}}if (!fl) {cout << "Yes\n";return 0;}cout << "No\n";} return 0;
}
http://www.lryc.cn/news/484995.html

相关文章:

  • spring boot整合https协议
  • 服务器开机即占用大量内存,解决
  • Keil uvision的edition
  • [每周一更]-(第123期):模拟面试|消息队列面试思路解析
  • 游戏引擎学习第12天
  • 深入理解Flutter生命周期函数之StatefulWidget(一)
  • 413: Quick Sort
  • vue之axios根据某个接口创建实例,并设置headers和超时时间,捕捉异常
  • Pandas数据透视表:交叉分析与聚合计算
  • 软件设计师考试大纲
  • 一文说清C++类型转换操作符(cast operator)
  • MOSFET电路栅源极GS之间并联电容后,MOS炸管原因分析
  • gitHub常用操作
  • [项目代码] YOLOv5 铁路工人安全帽安全背心识别 [目标检测]
  • Java 垃圾回收机制(GC)概览
  • Kafka节点服役和退役
  • Git如何简单使用
  • 酒水分销积分商城小程序开发方案php+uniapp
  • MTU-内核态(数据链路层或网络接口上能够传输的最大数据包大小)
  • React的基础API介绍(一)
  • 【Electron】总结:如何创建Electron+Element Plus的项目
  • 从依托指标字典到 NoETL 自动化指标平台,指标口径一致性管理的进阶
  • 嵌入式面试题练习 - 2024/11/15
  • 分析http话术异常挂断原因
  • 云岚到家 秒杀抢购
  • 【WPF】Prism库学习(一)
  • 0 -vscode搭建python环境教程参考(windows)
  • Uniapp 引入 Android aar 包 和 Android 离线打包
  • 10款高效音频剪辑工具,让声音编辑更上一层楼。
  • Javascript——设计模式(一)