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;
}
上一篇:STL序列式容器之list


下一篇:【不写for循环】玩玩行列