L2-033 简单计算器 (25分)
//开两个栈直接模拟
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
int n; cin>>n;
stack<int>s1;
stack<char>s2;
for(int i = 1; i <= n; i++){
int x; cin>>x; s1.push(x);
}
for(int i = 1; i < n; i++){
char ch; cin>>ch; s2.push(ch);
}
while(s1.size()>1){
int n1 = s1.top(); s1.pop();
int n2 = s1.top(); s1.pop();
char op = s2.top(); s2.pop();
if(op=='+')
s1.push(n2+n1);
else if(op=='-')
s1.push(n2-n1);
else if(op=='*')
s1.push(n2*n1);
else{
if(n1==0){
cout<<"ERROR: "<<n2<<"/0\n";
return 0;
}
s1.push(n2/n1);
}
}
cout<<s1.top();
return 0;
}
L2-034 口罩发放 (25分)
//+ 在D天里发口罩,每个人获得要间隔P天。
//+ 给出每天的申请记录和口罩供应,包括string,id,siates,time;按照先后顺序判断每天发给谁。
//+ 并统计状态出问题的人,汇总输出。
//+ 直接模拟即可。能获得口罩的人,P天内没拿过,并且每天排名前几个。
#include<bits/stdc++.h>
using namespace std;
struct node{string name, id; int states; int t1, t2;};//申请,出现时间
bool cmp(node a, node b){return a.t1!=b.t1?a.t1<b.t1:a.t2<b.t2;}
int checkid(string s){
if(s.size() != 18)return 0;
for(int i = 0; i < 18; i++)//WA4,5,身份证必须是数字
if(!isdigit(s[i]))return 0;
return 1;
}
int main(){
int d, p; cin>>d>>p;
map<string,int>mp;//相隔天数
vector<node>ans; set<string>se;//生病的人
for(int i = 1; i <= d; i++){
int t, c; cin>>t>>c;
vector<node>tmp;
for(int j = 1; j <= t; j++){
string name, id; int states, mm, hh; char ch;
cin>>name>>id>>states>>hh>>ch>>mm;
if(checkid(id)){
tmp.push_back({name,id,states,hh*60+mm,j});
if(states==1 && se.count(id)==0){
ans.push_back({name,id,states,hh*60+mm,j});
se.insert(id);
}
}
}
if(tmp.size()==0 || c==0)continue;
sort(tmp.begin(),tmp.end(), cmp);
for(int j = 0; j < tmp.size() && c; j++){
if(mp.count(tmp[j].id)==0 || mp[tmp[j].id]+p+1<=i){
mp[tmp[j].id] = i;
c--;
cout<<tmp[j].name<<" "<<tmp[j].id<<"\n";
}
}
}
for(int i = 0; i < ans.size(); i++){
cout<<ans[i].name<<" "<<ans[i].id<<"\n";
}
return 0;
}
L2-035 完全二叉树的层序遍历 (25分)
//满二叉树后序转先序,输出层次遍历
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
int n, a[maxn];
int Tree[maxn], r;
void build(int x){
if(x>n)return ;
build(x<<1);
build(x<<1|1);
Tree[x] = a[++r];
}
int main(){
cin>>n;
for(int i = 1; i <= n; i++)
cin>>a[i];
build(1);
for(int i = 1; i <= n; i++){
if(i!=1)cout<<" ";
cout<<Tree[i];
}
return 0;
}
L2-036 网红点打卡攻略 (25分)
//+ n个点m条带权边。给出k条路径,判断能否从v1出发经过每个点一次并返回v1,输出最短的路径。
//+ n<200 所以邻接矩阵存个图,直接暴力枚举就行。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 1e9+10;
const int maxn = 220;
int e[maxn][maxn], vis[maxn];
int main(){
int n, m;
cin>>n>>m;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
e[i][j] = inf;
for(int i = 1; i <= m; i++){
int u, v, w;
cin>>u>>v>>w;
e[u][v] = e[v][u] = w;
}
int k; cin>>k;
int cnt = 0, po = 0, hf = inf;
for(int i = 1; i <= k; i++){
memset(vis,0,sizeof(vis));
int kk; cin>>kk;
int la = 0, tmphf = 0, ok = 1;
for(int j = 1; j <= kk; j++){
int x; cin>>x;
if(e[la][x]!=inf && !vis[x]){
//cout<<tmpfile<<" "<<e[la][x]<<"\n";
tmphf += e[la][x];
//cout<<tmpfile<<"\n";
la = x;
vis[x]++;
}else{
ok = 0;
}
}
for(int i = 1; i <= n; i++){
if(vis[i]!=1)ok = 0;
}
if(ok && e[la][0] != inf){
tmphf += e[la][0];
cnt++;
//cout<<i<<": "<<tmphf<<"\n";
if(tmphf < hf){
hf = tmphf;
po = i;
}
}
}
cout<<cnt<<"\n";
cout<<po<<" "<<hf<<"\n";
return 0;
}