一、无人机打靶
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
struct Node
{
char c;
int s;
bool operator > (const Node& t)const
{
return s>t.s;
}
}q[N];
unordered_map<char,int>cnt;
unordered_map<char,string>color
{
{'g', "green"},
{'r', "red"},
{'b', "blue"},
{'p', "purple"}
};
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>q[i].c>>q[i].s;
cnt[q[i].c]++;
}
sort(q,q+n,greater<Node>());
for(auto x : cnt)
{
cout<<color[x.first]<<' '<<x.second<<endl;
}
for(int i=0;i<n;i++)
{
cout<<q[i].c<<' '<<q[i].s<<endl;
}
return 0;
}
二、顺子
bool judge(vector<int>& nums)
{
int Max=-1,Min=15;
unordered_set<int>s;
for(int x : nums)
{
if(x==0)continue;
Max=max(Max,x),Min=min(Min,x);
if(s.count(x))return false;
s.insert(x);
}
return (Max-Min)<5
}
三、递归实现指数型枚举
#include <bits/stdc++.h>
using namespace std;
const int N = 20, M = 3010;
int n, m;
int v[N], st[N];
vector<int> res;
bool flag;
void dfs(int u,int sum)
{
if(u>n||sum>m)
return;
if(sum==m)
{
for(int x : res)
cout<<v[x]<<' ';
flag=true;
cout<<endl;
return;
}
res.push_back(u);
dfs(u+1,sum+v[u]);
res.pop_back();
dfs(u+1,sum);
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>v[i];
dfs(0,0);
if(!flag)
cout<<"None";
return 0;
}