2021年冬季PAT乙级题解(C/C++语言)
7-1 自动打包机 (15 分)
原题
算法标签 模拟
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N,W,tW;
cin>>N>>W;
tW=W;
int res=0,cnt=0, ress=0;
while(N--){
int num;
cin>>num;
if(W>=num){
W-=num;
cnt++;
if(!W){
res++;
W=tW;
ress+=cnt;
cnt=0;
}
}
}
cout<<res<<" "<<ress;
return 0;
}
用药统计
原题
算法标签 模拟 结构体
代码
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
const int N = 10005, K = 15;
struct Ill{
string id;
int k;
string drug[K];
}ill[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
map<string, int> mp;
vector<string> vs;
cin>>N;
for(int i=0;i<N;++i){
cin>>ill[i].id>>ill[i].k;
for(int j=0;j<ill[i].k;++j){
cin>>ill[i].drug[j];
mp[ill[i].drug[j]]++;
}
}
int mx=0 ;
string mxidx="";
for(auto it: mp){
if(it.y>mx){
mx = it.y;
mxidx = it.x;
}
}
for(int i=0;i<N;++i){
for(int j=0;j<ill[i].k;++j){
if(ill[i].drug[j]==mxidx){
vs.push_back(ill[i].id);
}
}
}
cout<<mxidx<<" "<<mx<<"\n";
for(auto vss: vs){
cout<<vss<<"\n";
}
return 0;
}
五彩斑斓的黑
原题
算法标签 模拟
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
unordered_map<string, int> mp;
cin>>n;
for(int i=0;i<n;++i){
string s;
cin>>s;
mp[s]++;
}
cout<<mp.size()<<"\n";
return 0;
}
假新闻
原题
算法标签 模拟 排序
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 10005, M = 105;
int a[M][N], b[M][N], c[N], mx[M];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
cin>>a[i][j];
}
}
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
int cnt=0;
for(int k=0;k<n;++k){
if(k-j){
if(a[i][k]-a[i][j]){
cnt++;
}
}
}
b[i][j]=cnt;
}
}
for(int i=0;i<m;++i){
int Mx=0;
for(int j=0;j<n;++j){
if(b[i][j]>Mx){
Mx = b[i][j];
}
}
mx[i]=Mx;
}
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(b[i][j]==mx[i]){
c[j]++;
}
}
}
int Max=0,Maxidx=0;
for(int i=0;i<m;++i){
if(c[i]>Max){
Max=c[i];
Maxidx=i;
}
}
cout<<Maxidx+1<<"\n";
return 0;
}
静态链表的秩
原题
算法标签 模拟 链表 排序
代码 排序
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
int a[N], b[N], c[N], d[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=0;i<n;++i){
cin>>a[i];
c[i]=a[i];
}
sort(c, c+n);
int head=0;
for(int i=0;i<n;++i){
if(c[i]!=i-1){
head=i-1;
break;
}
}
b[0]=head;
for(int i=1;i<=n;++i){
b[i]=a[head];
head=a[head];
}
for(int i=0;i<n;++i){
int t=0;
for(int j=0;j<n;++j){
if(b[j]==i){
t=j;
break;
}
}
d[i]=t;
}
for(int i=0;i<n;++i){
cout<<d[i] + 1<<" ";
}
return 0;
}
上述代码对于测试样例无问题,可能存在bug, 遇到问题欢迎一起交流沟通哈。
原创不易 转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