更高级的进制转换

typedef long long ll;

string dta (ll n, ll base) { // dec to any
	string res;
	while (n) {
		int t = n % base;
		if(t < 10) {
			res += t + '0';
		} else {
			res += t - 10 + 'A';
		}
		n /= base;
	}
	reverse(res.begin(), res.end());
	return res;
}

01背包问题

#include <bits/stdc++.h>
using namespace std;

struct bag {
	int v, w;
};

bag b[105];
int dp[50000];
int n, m;

int main() {
	cin >> m >> n; // m指背包重量, n为数目
	for (int i = 1; i <= n; i++) {
		cin >> b[i].v >> b[i].w;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = m; j >= b[i].v; j--) { //注意从m开始
			if (j >= b[i].v) {
				dp[j] = max(dp[j], dp[j - b[i].v] + b[i].w); //dp
				// 状态转移方程
			}
		}
	}
	cout << dp[m] << endl; //背包大小为m时最大值
	return 0;
}

转进制(n \leq 16)

void toBase(int n, int b) {
	if (n == 0) {
		return;
	}
	toBase(n / b, b);
	if (n % b >= 10) {
		cout << char(n % b - 10 + 'A');
	} else {
		cout << n % b;
	}
}

最大公因数和最小公倍数

inline int gcd(int x, int y) {
	if (y == 0) {
		return x;
	}
	return gcd(y, x % y);
}

inline int lcm(int x, int y) {
	return x * y / gcd(x, y);
}

过滤空格

string deal (string s) {
	int k = s.find(' ');
	while (k) {
		s.erase(s.begin() + k);
		k = s.find(' ') != s.npos;
	}
	return s;
}

中缀转后缀

stack<char> s1;		//运算符栈
stack<char> s2;		//中间结果栈

int f(const char str) {
	int yxj;		//优先级
	switch (str) {
		case '*':
			yxj = 5;
			break;
		case '/':
			yxj = 5;
			break;
		case '+':
			yxj = 4;
			break;
		case '-':
			yxj = 4;
			break;
	}
	return yxj;
}

string Infix_to_suffix (string s) {
	int lenc = s.length();
	//读取字符串
	for (int i = 0; i < lenc; i++) {
		if (s[i] >= '0' && s[i] <= '9') {		//如果是数字,直接压入s2
			s2.push(s[i]);
		} else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {	//如果是运算符
			while (true) {
				if (s1.empty() || s1.top() == '(') {		//s1为空 ,或者栈顶为(
					s1.push(s[i]);
					break;
				} else if (f(s[i]) > f(s1.top())) {		//当前运算符优先级大于s1栈顶运算符优先级
					s1.push(s[i]);
					break;
				} else {								//小于等于
					char ss = s1.top();
					s1.pop();
					s2.push(ss);
				}
			}
		} else {
			if (s[i] == '(') {			//直接读入
				s1.push(s[i]);
			} else {
				while (s1.top() != '(') {
					char ss = s1.top();
					s1.pop();
					s2.push(ss);
				}
				s1.pop();
			}
		}
	}
	string res;
	while (!s1.empty()) {
		char ss = s1.top();
		s2.push(ss);
		s1.pop();
	}
	while (!s2.empty()) {
		char c = s2.top();
		s1.push(c);
		s2.pop();
	}
	while (!s1.empty()) {
		res += s1.top();
		s1.pop();
	}
	return res;
}

计算中缀表达式

#include <bits/stdc++.h>
using namespace std;

int getPriority(char ch) {
	//获取优先级
	if (ch == '(') {
		return 1;
	} else if (ch == '+' || ch == '-') {
		return 2;
	} else if (ch == '*' || ch == '/') {
		return 3;
	} else {
		return 4;
	}
}

void calculate(stack<double> &mystack, char operation) {
	double num1, num2, num3;
	num2 = mystack.top();
	mystack.pop();
	num1 = mystack.top();
	mystack.pop();
	if (operation == '+') {
		num3 = num1 + num2;
	} else if (operation == '-') {
		num3 = num1 - num2;
	} else if (operation == '*') {
		num3 = num1 * num2;
	} else if (operation == '/') {
		num3 = num1 / num2;
	}
	mystack.push(num3);
}

double calculator(string str) {
	//计算中缀表达式,默认输入是合法的
	stack<double> stack_number;
	stack<char> stack_operation;
	int i = 0, j;
	int size = str.size();
	char tmp_operation;
	string tmp_num;
	while (i < size) {
		if (str[i] >= '0' && str[i] <= '9') {
			j = i;
			while (j < size && str[j] >= '0' && str[j] <= '9') {
				j++;
			}
			tmp_num = str.substr(i, j - i);
			stack_number.push(atoi(tmp_num.c_str()));
			i = j;
		} else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
			if (stack_operation.empty()) {
				stack_operation.push(str[i]);
			} else {
				while (!stack_operation.empty()) {
					tmp_operation = stack_operation.top();
					if (getPriority(tmp_operation) >= getPriority(str[i])) {
						calculate(stack_number, tmp_operation);
						stack_operation.pop();
					} else break;
				}
				stack_operation.push(str[i]);
			}
			i++;
		} else {
			if (str[i] == '(') stack_operation.push(str[i]);
			else {
				while (stack_operation.top() != '(') {
					tmp_operation = stack_operation.top();
					calculate(stack_number, tmp_operation);
					stack_operation.pop();
				}
				stack_operation.pop();
			}
			i++;
		}
	}
	//遍历完后,若栈非空,弹出所有元素
	while (!stack_operation.empty()) {
		tmp_operation = stack_operation.top();
		//计算
		calculate(stack_number, tmp_operation);
		stack_operation.pop();
	}
	return stack_number.top();
}

