总结
T1:
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;//数
int f(int x){//递归函数
if(x==0) return 1;//递归边界
return x*f(x-1);//递归表达式
}
int main(){
cin >> n;//输入
cout << f(n);//函数
return 0;
}
T2:
正确代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,k;//数/操作次数
int main(){
cin >> n >> k;//录入
ll quan = 1;//个位权值
for(ll i = 0;i<k;i++){
ll num = n/quan%10;//提取
n/=quan;//计算
if(num<=4) n-=num;//四舍五入
else n += (10-num);
n*=quan;//还原
quan*=10;//权值增加
}
cout << n;//输出
return 0;
}T3:
正确代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[200010],b[2000010],m=1;//元素数/序列/第二个序列
map<int,int> mp,mp2;//两个map
signed main(){
cin >> n;//录入
for(int i = 1;i<=n;i++){
cin >> a[i];
}
sort(a+1,a+1+n);//排序
b[1]=a[1];//设定初始值
for(int i = 2;i<=n;i++){//计算b[i]
if(a[i]!=a[i-1]) b[++m]=a[i];
}
for(int i = m;i>=1;i--) mp[b[i]]=m-i;//计算mp
for(int i = 1;i<=n;i++){//计算mp2
mp2[mp[a[i]]]++;
}
for(int k = 0;k<n;k++) cout << mp2[k]<<endl;//输出
return 0;
}T4:
正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 2e5 + 10;
int n, m, sx, sy, k;//行/列/起始x/起始y/障碍数
map<int, vector<int> > hang, lie;//行/列
int main() {
cin >> n >> m;//录入
cin >> sx >> sy;
cin >> k;
while (k--) {
int r, c;
cin >> r >> c;
hang[r].push_back(c); // 第r行第c列
lie[c].push_back(r);
}
//迭代器
for (auto &x : hang) {//行
x.second.push_back(0);
sort(x.second.begin(), x.second.end());
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);
}
int q;//操作
cin >> q;
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;
}T5:
错误代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll t;
map<ll,vector<ll>> m;
vector<ll> a;
int main(){
cin >> t;
while(t--){
string op;
cin >> op;
if(op=="ADD"){
ll x;
cin >> x;
a.push_back(x);
}else if(op=="DELETE"){
if(a.size()!=0)a.pop_back();
}else if(op=="SAVE"){
ll y;
cin >> y;
m[y]=a;
}else{
ll z;
cin >> z;
swap(m[z],a);
}
if(a.empty()) cout << -1 << ' ';
else cout << a.back() << ' ';
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = (1e5+5)*5;
ll n,m,a[N];
ll tree[N],fa[N];//tree[i]:第1个节点的值,fa[i]第i个节点的父亲
ll cnt;//cnt:节点编号
ll cur;//当前所在的节点便编号
map<ll,ll> mp;//mp[页码]=节点编号;
int main(){
ios::sync_with_stdio(false);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;
fa[cnt]=cur;
cur=cnt;
}else if(op=="DELETE"){
cur=fa[cur];
}
else if(op=="SAVE"){
cin >> x;
mp[x]=cur;
}else {
cin >> x;
cur = mp[x];
}
if(cur==0)cout << -1 << " ";
else cout << tree[cur] << " ";
}
return 0;
}