牛客春招刷题训练营-2025.3.12题解
活动地址: 牛客春招刷题训练营 - 编程打卡活动
简单题 进制转换
可以手动枚举最低位到最高位,也可以使用库函数直接转换。
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin>>s;
cout<<stoi(s,nullptr,16);
return 0;
}
中等题 合并表记录
开一个数组,累计每个索引的值。
输出时,枚举整个值域,如果值不为 0 ,才输出。
当然,也可以开个 map ,方便快捷。
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
map<int,int> a;
int n,i,k,v;
cin>>n;
for(i=1;i<=n;++i)
{
cin>>k>>v;
a[k]+=v;
}
for(auto &p:a)
cout<<p.first<<' '<<p.second<<'\n';
return 0;
}
困难题 称砝码
多重背包 典题,注意到物品数量限制不多,可以不用二进制优化。
#include <iostream>
#include <bitset>
using namespace std;
const int N=200100;
const int M=20;
bitset<N> b;
int m[M],x[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,i,j;
cin>>n;
for(i=1;i<=n;++i)
cin>>m[i];
for(i=1;i<=n;++i)
cin>>x[i];
b[0]=1;
for(i=1;i<=n;++i)
for(j=1;j<=x[i];++j)
b|=b<<m[i];
cout<<b.count();
return 0;
}
#牛客春招刷题训练营#