string deal (string str) {
	int k = str.find(' ') != str.npos;
	while (k) {
		str.erase(str.begin() + str.find(' '));
		k = str.find(' ') != str.npos;
	}
	return str;
}

int main() {
	string str;
	getline(cin, str);
	str = deal(str);
	double num_res = calculator(str);
	cout << num_res << endl;
	return 0;
}

求10000以内的阶乘

c++c++

#include<bits/stdc++.h>
using namespace std;

int factorial[100005] = {1, 1}; //存放阶乘 ans[0]是位数 倒序存放数字
int carry[100005];//进位的值
int n;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) { //遍历1~n
		//求出i的阶乘
		// i!= (i-1)! * i
		for (int j = 1; j <= factorial[0]; j++) {
			//ans[0]中存放阶乘的位数
			factorial[j] = factorial[j] * i + carry[j]; //将i与每一位进行相乘
			carry[j] = 0; //重置进位值

			if (factorial[j] >= 10) { //超过10要进位
				carry[j + 1] += factorial[j] / 10; //记录进位值
				factorial[j] %= 10; //保留个位
				if (j == factorial[0]) {
					factorial[0]++;   //如果到了位数又发生进位,那么位数要发生变化
				}
			}
		}
	}

	for (int i = factorial[0]; i >= 1; i--) { //从高位开始倒序输出结果
		cout << factorial[i];
	}
	return 0;
}

pythonpython

import math
n = int(input())
result = math.factorial(n)
print(f"{result}")

大整数减法

char str1[256], str2[256], temp[256];
int a[256], b[256], c[256];
int lena, lenb, lenc, i;
void *subtraction () {
	lena = strlen(str1);
	lenb = strlen(str2);
	if ( (lena < lenb) || (lena == lenb && strcmp(str1, str2)) ) {
		strcpy(temp, str1);
		strcpy(str1, str2);
		strcpy(str2, temp);
		cout << "-";
	}
	lena = strlen(str1);
	lenb = strlen(str2);
	for (i = 0; i <= lena - 1; i++) {
		a[lena - i] = str1[i] - '0';
	}
	for (i = 0; i <= lenb - 1; i++) {
		b[lenb - i] = str2[i] - '0';
	}
	i = 1;
	while (i <= lena || i <= lenb) {
		if (a[i] < b[i]) {
			a[i] += 10;
			a[i + 1]--;
		}
		c[i] = a[i] - b[i];
		i++;
	}
	lenc = i;
	while ((c[lenc] == 0) && (lenc > 1)) {
		lenc--;
	}
	return c;
}

快读快写

template <typename T>
T read () {
	T res = 0, f = 1;
	char ch = getchar();
	while(!(ch >= '0' && ch <= '9')) {
		if (ch == '-') {
			f = -1;
		}
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		res = (res << 1) + (res << 3) + (ch ^ 48);
		ch = getchar();
	}
	return res * f;
}

template <typename T>
void write (T n) {
	if(n < 0) {
		putchar('-');
		n = -n;
	}
	if(n >= 10) {
		write(n / 10);
	}
	putchar(n % 10 + '0');
}

常用string函数

#include <bits/stdc++.h>
using namespace std;
#define sc(n) cout << n << '\n' << '\n'

int main() {
	string s;
	cin >> s;
	sc(s);
	// 输入字符串,读取到空格&回车结束

	fflush(stdin); // 刷新缓存区
	getline(cin, s); // 读取到回车才结束
	sc(s); // 等价于scanf("%s", s)

	s = to_string(123456); // 将整形转为字符串
	sc(s);

	s.clear(); // 清空
	sc(s << '\n' << "清空了s");

	s.append("1 2 3abcd"); // 在末尾添加元素
	sc(s);

	sc(*(s.begin())); // 返回头元素

	sc(*(s.end() - 1)); // 返回尾元素,end是末位后一个位置

	// cbegin(), cend(), rend(), rbegin(), crbegin(), crend();
	// 全家桶(((

	sc(s.length() << ' ' << s.size()); // 返回长度(元素数量)

	s = s.substr(0, 3); // 从0开始截取3个元素
	sc(s);

	sc(s.max_size()); // 输出最大长度

	sc(s.at(0)); // 下标,等价于s[0],但是如果越界会抛异常(value 3)

	sc(s.empty()); // 判断字符串是否为空, 0 = 不为空, 1 = 为空

	s.push_back('1'); // 在末尾添加一个字符,如果为数字会输出乱码...
	sc(s);

	s.pop_back(); // 弹出末尾单个元素
	sc(s);

	s.append("114514 1919810 Orz, qpzc"); // 加点元素不要在意(((

	s.erase(0, 3); // 从0开始删除3个元素
	sc(s);

	s.insert(0, "qwq"); // 从0插入qwq
	sc(s);

	sc(s.find("qwq") << ' ' << s.find("11111")); // 如若查找到返回起始处,否则返回npos

	sc(s.npos); // 一段特定的数字?

	for (string::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it;
	}
	cout << '\n' << '\n';
	// 正向迭代器

	for (string::reverse_iterator it = s.rbegin(); it != s.rend(); it++) {
		cout << *it;
	}
	cout << '\n' << '\n';
	// 反向迭代器
	
	for(auto i : s){
		cout << i;
	}
	cout << '\n' << '\n';
	// Range-based-for
	return 0;
}