以下题目写法上都用的是刘教的写法,虽然有更优的写法,但是期末考试应该只考以下内容。
栈
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
typedef unsigned long long ull;
ull T,n,x,i;
string s;
stack<ull> st;
void work()
{
cin>>n;
while (!st.empty()) st.pop();
for (i=1;i<=n;i++)
{
cin>>s;
if (s=="push") cin>>x,st.push(x);
else if (s=="pop")
{
if (st.empty()) cout<<"Empty\n";
else st.pop();
}
else if (s=="query")
{
if (st.empty()) cout<<"Anguei!\n";
else cout<<st.top()<<"\n";
}
else cout<<st.size()<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while (T--) work();
return 0;
}队列
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
int n,i,op,x;
queue<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<=n;i++)
{
cin>>op;
if (op==1) cin>>x,q.push(x);
else if (op==2)
{
if (q.empty()) cout<<"ERR_CANNOT_POP\n";
else q.pop();
}
else if (op==3)
{
if (q.empty()) cout<<"ERR_CANNOT_QUERY\n";
else cout<<q.front()<<"\n";
}
else cout<<q.size()<<"\n";
}
return 0;
}排列
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
typedef long long ll;
ll a,b;
ll A(ll x,ll y)
{
if (x<y) return 0;
if (!y) return 1;
return A(x-1,y-1)*x;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>a>>b;
cout<<A(a,b);
return 0;
}组合
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
typedef long long ll;
ll a,b;
ll C(ll x,ll y)
{
if (x<y) return 0;
if (!y) return 1;
return C(x-1,y-1)+C(x-1,y);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>a>>b;
cout<<C(a,b);
return 0;
}归并排序
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e5+10;
int n,i,a[N],t[N];
void msort(int l,int r)
{
if (l==r) return;
int mid=l+r>>1;
msort(l,mid);
msort(mid+1,r);
int i=l,j=mid+1,k=l-1;
while (i<=mid&&j<=r)
{
if (a[i]<=a[j]) t[++k]=a[i++];
else t[++k]=a[j++];
}
while (i<=mid) t[++k]=a[i++];
while (j<=r) t[++k]=a[j++];
for (i=l;i<=r;i++) a[i]=t[i];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<=n;i++) cin>>a[i];
msort(1,n);
for (i=1;i<=n;i++) cout<<a[i]<<" ";
return 0;
}逆序对
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
typedef long long ll;
const int N=5e5+10;
int n,i,a[N],t[N];
ll ans;
void msort(int l,int r)
{
if (l==r) return;
int mid=l+r>>1;
msort(l,mid);
msort(mid+1,r);
int i=l,j=mid+1,k=l-1;
while (i<=mid&&j<=r)
{
if (a[i]<=a[j]) t[++k]=a[i++];
else ans+=mid-i+1,t[++k]=a[j++];
}
while (i<=mid) t[++k]=a[i++];
while (j<=r) t[++k]=a[j++];
for (i=l;i<=r;i++) a[i]=t[i];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<=n;i++) cin>>a[i];
msort(1,n);
cout<<ans;
return 0;
}拓扑排序
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=110;
int n,i,x;
int last[N],c,din[N];
queue<int> q;
struct Node{
int to,nxt;
}a[N*N*2];
void add(int u,int v)
{
a[++c]={v,last[u]};
last[u]=c;
}
void topo()
{
int i,x,to;
for (i=1;i<=n;i++)
{
if (!din[i]) q.push(i);
}
while (!q.empty())
{
x=q.front();
q.pop();
cout<<x<<" ";
for (i=last[x];i;i=a[i].nxt)
{
to=a[i].to;
din[to]--;
if (!din[to]) q.push(to);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<=n;i++)
{
while (1)
{
cin>>x;
if (!x) break;
add(i,x);
din[x]++;
}
}
topo();
return 0;
}二叉树的遍历
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e6+10;
int n,l[N],r[N],i;
void dfs1(int x)
{
cout<<x<<" ";
if (l[x]) dfs1(l[x]);
if (r[x]) dfs1(r[x]);
}
void dfs2(int x)
{
if (l[x]) dfs2(l[x]);
cout<<x<<" ";
if (r[x]) dfs2(r[x]);
}
void dfs3(int x)
{
if (l[x]) dfs3(l[x]);
if (r[x]) dfs3(r[x]);
cout<<x<<" ";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<=n;i++) cin>>l[i]>>r[i];
dfs1(1);
cout<<"\n";
dfs2(1);
cout<<"\n";
dfs3(1);
cout<<"\n";
return 0;
}埃氏筛(时间复杂度 )
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e8+10;
int n,q,i,j,x,k,p[N];
bool vis[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>q;
for (i=2;i<=n;i++)
{
if (!vis[i])
{
p[++k]=i;
for (j=i;j<=n/i;j++) vis[i*j]=true;
}
}
while (q--)
{
cin>>x;
cout<<p[x]<<"\n";
}
return 0;
}欧拉筛法(时间复杂度 )
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e8+10;
int n,q,i,j,x,k,p[N];
int f[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>q;
for (i=2;i<=n;i++)
{
if (!f[i]) f[i]=i,p[++k]=i;
for (j=1;j<=k&&i*p[j]<=n;j++)
{
f[i*p[j]]=p[j];
if (i%p[j]==0) break;
}
}
while (q--)
{
cin>>x;
cout<<p[x]<<"\n";
}
return 0;
}部分背包问题
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=110;
int n,m,i;
double ans;
struct Node{
int w,v;
double t;
}a[N];
bool cmp(Node x,Node y)
{
return x.t>y.t;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for (i=1;i<=n;i++) cin>>a[i].w>>a[i].v,a[i].t=a[i].v*1.0/a[i].w;
sort(a+1,a+n+1,cmp);
for (i=1;i<=n;i++) ans+=min(m,a[i].w)*a[i].t,m-=min(m,a[i].w);
cout<<fixed<<setprecision(2)<<ans<<"\n";
return 0;
}01背包
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=110,M=1010;
int m,n,i,j,w[N],v[N];
int f[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>m>>n;
for (i=1;i<=n;i++) cin>>w[i]>>v[i];
for (i=1;i<=n;i++)
{
for (j=m;j>=w[i];j--) f[j]=max(f[j],f[j-w[i]]+v[i]);
}
cout<<f[m];
return 0;
}完全背包
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
typedef long long ll;
const int N=10010,M=1e7+10;
int m,n,i,j;
ll w[N],v[N];
ll f[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>m>>n;
for (i=1;i<=n;i++) cin>>w[i]>>v[i];
for (i=1;i<=n;i++)
{
for (j=w[i];j<=m;j++) f[j]=max(f[j],f[j-w[i]]+v[i]);
}
cout<<f[m];
return 0;
}求数位和
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
typedef long long ll;
int n,i,ans;
ll x;
int num(ll x)
{
int res=0;
while (x)
{
res+=x%10;
x/=10;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<=n;i++)
{
cin>>x;
ans=max(ans,num(x));
}
cout<<ans;
return 0;
}上楼梯
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e5+10;
int n,i,f[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
f[0]=f[1]=1;
for (i=2;i<=n;i++) f[i]=f[i-1]+f[i-2];
cout<<f[n];
return 0;
}纸币问题
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e3+10,M=1e4+10,inf=1e9+10;
int n,m,i,j,a[N],f[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for (i=1;i<=n;i++) cin>>a[i];
for (i=1;i<=m;i++) f[i]=inf;
for (i=1;i<=n;i++)
{
for (j=a[i];j<=m;j++) f[j]=min(f[j],f[j-a[i]]+1);
}
cout<<f[m];
return 0;
}纸币问题
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e3+10,M=1e4+10,mod=1e9+7;
int n,m,i,j,a[N],f[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for (i=1;i<=n;i++) cin>>a[i];
f[0]=1;
for (j=1;j<=m;j++)
{
for (i=1;i<=n;i++)
{
if (j>=a[i]) f[j]=(f[j]+f[j-a[i]])%mod;
}
}
cout<<f[m]<<"\n";
return 0;
}纸币问题
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct FSI{
template<typename T>
FSI& operator >> (T &res){
res=0;T f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
res*=f;
return *this;
}
}scan;
const int N=1e3+10,M=1e4+10,mod=1e9+7;
int n,m,i,j,a[N],f[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for (i=1;i<=n;i++) cin>>a[i];
f[0]=1;
for (i=1;i<=n;i++)
{
for (j=a[i];j<=m;j++) f[j]=(f[j]+f[j-a[i]])%mod;
}
cout<<f[m];
return 0;
}