这是今天的比赛 8.18
T1 U599092 神奇函数
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int n,ans=1;
void f(int x){
if(x==0){
cout<<ans<<'\n';
return;
}
ans*=x;
f(x-1);
}
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
f(n);
return 0;
}ヾ(≧▽≦*)o
T2 U599097 四舍五入
赛时代码
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// string s;
ll k,j=0,a;
cin>>a>>k;
for(int i=1;i<=k;i++){
a=(a+5*pow(10,i-1))/pow(10,i);
a=a*pow(10,i);
// cout<<a<<endl;
}
cout<<a;
return 0;
}
ヾ(≧▽≦*)o
T3 U599134 k种大于的数
正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
map<int,int> mp,mp2;
int a[200005],b[200005];
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i];//将a数组的值放到b数组中
}
sort(b+1,b+n+1);//排序
int m=unique(b+1,b+n+1)-b-1;//STL——去重
for(int i=m;i>=1;i--) mp[b[i]]=m-i//统计比a[i]大的数字有多少种
for(int i=1;i<=n;i++) mp2[mp[a[i]]]++;//统计a数组中每个数字的mp值的数量
for(int i=0;i<n;i++) cout<<mp2[i]<<'\n';//输出
return 0;
}
T4 U599136 横冲直撞
正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int n,m,sx,sy,k;
map<int,vector<int> > hang,lie;
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>sx>>sy>>k;
while(k--){
int r,c;
cin>>r>>c;
hang[r].push_back(c);//第r行第c列
lie[c].push_back(r);
}
int q;
cin>>q;
for(auto &x:hang){
x.second.push_back(0);//哨兵,处理边界问题
sort(x.second.begin(),x.second.end());//将第x.first行的vector数组升序排列
x.second.push_back(m+1);//哨兵
}
for(auto &x:lie){
x.second.push_back(0);//哨兵,处理边界问题
sort(x.second.begin(),x.second.end());
x.second.push_back(n+1);//哨兵
}
while(q--){
char d;
int l;
cin>>d>>l;
if(d=='L'){
if(hang.count(sx)==0) sy=max(sy-l,1);
else{
int pos=lower_bound(hang[sx].begin(),hang[sx].end(),sy)-hang[sx].begin();
pos--;
sy=max(sy-l,hang[sx][pos]+1);
}
}
if(d=='R'){
if(hang.count(sx)==0) sy=min(sy+l,m);
else{
int pos=upper_bound(hang[sx].begin(),hang[sx].end(),sy)-hang[sx].begin();
sy=min(sy+l,hang[sx][pos]-1);
}
}
if(d=='U'){
if(lie.count(sy)==0) sx=max(sx-l,1);
else{
int pos=lower_bound(lie[sy].begin(),lie[sy].end(),sx)-lie[sy].begin();
pos--;
sx=max(sx-l,lie[sy][pos]+1);
}
}
if(d=='D'){
if(lie.count(sy)==0) sx=min(sx+l,n);
else{
int pos=upper_bound(lie[sy].begin(),lie[sy].end(),sx)-lie[sy].begin();
sx=min(sx+l,lie[sy][pos]-1);
}
}
cout<<sx<<" "<<sy<<endl;
}
return 0;
}T4 U599138 笔记本
正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
ll n,m,a[500005],tree[500005],fa[500005],cnt,cur;
map<ll,ll> mp;
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin>>q;
while(q--){
string op;
ll x;
cin>>op;
if(op=="ADD"){
cin>>x;
tree[++cnt]=x;//第cnt+1个节点的值
fa[cnt]=cur;//第cnt个节点的父亲为cur(cur也就是当前节点的编号)
cur=cnt;
}else if(op=="DELETE") cur=fa[cur];//将当前节点回溯
else if(op=="SAVE"){
cin>>x;
mp[x]=cur;
}else if(op=="LOAD"){
cin>>x;
cur=mp[x];
}
if(cur==0) cout<<-1<<" ";//如果一直没有节点产生输出-1
else cout<<tree[cur]<<" ";//否则输出当前节点的值
}
return 0;
}