头文件
#include<iostream>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
#include<list>
#include<bitset>//二进制函数
#include<iomanip>//进制转换
#include<cstdlib>//文件输入输出
#include<cmath>//常用数学函数
输入方式
getline(cin,str) 主要用于字符串输入,遇到空格不会停
scanf 遇到空格会停 无法获取回车
getchar() 可获取回车
进制转换
P进制转换为十进制
int y = 0,product = 1;
while(x){
y = y + (x%10)*product;
x = x/10;
product = product * P;
}
y转换为Q进制 反向输出
int z[40],num = 0;
do{
z[num++] = y%Q;
y = y/Q;
} while(y!= 0)
排序
冒泡
#include<iostream>
using namespace std;
int main(){
int a[10] = {3,1,4,5,2};
for(int i = 1;i <= 4;i++){
for(int j = 0;j < 5-i;j++){
if(a[j] > a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int i = 0;i < 5;i++) printf("%d ",a[i]);
return 0;
}
选择
void selectSort(){
for(int i = 1;i <= n; i++){
int k = i;
for(int j = i;j <= n; j++){
if(A[j] < A[k]) k = j;
}
int temp = A[i];
A[i] = A[k];
A[k] = temp;
}
}
插入
void insertSort(){
for(int i = 2;i <= n;i++){
int temp = A[j],j = i;
while(j > 1 && temp < A[j - 1]){
A[j] = A[j-1];
j--;
}
A[j] = temp;
}
}
常用函数
to_string
循环输入结束
while(cin>>a){
if(cin.get() == '\n')break;
}
while(scanf("%d",&n)!=EOF){
xxx
}
闰年
bool isLeap(int year){
return (year % 4 ==0 && year %100!=0) || (year %400==0);
}
//全排列
void main1()
{ int str[]={1,2,3,4,5,6,7,8,9};
int cns=9;
do{
//条件判断
}while(next_permutation(str,str+cns)); }
//快速幂
long long pow_2(int a,int b)
{
long x=a;
long res=1;
while(b>0)
{
if(b&1)
{
res*=x;
}
b>>=1;
x*=x;
}
return res;
}
int gcd(int a,int b){gcd/lcm
return !b ? a : gcd(b,a%b);
}
//最小公倍数
int lcm(int a,int b)
{ return a*b/gcd(a,b); }
素数
bool isprime(int n){
if(n <= 1) return false;
for(int i = 2;i * i <= n;i++){
if(n%i == 0) return false;
}
return true;
}
判断回文
bool huiwen(int x){
int y=x,num=0;
while(y != 0){
num = num*10 + y%10;
y/=10;
}
if(num ==x) return 1;
else return 0;
}
常用STL
vector<int> res;
vector<int> ::iterator it;
res.push_back();
res.pop_back();
res.size();
res.clear();
res.insert(it,x);
set<int> s; 自动排序且去重
s.insert(x);
for(auto it = s.begin();it != s.end();it++){//遍历
printf("%d",*it);
}
s.find(x) != s.end();
string str;
str.insert(3,str2);
str.erase(x,y);
str.substr(x,y); y取不到
string::nops 代指取不到的最大数
str1.find(str2); 找不到返回-1
str1.replace(pos,len,str2);
map<int,bool> _m;
map<int, int>::iterator iter;
iter = _m.begin();
while(iter != _m.end()) {
cout << iter->first << " : " << iter->second << endl;
iter++;
}
map<int,int>::iterator it = m.find('xxx');
printf("%d %d",it->first,it->second);
queue<int> q;
front()/pop()/push()/size()/empty()
stack<int> st;
push()/top()/pop()/empty()/size()
int a[] = { 1,2,3,4,5 };
list<int> lt;
list<int>::iterator it;//创建迭代器
list<int> lt(a, a + 5);
list<int> lt(2, 100);
push_back,push_front(插入尾,插入头)
pop_back,pop_front(删除尾,删除头)
//assign(插入)
list<int> first;
list<int> second;
first.assgin(2,100);
//添加2个100的元素
second.assgin(first.begin(),first.end())//将first拷贝给second
//insert(指定位置插入)
/*
iterator insert (iterator position, const value_type& val);
//position是要插入的这个list的迭代器,val是要插入的值
void insert (iterator position, size_type n, const value_type& val);
//从该list容器中的position位置处开始,插入n个值为val的元素
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last);
//first,last是我们选择的把值插入到这个list中的值所在的容器的迭代器
*/
list<int> lt;
list<int>::iterator it;
it = lt.begin();
lt.insert(it, 2);
lt.insert(it, 2, 100);
lt.insert(it, sth.begin(), sth.end());
//在指定位置插入某容器的一个区段
//遍历
list<int> lt;
list<int>::iterator it;
for (it = lt.begin(); it != lt.end(); it++)
cout << *it;
//erase(删除元素)erase函数是可以有返回值的,注意当删除元素的同时,迭代器也被销毁了。 lt.erase(iterator it);//删除it位置的元素
lt.erase(iterator begin,iterator end);//删除一定区间的元素
//swap(交换)
list<int> first;
list<int> second;
first.swap(second);
//clear(清空)
lt.clear();
//splice(转移元素)
/*
void splice (iterator position, list& x);
//将列表x中的所有元素移到当前list中,从当前列表的position指向的位置开始,此时列表x为空 void splice (iterator position, list& x, iterator i);
//将列表x中迭代器 i 指向的元素移到当前list的position指向的位置处,由于i指向的元素从列表x中被移,所以迭代器 i 此时是invalid的;position是当前列表的迭代器,i是列表x的迭代器
void splice (iterator position, list& x, iterator first, iterator last);
//将列表x中[first, last)的元素移到当前list中,从position指向的位置开始;
first,last是列表x的迭代器
*/
//remove(移除指定元素)
/*
void remove (const value_type& val);
//从list中删除所有值为val的元素
*/
lt.remove(100);
//unique(删除重复值)
/*
void unique();
//只能删除相邻的重复元素,然后保留第一个值,因此这个函数只对排好序的list有用 */ //sort(排序) 默认升序,可自写cmp函数
lt.sort(cmp);
//reverse(逆序) lt.reserve();
//merge(合并有序的list)
list<int> first;
list<int> second;
first.merge(second);
//remove_if(按条件移除元素)
bool single_digit (const int& value) {
return (value < 10); }
lt.remove_if (single_digit);
注意
01背包
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int v[MAXN];
int w[MAXN];
int dp[MAXN][MAXN];
int main(){
int n,m;
cin>>n>>m;
for(int i = 1; i <= n;i++){
cin>>v[i]>>w[i];
}
for(int i = 1; i <= n;i++){
for(int j = 1;j <= m; j++){
if(j < v[i])
dp[i][j] = dp[i-1][j];
else
dp[i][j] = max(dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
}
}
cout<<dp[n][m]<<endl;
return 0;
}
完全背包
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int v[MAXN];
int w[MAXN];
int dp[MAXN][MAXN];
int main(){
int n,m;
cin>>n>>m;
for(int i = 1; i <= n;i++){
cin>>v[i]>>w[i];
}
for(int i = 1; i <= n;i++){
for(int j = 1;j <= m; j++){
dp[i][j] = dp[i-1][j];
if(j - v[i] >= 0)
dp[i][j] = max(dp[i][j],dp[i][j-v[i]]+w[i]);
}
}
cout<<dp[n][m]<<endl;
return 0;
}